competition: join team apig

dev_forum
p31729568 5 years ago
parent d9eb84eafc
commit 0536b357bc

@ -4,9 +4,16 @@ class Competitions::CompetitionTeamsController < Competitions::BaseController
end end
def create def create
if current_competition.personal? # 个人赛报名
Competitions::CreatePersonalTeamService.call(current_competition, current_user)
else
team = current_competition.competition_teams.new(user: current_user) team = current_competition.competition_teams.new(user: current_user)
Competitions::SaveTeamService.call(team, save_params) Competitions::SaveTeamService.call(team, save_params)
end
render_ok render_ok
rescue Competitions::CreatePersonalTeamService::Error => ex
render_error(ex.message)
end end
def update def update
@ -22,6 +29,20 @@ class Competitions::CompetitionTeamsController < Competitions::BaseController
render_error(ex.message) render_error(ex.message)
end 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 private
def all_competition_teams def all_competition_teams

@ -23,6 +23,11 @@ class Competition < ApplicationRecord
status? status?
end end
# 是否为个人赛
def personal?
competition_staffs.maximum(:maximum) == 1
end
# 报名是否结束 # 报名是否结束
def enroll_ended? def enroll_ended?
enroll_end_time.blank? || enroll_end_time < Time.now enroll_end_time.blank? || enroll_end_time < Time.now

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

@ -17,6 +17,7 @@ class Competitions::SaveTeamService < ApplicationService
is_teacher = team.user.is_teacher? is_teacher = team.user.is_teacher?
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
team.generate_invite_code if new_record team.generate_invite_code if new_record
team.team_type = 1 # 组队竞赛
team.name = params[:name].to_s.strip team.name = params[:name].to_s.strip
team.save! team.save!

@ -676,6 +676,7 @@ Rails.application.routes.draw do
resource :competition_staff resource :competition_staff
resources :competition_teams, only: [:index, :show] do resources :competition_teams, only: [:index, :show] do
post :join, on: :collection post :join, on: :collection
post :leave, on: :member
end end
resources :teachers, only: [:index] resources :teachers, only: [:index]
resources :students, only: [:index] resources :students, only: [:index]

Loading…
Cancel
Save