|
3 | 3 | import com.group1.cuisines.dto.RatingDto;
|
4 | 4 | import com.group1.cuisines.dto.RecipeDetailDto;
|
5 | 5 | import com.group1.cuisines.entities.Comment;
|
| 6 | +import com.group1.cuisines.entities.User; |
6 | 7 | import com.group1.cuisines.services.RecipeService;
|
7 | 8 | import org.springframework.http.HttpStatus;
|
8 | 9 | import org.springframework.security.core.Authentication;
|
|
13 | 14 | import org.springframework.security.access.prepost.PreAuthorize;
|
14 | 15 | import java.util.List;
|
15 | 16 |
|
| 17 | +import java.util.List; |
| 18 | + |
16 | 19 | @RestController
|
17 | 20 | @RequestMapping("/api/v1")
|
18 | 21 | public class RecipeController {
|
@@ -69,6 +72,34 @@ public ResponseEntity<?> rateRecipe(@PathVariable Integer recipeId, @RequestBody
|
69 | 72 | }
|
70 | 73 | }
|
71 | 74 |
|
| 75 | + @PostMapping("/recipes/{recipeId}/bookmarks") |
| 76 | + public ResponseEntity<?> bookmarkRecipe(@PathVariable Integer recipeId) { |
| 77 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 78 | + if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) { |
| 79 | + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required."); |
| 80 | + } |
| 81 | + |
| 82 | + String username = authentication.getName(); // Assuming the username can be obtained like this |
| 83 | + boolean success = recipeService.bookmarkRecipe(recipeId, username); |
| 84 | + |
| 85 | + if (success) { |
| 86 | + return ResponseEntity.ok().body("Recipe bookmarked successfully"); |
| 87 | + } else { |
| 88 | + return ResponseEntity.badRequest().body("Failed to bookmark recipe"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @GetMapping("/recipes/{recipeId}/bookmarks") |
| 93 | + public ResponseEntity<?> getBookmarks(@PathVariable Integer recipeId) { |
| 94 | + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| 95 | + if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) { |
| 96 | + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required."); |
| 97 | + } |
| 98 | + |
| 99 | + List<User> whoBookmarked = recipeService.getWhoBookmarked(recipeId); |
| 100 | + return ResponseEntity.ok().body(whoBookmarked); |
| 101 | + } |
| 102 | + |
72 | 103 | @GetMapping("/recipes/{recipeId}/comments")
|
73 | 104 | public ResponseEntity<?> getComments(@PathVariable Integer recipeId) {
|
74 | 105 |
|
|
0 commit comments