forked from svsticky/constipated-koala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
participants_controller.rb
48 lines (38 loc) · 1.43 KB
/
participants_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#:nodoc:
class Api::ParticipantsController < ApiController
before_action -> { doorkeeper_authorize!('participant-read') }, only: :index
before_action lambda {
doorkeeper_authorize!('participant-write')
}, only: [:create, :update, :destroy]
def index
if params[:activity_id].present?
@participants = Participant.where(activity: Activity.find_by(id: params[:activity_id])).includes(
:activity, :member
)
elsif params[:member_id].present?
head(:unauthorized) && return unless Authorization._member.id == params[:member_id].to_i
@participants = Participant.where(member: Authorization._member).includes(:activity, :member)
else
head(:bad_request)
return
end
end
def create
@participant = Participant.new(member: Authorization._member,
activity: Activity.find(params[:activity_id]))
head(:bad_request) && return unless @participant.save
render(status: :created)
end
def update
raise(NotImplementedError)
end
# TODO: uitschrijfdeadline hierin meenemen
def destroy
participant = Participant.find_by!(member_id: Authorization._member.id,
activity_id: params[:activity_id])
head(:locked) && return if participant.paid
head(:locked) && return if participant.activity.start_date <= Date.today
participant.destroy!
head(:no_content)
end
end