diff --git a/app/controllers/users/interests_controller.rb b/app/controllers/users/interests_controller.rb new file mode 100644 index 000000000..ca5f63af9 --- /dev/null +++ b/app/controllers/users/interests_controller.rb @@ -0,0 +1,25 @@ +class Users::InterestsController < Users::BaseController + before_action :require_login + before_action :private_user_resources! + + def create + identity = params[:identity].to_s.strip + + extension = observed_user.user_extension || observed_user.build_user_extension + return render_error('请选择职业') unless %w(teacher student professional).include?(identity) + + ActiveRecord::Base.transaction do + extension.update_column(:identity, identity) + + # 兴趣 + observed_user.user_interests.delete_all + UserInterest.bulk_insert(:user_id, :repertoire_id) do |worker| + (Repertoire.pluck(:id) & Array.wrap(params[:interest_ids]).map(&:to_i)).each do |repertoire_id| + worker.add(user_id: observed_user.id, repertoire_id: repertoire_id) + end + end + end + + render_ok + end +end \ No newline at end of file diff --git a/app/models/repertoire.rb b/app/models/repertoire.rb index d76a022c5..72e1ebfff 100644 --- a/app/models/repertoire.rb +++ b/app/models/repertoire.rb @@ -2,4 +2,5 @@ class Repertoire < ApplicationRecord has_many :sub_repertoires, ->{order(updated_at: :desc)}, :dependent => :destroy has_many :tag_repertoires, through: :sub_repertoires + has_many :user_interests, dependent: :delete_all end diff --git a/app/models/user.rb b/app/models/user.rb index e775af5f8..7e04ff423 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -115,6 +115,10 @@ class User < ApplicationRecord has_many :attendances + # 兴趣 + has_many :user_interests, dependent: :delete_all + has_many :interests, through: :user_interests, source: :repertoire + # Groups and active users scope :active, lambda { where(status: STATUS_ACTIVE) } diff --git a/app/models/user_interest.rb b/app/models/user_interest.rb new file mode 100644 index 000000000..bdb6e77d4 --- /dev/null +++ b/app/models/user_interest.rb @@ -0,0 +1,4 @@ +class UserInterest < ApplicationRecord + belongs_to :user + belongs_to :repertoire +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index cf5bad608..f0123d95b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -47,6 +47,7 @@ Rails.application.routes.draw do resource :experience_records, only: [:show] resource :grade_records, only: [:show] resource :watch, only: [:create, :destroy] + resource :interest, only: [:create] end diff --git a/db/migrate/20190706010307_create_user_interests.rb b/db/migrate/20190706010307_create_user_interests.rb new file mode 100644 index 000000000..d64a1c228 --- /dev/null +++ b/db/migrate/20190706010307_create_user_interests.rb @@ -0,0 +1,8 @@ +class CreateUserInterests < ActiveRecord::Migration[5.2] + def change + create_table :user_interests do |t| + t.references :user + t.references :repertoire + end + end +end