11package net .hackyourfuture .coursehub .web ;
22
33import jakarta .validation .constraints .Positive ;
4+ import net .hackyourfuture .coursehub .data .AuthenticatedUser ;
45import net .hackyourfuture .coursehub .service .CourseService ;
56import net .hackyourfuture .coursehub .web .model .CourseListResponse ;
7+ import org .springframework .http .HttpStatus ;
8+ import org .springframework .http .HttpStatusCode ;
9+ import org .springframework .security .core .annotation .AuthenticationPrincipal ;
610import org .springframework .web .bind .annotation .GetMapping ;
711import org .springframework .web .bind .annotation .PathVariable ;
812import org .springframework .web .bind .annotation .RequestMapping ;
913import org .springframework .web .bind .annotation .RestController ;
14+ import org .springframework .web .client .HttpClientErrorException ;
15+ import org .springframework .web .client .HttpServerErrorException ;
16+ import org .springframework .web .client .HttpStatusCodeException ;
1017
1118@ RestController
1219@ RequestMapping ("/students" )
@@ -19,7 +26,11 @@ public StudentController(CourseService courseService) {
1926 }
2027
2128 @ GetMapping ("/{studentId}/courses" )
22- public CourseListResponse getCoursesForStudent (@ PathVariable @ Positive Integer studentId ) {
29+ public CourseListResponse getCoursesForStudent (
30+ @ PathVariable @ Positive Integer studentId , @ AuthenticationPrincipal AuthenticatedUser user ) {
31+ if (!user .getUserId ().equals (studentId )) {
32+ throw new HttpClientErrorException (HttpStatus .FORBIDDEN );
33+ }
2334 var courses = courseService .getCoursesForStudent (studentId );
2435 return new CourseListResponse (courses );
2536 }
0 commit comments