Skip to content

Commit 75f562a

Browse files
authored
Merge pull request #187 from bounswe/backend/feature/178-get-bookmark
GET /recipes/{recipeId}/bookmarks endpoint implemented
2 parents ef001ed + ddb34da commit 75f562a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import com.group1.cuisines.dto.NewRecipeDto;
33
import com.group1.cuisines.dto.RatingDto;
44
import com.group1.cuisines.dto.RecipeDetailDto;
5+
import com.group1.cuisines.entities.User;
56
import com.group1.cuisines.services.RecipeService;
67
import org.springframework.http.HttpStatus;
78
import org.springframework.security.core.Authentication;
@@ -11,6 +12,8 @@
1112
import org.springframework.http.ResponseEntity;
1213
import org.springframework.security.access.prepost.PreAuthorize;
1314

15+
import java.util.List;
16+
1417
@RestController
1518
@RequestMapping("/api/v1")
1619
public class RecipeController {
@@ -84,4 +87,16 @@ public ResponseEntity<?> bookmarkRecipe(@PathVariable Integer recipeId) {
8487
}
8588
}
8689

90+
@GetMapping("/recipes/{recipeId}/bookmarks")
91+
public ResponseEntity<?> getBookmarks(@PathVariable Integer recipeId) {
92+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
93+
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
94+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
95+
}
96+
97+
List<User> whoBookmarked = recipeService.getWhoBookmarked(recipeId);
98+
return ResponseEntity.ok().body(whoBookmarked);
99+
}
100+
101+
87102
}

backend/src/main/java/com/group1/cuisines/services/RecipeService.java

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.stereotype.Service;
1212
import org.springframework.transaction.annotation.Transactional;
1313

14+
import java.util.List;
1415
import java.util.Optional;
1516

1617
@Service
@@ -140,4 +141,8 @@ public boolean bookmarkRecipe(Integer recipeId, String username) {
140141
return true;
141142
}
142143

144+
public List<User> getWhoBookmarked(Integer recipeId) {
145+
return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList();
146+
}
147+
143148
}

0 commit comments

Comments
 (0)