@@ -8,7 +8,6 @@ class Reservation < ApplicationRecord
88 # ========================================
99 before_validation :set_defaults
1010 before_validation :generate_code , on : :create
11- before_validation :check_and_handle_reservation_conflicts , on : [ :create , :update ]
1211
1312 # ========================================
1413 # Validations
@@ -19,7 +18,7 @@ class Reservation < ApplicationRecord
1918 validates :priority , presence : true
2019 validates :start_time , presence : true
2120 validates :end_time , presence : true
22- validate :start_time_before_end_time
21+ validate :start_time_before_end_time
2322
2423 # ========================================
2524 # Default scope (Soft delete)
@@ -29,7 +28,7 @@ class Reservation < ApplicationRecord
2928 # ========================================
3029 # Soft delete method
3130 # ========================================
32- def soft_delete
31+ def soft_delete ( deleted_by : nil )
3332 update ( deleted_at : Time . current )
3433 end
3534
@@ -40,7 +39,7 @@ def self.with_deleted
4039 private
4140
4241 # ========================================
43- # Default values (NEW)
42+ # Default values
4443 # ========================================
4544 def set_defaults
4645 self . priority ||= 0
@@ -54,70 +53,13 @@ def generate_code
5453 end
5554
5655 # ========================================
57- # Custom validation methods
56+ # Time validation
5857 # ========================================
5958 def start_time_before_end_time
6059 return unless start_time && end_time
6160
6261 if start_time >= end_time
63- errors . add ( :start_time , "시작 시간은 종료 시간보다 빨라야 합니다. " )
62+ errors . add ( :start_time , "must be earlier than end_time " )
6463 end
6564 end
66-
67- def check_and_handle_reservation_conflicts
68- # 운영 시간 확인
69- unless is_available_during_operating_hours?
70- errors . add ( :base , "요청하신 시간은 스터디룸 운영 시간 범위에 포함되지 않거나 휴일입니다." )
71- throw :abort
72- end
73-
74- # 중복 예약 확인
75- conflicting_reservations = Reservation . where ( room_id : room_id )
76- . where . not ( id : id )
77- . where ( "start_time < ? AND end_time > ?" , end_time , start_time )
78-
79- conflicting_reservations . each do |existing_reservation |
80- if priority > existing_reservation . priority
81- existing_reservation . soft_delete
82- else
83- errors . add ( :base , "요청하신 시간에 이미 예약이 존재하며, 우선순위가 낮거나 같아 예약할 수 없습니다." )
84- throw :abort
85- end
86- end
87- end
88-
89- def is_available_during_operating_hours?
90- reservation_date = start_time . to_date
91- start_time_str = start_time . strftime ( '%H:%M' )
92- end_time_str = end_time . strftime ( '%H:%M' )
93-
94- # RoomException 우선 확인
95- room_exception = room . room_exceptions . find_by ( holiday_date : reservation_date )
96-
97- if room_exception . present?
98- # Case 1: 완전 휴무
99- if room_exception . opening_time . nil? && room_exception . closing_time . nil?
100- return false
101- end
102-
103- # Case 2: 특별 운영 시간
104- if room_exception . opening_time . present? && room_exception . closing_time . present?
105- exception_opening_time_str = room_exception . opening_time . strftime ( '%H:%M' )
106- exception_closing_time_str = room_exception . closing_time . strftime ( '%H:%M' )
107- return start_time_str >= exception_opening_time_str && end_time_str <= exception_closing_time_str
108- end
109-
110- # Case 3: 이상 데이터
111- return false
112- end
113-
114- # 일반 운영 시간 확인
115- operating_hour = room . room_operating_hours . find_by ( day_of_week : start_time . wday )
116- return false if operating_hour . nil?
117-
118- opening_time_str = operating_hour . opening_time . strftime ( '%H:%M' )
119- closing_time_str = operating_hour . closing_time . strftime ( '%H:%M' )
120-
121- start_time_str >= opening_time_str && end_time_str <= closing_time_str
122- end
12365end
0 commit comments