diff --git a/app/assets/javascripts/admins/competition_settings/index.js b/app/assets/javascripts/admins/competition_settings/index.js index 366838627..4ee6235eb 100644 --- a/app/assets/javascripts/admins/competition_settings/index.js +++ b/app/assets/javascripts/admins/competition_settings/index.js @@ -57,7 +57,7 @@ $(document).on('turbolinks:load', function(){ $basicForm.find('.error').html(''); var valid = $basicForm.valid(); - if ($("input[name='mode'[checked]]").val() == 2) { + if ($("input[name='mode']:checked").val() == 2) { var $courseId = $("input[name='course_id'"); if ($courseId.val() === undefined || $course_id.val().length === 0) { $courseId.addClass('danger text-danger'); @@ -65,7 +65,7 @@ $(document).on('turbolinks:load', function(){ } else { $courseId.removeClass('danger text-danger'); } - } else if ($("input[name='mode'[checked]]").val() == 4) { + } else if ($("input[name='mode']:checked").val() == 4) { var $techStartTime = $("input[name='teach_start_time'"); var $techEndTime = $("input[name='teach_end_time'"); if ($techStartTime.val() === undefined || $techStartTime.val().length === 0) { @@ -85,7 +85,7 @@ $(document).on('turbolinks:load', function(){ if (!valid) return; $.ajax({ - method: 'PUT', + method: 'POST', dataType: 'json', url: $basicForm.attr('action'), data: new FormData($basicForm[0]), @@ -97,10 +97,10 @@ $(document).on('turbolinks:load', function(){ }, error: function (res) { var data = res.responseJSON; - $form.find('.error').html(data.message); + $basicForm.find('.error').html(data.message); }, complete: function () { - $form.find('.submit-btn').attr('disabled', false); + $basicForm.find('.submit-btn').attr('disabled', false); } }); }); diff --git a/app/controllers/admins/competition_settings_controller.rb b/app/controllers/admins/competition_settings_controller.rb index a1ac27d8c..f4863458c 100644 --- a/app/controllers/admins/competition_settings_controller.rb +++ b/app/controllers/admins/competition_settings_controller.rb @@ -4,7 +4,12 @@ class Admins::CompetitionSettingsController < Admins::BaseController end def basic_setting - Admins::CompetitionBasicSettingService.call(current_competition, form_params) + Admins::CompetitionBasicSettingService.call(current_competition, basic_form_params) + render_ok + end + + def nav_setting + Admins::CompetitionNavSettingService.call(current_competition, basic_form_params) render_ok end @@ -14,7 +19,7 @@ class Admins::CompetitionSettingsController < Admins::BaseController @_current_competition ||= Competition.find(params[:competition_id]) end - def form_params - params.permit(:identifier, :name, :nav_logo, :login_logo, :tab_logo, :footer, navbar: %i[name link hidden]) + def basic_form_params + params.permit(:identifier, :name, :sub_title, :start_time, :end_time, :mode, :identifier, :bonus, :awards_count, :description, :course_id, :teach_start_time, :teach_end_time) end end \ No newline at end of file diff --git a/app/models/competition.rb b/app/models/competition.rb index 708af2556..44b38f321 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -100,10 +100,21 @@ class Competition < ApplicationRecord private + def get_module_name type + case type + when 'home' then '首页' + when 'enroll' then '报名' + when 'inform' then '通知公告' + when 'chart' then '排行榜' + when 'resource' then '资料下载' + else '' + end + end + def create_competition_modules - CompetitionModule.bulk_insert(*%i[competition_id name position created_at updated_at]) do |worker| - %w(首页 报名 通知公告 排行榜 资料下载).each_with_index do |name, index| - worker.add(competition_id: id, name: name, position: index + 1) + CompetitionModule.bulk_insert(*%i[competition_id module_type name position created_at updated_at]) do |worker| + %w(home enroll inform chart resource).each_with_index do |type, index| + worker.add(competition_id: id, module_type: type, name: get_module_name(type), position: index + 1, ) end end end diff --git a/app/models/competition_module.rb b/app/models/competition_module.rb index 9579533da..76f7610d2 100644 --- a/app/models/competition_module.rb +++ b/app/models/competition_module.rb @@ -6,16 +6,16 @@ class CompetitionModule < ApplicationRecord has_one :competition_module_md_content, dependent: :destroy def module_url - case name - when "首页", "赛制介绍" + case module_type + when "home" "/competitions/#{competition.identifier}" - when "通知公告" + when "inform" "/competitions/#{competition.identifier}/informs?status=1" - when "参赛手册" + when "manual" "/competitions/#{competition.identifier}/informs?status=2" - when "排行榜" + when "chart" "/competitions/#{competition.identifier}/charts" - when "报名" + when "enroll" "/competitions/#{competition.identifier}/competition_teams" else url || "/competitions/#{competition.identifier}/md_content?md_content_id=#{competition_module_md_content&.id}" diff --git a/app/services/admins/competition_basic_setting.rb b/app/services/admins/competition_basic_setting_service.rb similarity index 62% rename from app/services/admins/competition_basic_setting.rb rename to app/services/admins/competition_basic_setting_service.rb index b8e2c3686..e5a7dd6be 100644 --- a/app/services/admins/competition_basic_setting.rb +++ b/app/services/admins/competition_basic_setting_service.rb @@ -13,7 +13,7 @@ class Admins::CompetitionBasicSettingService < ApplicationService competition.start_time = params[:start_time] competition.end_time = params[:end_time] competition.mode = params[:mode] - competition.identifier = strip params[:indentifier] + competition.identifier = strip params[:identifier] competition.bonus = params[:bonus] competition.awards_count = params[:awards_count] competition.description = strip params[:description] @@ -22,9 +22,18 @@ class Admins::CompetitionBasicSettingService < ApplicationService if competition.mode == 1 || competition.mode == 4 competition.competition_mode_setting.destroy - elsif competition.mode == 2 - + else + setting = competition.competition_mode_setting || CompetitionModeSetting.create!(competition_id: competition.id) + if competition.mode == 2 + setting.course_id = params[:course_id] + elsif competition.mode == 3 + setting.start_time = params[:teach_start_time] + setting.end_time = params[:teach_end_time] + end + setting.save! end + + competition end end end \ No newline at end of file diff --git a/app/services/admins/competition_nav_setting_service.rb b/app/services/admins/competition_nav_setting_service.rb new file mode 100644 index 000000000..1923f4e4c --- /dev/null +++ b/app/services/admins/competition_nav_setting_service.rb @@ -0,0 +1,39 @@ +class Admins::CompetitionNavSettingService < ApplicationService + attr_reader :competition, :params + + def initialize(competition, params) + @params = params + @competition = competition + end + + def call + ActiveRecord::Base.transaction do + competition.name = strip params[:name] + competition.sub_title = strip params[:sub_title] + competition.start_time = params[:start_time] + competition.end_time = params[:end_time] + competition.mode = params[:mode] + competition.identifier = strip params[:identifier] + competition.bonus = params[:bonus] + competition.awards_count = params[:awards_count] + competition.description = strip params[:description] + + competition.save! + + if competition.mode == 1 || competition.mode == 4 + competition.competition_mode_setting.destroy + else + setting = competition.competition_mode_setting || CompetitionModeSetting.create!(competition_id: competition.id) + if competition.mode == 2 + setting.course_id = params[:course_id] + elsif competition.mode == 3 + setting.start_time = params[:teach_start_time] + setting.end_time = params[:teach_end_time] + end + setting.save! + end + + competition + end + end +end \ No newline at end of file diff --git a/app/views/competitions/competitions/common_header.json.jbuilder b/app/views/competitions/competitions/common_header.json.jbuilder index 882d1d1c0..88d7925a7 100644 --- a/app/views/competitions/competitions/common_header.json.jbuilder +++ b/app/views/competitions/competitions/common_header.json.jbuilder @@ -17,7 +17,7 @@ end json.stages -if @competition.mode == 1 +if @competition.mode == 2 json.course_id @competition.competition_mode_setting&.course_id json.member_of_course @user.member_of_course?(@competition.competition_mode_setting&.course) end diff --git a/db/migrate/20191023074837_migrate_competition_module_type.rb b/db/migrate/20191023074837_migrate_competition_module_type.rb new file mode 100644 index 000000000..009b65cc9 --- /dev/null +++ b/db/migrate/20191023074837_migrate_competition_module_type.rb @@ -0,0 +1,28 @@ +class MigrateCompetitionModuleType < ActiveRecord::Migration[5.2] + def change + add_column :competition_modules, :module_type, :string + + Competition.all.each do |competition| + competition.competition_modules.each do |com_module| + mod_type = "" + case com_module.name + when '首页' + mod_type = "home" + when '报名' + mod_type = "enroll" + when '通知公告' + mod_type = "inform" + when '参赛手册' + mod_type = "manual" + when '排行榜' + mod_type = "chart" + when '资料下载 ' + mod_type = "resource" + else + mod_type = "md" + end + com_module.update_attributes!(module_type: mod_type) + end + end + end +end