user interest api

dev_course
p31729568 5 years ago
parent c31c07b453
commit e6999da273

@ -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

@ -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

@ -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) }

@ -0,0 +1,4 @@
class UserInterest < ApplicationRecord
belongs_to :user
belongs_to :repertoire
end

@ -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

@ -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
Loading…
Cancel
Save