@@ -9,8 +9,6 @@ class SchedulesController < ApplicationController
99
1010 # 예약 상세 조회
1111 def show
12- return render json : BaseResponse . of_failure ( Failure ::NO_PERMISSION_SHOW ) . to_json ,
13- status : Failure ::NO_PERMISSION_SHOW . status_code unless admin? || @current_user . eql? ( @schedule . user )
1412 render json : BaseResponse . of_success ( Success ::GET_RESERVATION_DETAIL , ScheduleDetail . new ( @schedule ) ) . to_json ,
1513 status : Success ::GET_RESERVATION_DETAIL . status_code
1614 end
@@ -24,8 +22,8 @@ def index
2422
2523 # 예약 신청 가능 시간 및 인원 조회
2624 def available
27- start_date = target_month_start_date
28- end_date = target_month_start_date . end_of_month
25+ start_date = target_start_date
26+ end_date = target_start_date . end_of_month
2927
3028 confirmed_schedules = Schedule . confirmed . where ( start_datetime : start_date ..end_date )
3129
@@ -43,36 +41,40 @@ def create
4341 return render json : BaseResponse . of_failure ( Failure ::IMPOSSIBLE_CREATE_TOO_LATE ) . to_json ,
4442 status : Failure ::IMPOSSIBLE_CREATE_TOO_LATE . status_code if too_late?
4543 return render json : BaseResponse . of_failure ( Failure ::OVER_PERSONNEL_TO_RESERVE ) . to_json ,
46- status : Failure ::OVER_PERSONNEL_TO_RESERVE . status_code if exceeds_capacity ?
44+ status : Failure ::OVER_PERSONNEL_TO_RESERVE . status_code if exceeds_personnel ?
4745
48- schedule = @current_user . schedules . create! ( create_schedule_body )
46+ schedule = @current_user . schedules . create! ( schedule_content )
4947 render json : BaseResponse . of_success ( Success ::CREATE_RESERVATION , schedule ) . to_json ,
5048 status : Success ::CREATE_RESERVATION . status_code
5149 end
5250
5351 # 예약 확정 (Admin only)
5452 def confirm
5553 return render json : BaseResponse . of_failure ( Failure ::OVER_PERSONNEL_TO_CONFIRM ) . to_json ,
56- status : Failure ::OVER_PERSONNEL_TO_CONFIRM . status_code if exceeds_capacity ?( true )
54+ status : Failure ::OVER_PERSONNEL_TO_CONFIRM . status_code if exceeds_personnel ?( true )
5755 return render json : BaseResponse . of_failure ( Failure ::NO_PERMISSION_CONFIRM ) . to_json ,
58- status : Failure ::NO_PERMISSION_CONFIRM . status_code unless admin? || @schedule . user ( @current_user )
56+ status : Failure ::NO_PERMISSION_CONFIRM . status_code unless admin?
5957
6058 @schedule . update! ( is_confirm : true )
6159 render status : Success ::CONFIRM_RESERVATION . status_code
6260 end
6361
6462 # 예약 수정
6563 def update
64+ return render json : BaseResponse . of_failure ( Failure ::IMPOSSIBLE_UPDATE_NOT_OWNER ) . to_json ,
65+ status : Failure ::IMPOSSIBLE_UPDATE_NOT_OWNER . status_code unless admin? || owner?
6666 return render json : BaseResponse . of_failure ( Failure ::IMPOSSIBLE_UPDATE_ALREADY_CONFIRM ) . to_json ,
6767 status : Failure ::IMPOSSIBLE_UPDATE_ALREADY_CONFIRM . status_code if !admin? && @schedule . is_confirm
6868 return render json : BaseResponse . of_failure ( Failure ::OVER_PERSONNEL_TO_CONFIRM ) . to_json ,
69- status : Failure ::OVER_PERSONNEL_TO_CONFIRM . status_code if exceeds_capacity ?
70- @schedule . update! ( create_schedule_body )
69+ status : Failure ::OVER_PERSONNEL_TO_CONFIRM . status_code if exceeds_personnel ?
70+ @schedule . update! ( schedule_content )
7171 render status : Success ::MODIFY_RESERVATION . status_code
7272 end
7373
7474 # 예약 삭제
7575 def destroy
76+ return render json : BaseResponse . of_failure ( Failure ::IMPOSSIBLE_DELETE_NOT_OWNER ) . to_json ,
77+ status : Failure ::IMPOSSIBLE_DELETE_NOT_OWNER . status_code unless admin? || owner?
7678 return render json : BaseResponse . of_failure ( Failure ::IMPOSSIBLE_DELETE_ALREADY_CONFIRM ) . to_json ,
7779 status : Failure ::IMPOSSIBLE_DELETE_ALREADY_CONFIRM . status_code if !admin? && @schedule . is_confirm
7880 @schedule . destroy
@@ -81,46 +83,58 @@ def destroy
8183
8284 private
8385 def authenticate_user
84- @current_user = User . find_by ( id : request . env [ "user_id" ] )
85- render json : BaseResponse . of_failure ( Failure ::NOT_FOUND_USER ) . to_json ,
86- status : Failure ::NOT_FOUND_USER . status_code unless @current_user
86+ begin
87+ @current_user = User . find_by ( id : request . env [ "user_id" ] )
88+ rescue ActiveRecord ::RecordNotFound
89+ render json : BaseResponse . of_failure ( Failure ::NOT_FOUND_USER ) . to_json ,
90+ status : Failure ::NOT_FOUND_USER . status_code
91+ end
8792 end
8893
8994 def admin?
9095 @current_user &.admin?
9196 end
9297
93- def target_month_start_date
94- params [ :target_month ] . present? ? Date . strptime ( params [ :target_month ] . to_s , "%Y-%m" ) : Date . today
98+ def owner?
99+ @schedule . is_owner? ( @current_user . id )
95100 end
96101
97- def find_schedule
98- @schedule = admin? ? Schedule . find ( params [ :id ] ) : @current_user . schedules . find ( params [ :id ] )
99- render json : BaseResponse . of_failure ( Failure ::NOT_FOUND_SCHEDULE ) . to_json ,
100- status : Failure ::NOT_FOUND_SCHEDULE . status_code unless @schedule
102+ def too_late?
103+ params [ :start_datetime ] . to_date <= 3 . days . from_now . to_date
101104 end
102105
103- def find_all_schedules
104- @schedules = admin ? ? Schedule . all : Schedule . where ( user_id : request . env [ "user_id" ] )
106+ def target_start_date
107+ params [ :target_month ] . present ? ? Date . strptime ( params [ :target_month ] . to_s , "%Y-%m" ) : Date . today
105108 end
106109
107- def create_schedule_body
108- params . require ( :schedule ) . permit ( :start_datetime , :end_datetime , :personnel )
110+ def schedule_content
111+ params . require ( :schedule ) . permit ( :name , : start_datetime, :end_datetime , :personnel )
109112 end
110113
111- def too_late?
112- params [ :start_datetime ] . to_date <= 3 . days . from_now . to_date
114+ def exceeds_personnel? ( is_confirm = false )
115+ current_personnel = Schedule . confirmed . where ( start_datetime : params [ :start_datetime ] ) . sum ( :personnel )
116+ additional_personnel = is_confirm ? @schedule . personnel : params [ :personnel ] . to_i
117+ ( current_personnel + additional_personnel ) > 50_000
113118 end
114119
115- def exceeds_capacity? ( confirming = false )
116- current_count = Schedule . confirmed . where ( start_datetime : params [ :start_datetime ] ) . sum ( :personnel )
117- new_count = confirming ? @schedule . personnel : params [ :personnel ] . to_i
118- ( current_count + new_count ) > 50_000
120+ def find_schedule
121+ begin
122+ @schedule = Schedule . find ( params [ :id ] )
123+ render json : BaseResponse . of_failure ( Failure ::NO_PERMISSION_SHOW ) . to_json ,
124+ status : Failure ::NO_PERMISSION_SHOW . status_code if !admin? && !owner?
125+ rescue ActiveRecord ::RecordNotFound
126+ render json : BaseResponse . of_failure ( Failure ::NOT_FOUND_SCHEDULE ) . to_json ,
127+ status : Failure ::NOT_FOUND_SCHEDULE . status_code
128+ end
129+ end
130+
131+ def find_all_schedules
132+ @schedules = admin? ? Schedule . all : Schedule . where ( user_id : request . env [ "user_id" ] )
119133 end
120134
121135 def calculate_available_times ( schedules , start_date , end_date )
122136 available_slots = { }
123- # 해당 월의 각 날짜를 순회하며 기본 값(50,000명 가능) 설정
137+
124138 ( start_date ..end_date ) . each do |date |
125139 datetime = date . to_datetime
126140 if datetime >= Date . today + 3 . days
@@ -130,7 +144,7 @@ def calculate_available_times(schedules, start_date, end_date)
130144 end
131145 end
132146
133- # 예약된 일정 반영하여 예약 가능 인원 조정
147+
134148 schedules . each do |schedule |
135149 ( schedule . start_datetime . hour ..schedule . end_datetime . hour - 1 ) . each do |hour |
136150 datetime = schedule . start_datetime . to_datetime . change ( hour : hour )
0 commit comments