From ddb34da0fdff0c17682cce6dc3a28d5032a3620c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atakan=20Ya=C5=9Far?= Date: Fri, 10 May 2024 23:47:21 +0300 Subject: [PATCH] get who bookmarked a recipe --- .../cuisines/controllers/RecipeController.java | 15 +++++++++++++++ .../group1/cuisines/services/RecipeService.java | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java b/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java index 62604db7..cabcc6ae 100644 --- a/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java +++ b/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java @@ -2,6 +2,7 @@ import com.group1.cuisines.dto.NewRecipeDto; import com.group1.cuisines.dto.RatingDto; import com.group1.cuisines.dto.RecipeDetailDto; +import com.group1.cuisines.entities.User; import com.group1.cuisines.services.RecipeService; import org.springframework.http.HttpStatus; import org.springframework.security.core.Authentication; @@ -11,6 +12,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; +import java.util.List; + @RestController @RequestMapping("/api/v1") public class RecipeController { @@ -84,4 +87,16 @@ public ResponseEntity bookmarkRecipe(@PathVariable Integer recipeId) { } } + @GetMapping("/recipes/{recipeId}/bookmarks") + public ResponseEntity getBookmarks(@PathVariable Integer recipeId) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required."); + } + + List whoBookmarked = recipeService.getWhoBookmarked(recipeId); + return ResponseEntity.ok().body(whoBookmarked); + } + + } diff --git a/backend/src/main/java/com/group1/cuisines/services/RecipeService.java b/backend/src/main/java/com/group1/cuisines/services/RecipeService.java index 8dd60e64..8079503a 100644 --- a/backend/src/main/java/com/group1/cuisines/services/RecipeService.java +++ b/backend/src/main/java/com/group1/cuisines/services/RecipeService.java @@ -11,6 +11,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; import java.util.Optional; @Service @@ -140,4 +141,8 @@ public boolean bookmarkRecipe(Integer recipeId, String username) { return true; } + public List getWhoBookmarked(Integer recipeId) { + return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList(); + } + }