diff --git a/app/controllers/trustie_hacks_controller.rb b/app/controllers/trustie_hacks_controller.rb index d66f5a215..7e76e3f4c 100644 --- a/app/controllers/trustie_hacks_controller.rb +++ b/app/controllers/trustie_hacks_controller.rb @@ -36,6 +36,15 @@ class TrustieHacksController < ApplicationController render_ok end + def entry + if @hack.hack_users.exists?(user_id: current_user) + render_error('已经报名,请勿重复操作') + else + @hack.hack_users.create(user_id: current_user) + render_ok + end + end + private diff --git a/app/models/trustie_hack.rb b/app/models/trustie_hack.rb index 898bf2195..b436007ca 100644 --- a/app/models/trustie_hack.rb +++ b/app/models/trustie_hack.rb @@ -1,4 +1,9 @@ class TrustieHack < ApplicationRecord has_many :hack_users, :dependent => :destroy belongs_to :trustie_hackathon, counter_cache: true + + + def entry_info(user_id) + hack_users.exists?(user_id: user_id) + end end diff --git a/app/views/trustie_hacks/index.json.jbuilder b/app/views/trustie_hacks/index.json.jbuilder index 05e3fd7c7..0f4d4f060 100644 --- a/app/views/trustie_hacks/index.json.jbuilder +++ b/app/views/trustie_hacks/index.json.jbuilder @@ -5,5 +5,6 @@ end json.hacks @hacks do |hack| json.(hack, :id, :name, :description, :hack_users_count) + json.entry_info hack.entry_info(current_user.id) end diff --git a/config/routes.rb b/config/routes.rb index c0231a701..b2a7b1de6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -880,7 +880,9 @@ Rails.application.routes.draw do get :edit_hackathon post :update_hackathon end - + member do + post :entry + end end end diff --git a/db/migrate/20191106061342_add_uniq_index_for_hack_users.rb b/db/migrate/20191106061342_add_uniq_index_for_hack_users.rb new file mode 100644 index 000000000..6d1924d92 --- /dev/null +++ b/db/migrate/20191106061342_add_uniq_index_for_hack_users.rb @@ -0,0 +1,5 @@ +class AddUniqIndexForHackUsers < ActiveRecord::Migration[5.2] + def change + add_index :hack_users, [:user_id, :trustie_hack_id], unique: true + end +end