From 02b0851e38de2b652ef2c850f117b27e229ad4aa Mon Sep 17 00:00:00 2001 From: p31729568 Date: Thu, 18 Jul 2019 16:05:24 +0800 Subject: [PATCH] competition module update api modify --- .../competitions/competition_modules_controller.rb | 12 +++++++++++- app/models/searchable/dependents/user.rb | 4 ++-- .../competition_modules/show.json.jbuilder | 3 +++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/controllers/competitions/competition_modules_controller.rb b/app/controllers/competitions/competition_modules_controller.rb index da9873c43..c4692af70 100644 --- a/app/controllers/competitions/competition_modules_controller.rb +++ b/app/controllers/competitions/competition_modules_controller.rb @@ -15,7 +15,17 @@ class Competitions::CompetitionModulesController < Competitions::BaseController md = current_module.competition_module_md_content || current_module.build_competition_module_md_content md.name = params[:md_name] md.content = params[:md_content] - md.save! + + ActiveRecord::Base.transaction do + md.save! + + attachment_ids = Array.wrap(params[:attachment_ids]).map(&:to_i) + old_attachment_ids = md.attachments.pluck(:id) + + destroy_ids = old_attachment_ids - attachment_ids + md.attachments.where(id: destroy_ids).delete_all + Attachment.where(id: attachment_ids - old_attachment_ids).update_all(container: md) + end render_ok end diff --git a/app/models/searchable/dependents/user.rb b/app/models/searchable/dependents/user.rb index 103131ed6..f6dcaa430 100644 --- a/app/models/searchable/dependents/user.rb +++ b/app/models/searchable/dependents/user.rb @@ -10,13 +10,13 @@ module Searchable::Dependents::User def check_searchable_dependents if firstname_previously_changed? || lastname_previously_changed? || user_extension.school_id_previously_changed? # reindex shixun - created_shixuns.each{ |shixun| shixun.reindex } + created_shixuns.each(&:reindex) # reindex course manage_courses.each(&:reindex) # reindex subject - created_subjects.each { |subject| subject.reindex } + created_subjects.each(&:reindex) end end end \ No newline at end of file diff --git a/app/views/competitions/competition_modules/show.json.jbuilder b/app/views/competitions/competition_modules/show.json.jbuilder index d47742cf0..69cdcc544 100644 --- a/app/views/competitions/competition_modules/show.json.jbuilder +++ b/app/views/competitions/competition_modules/show.json.jbuilder @@ -5,4 +5,7 @@ if md.present? json.md_name md.name json.md_content md.content json.created_at md.created_at.strftime('%Y-%m-%d %H:%M:%S') + json.attachments do + json.array! md.attachments, partial: 'attachments/attachment_simple', as: :attachment + end end \ No newline at end of file