From 0536b357bc4d1a309ca38647f182477b416a7cd9 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Thu, 18 Jul 2019 14:48:02 +0800 Subject: [PATCH] competition: join team apig --- .../competition_teams_controller.rb | 25 +++++++++++++++-- app/models/competition.rb | 5 ++++ .../create_personal_team_service.rb | 27 +++++++++++++++++++ .../competitions/save_team_service.rb | 1 + config/routes.rb | 1 + 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 app/services/competitions/create_personal_team_service.rb diff --git a/app/controllers/competitions/competition_teams_controller.rb b/app/controllers/competitions/competition_teams_controller.rb index 34c80d24c..b4861c4d3 100644 --- a/app/controllers/competitions/competition_teams_controller.rb +++ b/app/controllers/competitions/competition_teams_controller.rb @@ -4,9 +4,16 @@ class Competitions::CompetitionTeamsController < Competitions::BaseController end def create - team = current_competition.competition_teams.new(user: current_user) - Competitions::SaveTeamService.call(team, save_params) + if current_competition.personal? # 个人赛报名 + Competitions::CreatePersonalTeamService.call(current_competition, current_user) + else + team = current_competition.competition_teams.new(user: current_user) + Competitions::SaveTeamService.call(team, save_params) + end render_ok + + rescue Competitions::CreatePersonalTeamService::Error => ex + render_error(ex.message) end def update @@ -22,6 +29,20 @@ class Competitions::CompetitionTeamsController < Competitions::BaseController render_error(ex.message) end + def leave + team = current_competition.competition_teams.find(params[:id]) + member = team.team_members.find_by(user_id: current_user.id) + return render_error('您不是该战队的成员') if member.blank? + + if member.user_id == team.user_id + team.destroy! # 队长退出,战队解散 + else + member.destroy! + end + + render_ok + end + private def all_competition_teams diff --git a/app/models/competition.rb b/app/models/competition.rb index f5d146ab4..024478ad6 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -23,6 +23,11 @@ class Competition < ApplicationRecord status? end + # 是否为个人赛 + def personal? + competition_staffs.maximum(:maximum) == 1 + end + # 报名是否结束 def enroll_ended? enroll_end_time.blank? || enroll_end_time < Time.now diff --git a/app/services/competitions/create_personal_team_service.rb b/app/services/competitions/create_personal_team_service.rb new file mode 100644 index 000000000..12b595cae --- /dev/null +++ b/app/services/competitions/create_personal_team_service.rb @@ -0,0 +1,27 @@ +class Competitions::CreatePersonalTeamService < ApplicationService + Error = Class.new(StandardError) + + attr_reader :competition, :user + + def initialize(competition, user) + @competition = competition + @user = user + end + + def call + raise Error, '个人赛才能报名' unless competition.personal? + + is_teacher = user.is_teacher? + raise Error, '本竞赛的参赛者限定为:学生' if is_teacher && competition.teacher_enroll_forbidden? + raise Error, '本竞赛的参赛者限定为:教师' if !is_teacher && competition.member_enroll_forbidden? + + enrolled = competition.competition_teams.exists?(user_id: user.id) + multiple_limited = (is_teacher && competition.teacher_multiple_limited?) || (!is_teacher && competition.member_multiple_limited?) + raise Error, '您已报名该竞赛' if enrolled && multiple_limited + + ActiveRecord::Base.transaction do + team = competition.competition_teams.create!(name: user.show_name, user_id: user.id) + team.team_members.create!(competition_id: competition, user_id: user.id, role: 1, is_teacher: is_teacher) + end + end +end \ No newline at end of file diff --git a/app/services/competitions/save_team_service.rb b/app/services/competitions/save_team_service.rb index c134e70d7..1021e6e6d 100644 --- a/app/services/competitions/save_team_service.rb +++ b/app/services/competitions/save_team_service.rb @@ -17,6 +17,7 @@ class Competitions::SaveTeamService < ApplicationService is_teacher = team.user.is_teacher? ActiveRecord::Base.transaction do team.generate_invite_code if new_record + team.team_type = 1 # 组队竞赛 team.name = params[:name].to_s.strip team.save! diff --git a/config/routes.rb b/config/routes.rb index dc521266c..a5742494d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -676,6 +676,7 @@ Rails.application.routes.draw do resource :competition_staff resources :competition_teams, only: [:index, :show] do post :join, on: :collection + post :leave, on: :member end resources :teachers, only: [:index] resources :students, only: [:index]