11package com .halolight .controller ;
22
33import com .halolight .dto .ApiResponse ;
4+ import com .halolight .security .UserPrincipal ;
45import com .halolight .service .CalendarService ;
56import com .halolight .web .dto .calendar .CreateEventRequest ;
67import com .halolight .web .dto .calendar .EventResponse ;
1920import org .springframework .format .annotation .DateTimeFormat ;
2021import org .springframework .http .HttpStatus ;
2122import org .springframework .http .ResponseEntity ;
22- import org .springframework .security .core .Authentication ;
23+ import org .springframework .security .core .annotation . AuthenticationPrincipal ;
2324import org .springframework .web .bind .annotation .*;
2425
2526import java .time .Instant ;
@@ -38,14 +39,13 @@ public class CalendarController {
3839 @ Operation (summary = "Get all events" , description = "Retrieve all calendar events for the current user with optional date range filter" )
3940 @ GetMapping
4041 public ResponseEntity <ApiResponse <List <EventResponse >>> getAllEvents (
41- Authentication authentication ,
42+ @ AuthenticationPrincipal UserPrincipal user ,
4243 @ Parameter (description = "Start date (ISO-8601 format)" )
4344 @ RequestParam (required = false ) @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant start ,
4445 @ Parameter (description = "End date (ISO-8601 format)" )
4546 @ RequestParam (required = false ) @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant end
4647 ) {
47- String userId = getUserId (authentication );
48- List <EventResponse > events = calendarService .getAllEvents (userId , start , end );
48+ List <EventResponse > events = calendarService .getAllEvents (user .getId (), start , end );
4949 return ResponseEntity .ok (ApiResponse .success (events ));
5050 }
5151
@@ -89,11 +89,10 @@ public ResponseEntity<ApiResponse<EventResponse>> getEventById(@PathVariable Str
8989 @ Operation (summary = "Create event" , description = "Create a new calendar event" )
9090 @ PostMapping
9191 public ResponseEntity <ApiResponse <EventResponse >> createEvent (
92- Authentication authentication ,
92+ @ AuthenticationPrincipal UserPrincipal user ,
9393 @ Valid @ RequestBody CreateEventRequest request
9494 ) {
95- String userId = getUserId (authentication );
96- EventResponse event = calendarService .createEvent (userId , request );
95+ EventResponse event = calendarService .createEvent (user .getId (), request );
9796 return ResponseEntity .status (HttpStatus .CREATED )
9897 .body (ApiResponse .success ("Event created successfully" , event ));
9998 }
@@ -102,49 +101,45 @@ public ResponseEntity<ApiResponse<EventResponse>> createEvent(
102101 @ PutMapping ("/{id}" )
103102 public ResponseEntity <ApiResponse <EventResponse >> updateEvent (
104103 @ PathVariable String id ,
105- Authentication authentication ,
104+ @ AuthenticationPrincipal UserPrincipal user ,
106105 @ Valid @ RequestBody UpdateEventRequest request
107106 ) {
108- String userId = getUserId (authentication );
109- EventResponse event = calendarService .updateEvent (id , userId , request );
107+ EventResponse event = calendarService .updateEvent (id , user .getId (), request );
110108 return ResponseEntity .ok (ApiResponse .success ("Event updated successfully" , event ));
111109 }
112110
113111 @ Operation (summary = "Delete event" , description = "Delete a calendar event" )
114112 @ DeleteMapping ("/{id}" )
115113 public ResponseEntity <ApiResponse <Void >> deleteEvent (
116114 @ PathVariable String id ,
117- Authentication authentication
115+ @ AuthenticationPrincipal UserPrincipal user
118116 ) {
119- String userId = getUserId (authentication );
120- calendarService .deleteEvent (id , userId );
117+ calendarService .deleteEvent (id , user .getId ());
121118 return ResponseEntity .ok (ApiResponse .success ("Event deleted successfully" , null ));
122119 }
123120
124121 @ Operation (summary = "Batch delete events" , description = "Delete multiple calendar events" )
125122 @ PostMapping ("/batch-delete" )
126123 public ResponseEntity <ApiResponse <Void >> batchDeleteEvents (
127- Authentication authentication ,
124+ @ AuthenticationPrincipal UserPrincipal user ,
128125 @ RequestBody Map <String , List <String >> body
129126 ) {
130- String userId = getUserId (authentication );
131127 List <String > ids = body .get ("ids" );
132128 if (ids == null || ids .isEmpty ()) {
133129 return ResponseEntity .badRequest ()
134130 .body (ApiResponse .error ("Event IDs are required" ));
135131 }
136- calendarService .batchDeleteEvents (ids , userId );
132+ calendarService .batchDeleteEvents (ids , user . getId () );
137133 return ResponseEntity .ok (ApiResponse .success ("Events deleted successfully" , null ));
138134 }
139135
140136 @ Operation (summary = "Reschedule event" , description = "Reschedule a calendar event to a new time" )
141137 @ PatchMapping ("/{id}/reschedule" )
142138 public ResponseEntity <ApiResponse <EventResponse >> rescheduleEvent (
143139 @ PathVariable String id ,
144- Authentication authentication ,
140+ @ AuthenticationPrincipal UserPrincipal user ,
145141 @ RequestBody Map <String , Instant > body
146142 ) {
147- String userId = getUserId (authentication );
148143 Instant newStart = body .get ("start" );
149144 Instant newEnd = body .get ("end" );
150145
@@ -153,26 +148,25 @@ public ResponseEntity<ApiResponse<EventResponse>> rescheduleEvent(
153148 .body (ApiResponse .error ("Both start and end times are required" ));
154149 }
155150
156- EventResponse event = calendarService .rescheduleEvent (id , userId , newStart , newEnd );
151+ EventResponse event = calendarService .rescheduleEvent (id , user . getId () , newStart , newEnd );
157152 return ResponseEntity .ok (ApiResponse .success ("Event rescheduled successfully" , event ));
158153 }
159154
160155 @ Operation (summary = "Add attendees" , description = "Add attendees to a calendar event" )
161156 @ PostMapping ("/{id}/attendees" )
162157 public ResponseEntity <ApiResponse <EventResponse >> addAttendees (
163158 @ PathVariable String id ,
164- Authentication authentication ,
159+ @ AuthenticationPrincipal UserPrincipal user ,
165160 @ RequestBody Map <String , List <String >> body
166161 ) {
167- String userId = getUserId (authentication );
168162 List <String > attendeeIds = body .get ("attendeeIds" );
169163
170164 if (attendeeIds == null || attendeeIds .isEmpty ()) {
171165 return ResponseEntity .badRequest ()
172166 .body (ApiResponse .error ("Attendee IDs are required" ));
173167 }
174168
175- EventResponse event = calendarService .addAttendees (id , userId , attendeeIds );
169+ EventResponse event = calendarService .addAttendees (id , user . getId () , attendeeIds );
176170 return ResponseEntity .ok (ApiResponse .success ("Attendees added successfully" , event ));
177171 }
178172
@@ -181,10 +175,9 @@ public ResponseEntity<ApiResponse<EventResponse>> addAttendees(
181175 public ResponseEntity <ApiResponse <EventResponse >> removeAttendee (
182176 @ PathVariable String id ,
183177 @ PathVariable String attendeeId ,
184- Authentication authentication
178+ @ AuthenticationPrincipal UserPrincipal user
185179 ) {
186- String userId = getUserId (authentication );
187- EventResponse event = calendarService .removeAttendee (id , userId , attendeeId );
180+ EventResponse event = calendarService .removeAttendee (id , user .getId (), attendeeId );
188181 return ResponseEntity .ok (ApiResponse .success ("Attendee removed successfully" , event ));
189182 }
190183
@@ -198,25 +191,4 @@ public ResponseEntity<ApiResponse<EventResponse>> updateAttendeeStatus(
198191 EventResponse event = calendarService .updateAttendeeStatus (id , userId , request .getStatus ());
199192 return ResponseEntity .ok (ApiResponse .success ("Attendee status updated successfully" , event ));
200193 }
201-
202- /**
203- * Extract user ID from authentication.
204- * This assumes the authentication principal contains user information.
205- * Adjust based on your actual security configuration.
206- */
207- private String getUserId (Authentication authentication ) {
208- // For now, we'll use the username as ID
209- // TODO: Update this based on your actual UserPrincipal implementation
210- // If UserPrincipal has been updated to use String id, use:
211- // return ((UserPrincipal) authentication.getPrincipal()).getId();
212-
213- // Temporary solution - assumes username is the user ID or can be resolved
214- Object principal = authentication .getPrincipal ();
215- if (principal instanceof String ) {
216- return (String ) principal ;
217- }
218- // If you have a custom UserPrincipal, cast and get ID
219- // For now, using authentication name as fallback
220- return authentication .getName ();
221- }
222194}
0 commit comments