11package com .thughari .jobtrackerpro .controller ;
22
33import com .thughari .jobtrackerpro .dto .CareerResourceDTO ;
4+ import com .thughari .jobtrackerpro .dto .CareerResourcePageResponse ;
45import com .thughari .jobtrackerpro .dto .CreateCareerResourceRequest ;
6+ import com .thughari .jobtrackerpro .dto .UpdateCareerResourceRequest ;
57import com .thughari .jobtrackerpro .service .CareerResourceService ;
8+ import org .springframework .http .MediaType ;
69import org .springframework .http .ResponseEntity ;
10+ import org .springframework .security .authentication .AnonymousAuthenticationToken ;
11+ import org .springframework .security .core .Authentication ;
712import org .springframework .security .core .context .SecurityContextHolder ;
13+ import org .springframework .web .bind .annotation .DeleteMapping ;
814import org .springframework .web .bind .annotation .GetMapping ;
15+ import org .springframework .web .bind .annotation .PathVariable ;
916import org .springframework .web .bind .annotation .PostMapping ;
17+ import org .springframework .web .bind .annotation .PutMapping ;
1018import org .springframework .web .bind .annotation .RequestBody ;
1119import org .springframework .web .bind .annotation .RequestMapping ;
20+ import org .springframework .web .bind .annotation .RequestParam ;
1221import org .springframework .web .bind .annotation .RestController ;
22+ import org .springframework .web .multipart .MultipartFile ;
1323
24+ import java .util .UUID ;
1425import java .util .List ;
1526
1627@ RestController
@@ -24,8 +35,20 @@ public CareerResourceController(CareerResourceService careerResourceService) {
2435 }
2536
2637 @ GetMapping
27- public ResponseEntity <List <CareerResourceDTO >> getResources () {
28- return ResponseEntity .ok (careerResourceService .getAllResources ());
38+ public ResponseEntity <CareerResourcePageResponse > getResources (
39+ @ RequestParam (defaultValue = "0" ) int page ,
40+ @ RequestParam (defaultValue = "20" ) int size ,
41+ @ RequestParam (required = false ) String query ,
42+ @ RequestParam (required = false ) String category ,
43+ @ RequestParam (required = false ) String type
44+ ) {
45+ return ResponseEntity .ok (careerResourceService .getResourcePage (page , size , query , category , type , getAuthenticatedEmailOrNull ()));
46+ }
47+
48+
49+ @ GetMapping ("/categories" )
50+ public ResponseEntity <List <String >> getCategories () {
51+ return ResponseEntity .ok (careerResourceService .getAllCategories ());
2952 }
3053
3154 @ PostMapping
@@ -34,7 +57,52 @@ public ResponseEntity<CareerResourceDTO> addResource(@RequestBody CreateCareerRe
3457 return ResponseEntity .ok (careerResourceService .createResource (email , request ));
3558 }
3659
60+ @ GetMapping ("/mine" )
61+ public ResponseEntity <List <CareerResourceDTO >> getMyResources () {
62+ String email = getAuthenticatedEmail ();
63+ return ResponseEntity .ok (careerResourceService .getMyResources (email ));
64+ }
65+
66+ @ PutMapping ("/{id}" )
67+ public ResponseEntity <CareerResourceDTO > updateResource (@ PathVariable UUID id ,
68+ @ RequestBody UpdateCareerResourceRequest request ) {
69+ String email = getAuthenticatedEmail ();
70+ return ResponseEntity .ok (careerResourceService .updateResource (email , id , request ));
71+ }
72+
73+ @ PostMapping (path = "/upload" , consumes = MediaType .MULTIPART_FORM_DATA_VALUE )
74+ public ResponseEntity <CareerResourceDTO > uploadResource (
75+ @ RequestParam String title ,
76+ @ RequestParam String category ,
77+ @ RequestParam (required = false ) String description ,
78+ @ RequestParam MultipartFile file
79+ ) {
80+ String email = getAuthenticatedEmail ();
81+ return ResponseEntity .ok (careerResourceService .createResourceFromFile (email , title , category , description , file ));
82+ }
83+
84+ @ DeleteMapping ("/{id}" )
85+ public ResponseEntity <Void > deleteResource (@ PathVariable UUID id ) {
86+ String email = getAuthenticatedEmail ();
87+ careerResourceService .deleteResource (email , id );
88+ return ResponseEntity .noContent ().build ();
89+ }
90+
3791 private String getAuthenticatedEmail () {
3892 return ((String ) SecurityContextHolder .getContext ().getAuthentication ().getPrincipal ()).toLowerCase ();
3993 }
94+
95+ private String getAuthenticatedEmailOrNull () {
96+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
97+ if (authentication == null || !authentication .isAuthenticated () || authentication instanceof AnonymousAuthenticationToken ) {
98+ return null ;
99+ }
100+
101+ Object principal = authentication .getPrincipal ();
102+ if (principal instanceof String email && !"anonymousUser" .equalsIgnoreCase (email )) {
103+ return email .toLowerCase ();
104+ }
105+
106+ return null ;
107+ }
40108}
0 commit comments