Skip to content

Commit

Permalink
feat(backend): implement delete bookmark endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
atakanyasar committed May 15, 2024
1 parent 98d7dff commit 2e8bb37
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ public ResponseEntity<?> getBookmarks(@PathVariable Integer recipeId) {
return ResponseEntity.ok(new SuccessResponse<>(200, whoBookmarked, "Bookmarks fetched successfully"));
}

@DeleteMapping("/recipes/{recipeId}/bookmarks")
public ResponseEntity<?> deleteBookmark(@PathVariable Integer recipeId) {
String username = authenticationService.getUser().map(User::getUsername).orElse(null);
if (username == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorResponse(401, "Authentication required"));
}

boolean success = recipeService.deleteBookmark(recipeId, username);

if (success) {
return ResponseEntity.ok(new SuccessResponse<>(200, "", "Bookmark deleted successfully"));
}
return ResponseEntity.ok(new ErrorResponse(400, "Failed to delete bookmark"));
}

@GetMapping("/recipes/{recipeId}/comments")
public ResponseEntity<?> getComments(@PathVariable Integer recipeId) {
List<CommentsDto> commentsDto = recipeService.getCommentsByRecipeId(recipeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ public List<User> getWhoBookmarked(Integer recipeId) {
return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList();
}

public boolean deleteBookmark(Integer recipeId, String username) {
User user = userRepository.findByUsername(username).orElse(null);
if (user == null){
return false;
}
Recipe recipe = recipeRepository.findById(recipeId).orElse(null);
if (recipe == null){
return false;
}
Optional<Bookmark> bookmark = bookmarkRepository.findByUserIdAndRecipeId(user.getId(), recipeId);
if (bookmark.isEmpty()){
return false;
}
bookmarkRepository.delete(bookmark.get());
return true;
}


public List<CommentsDto> getCommentsByRecipeId(Integer recipeId) {
return commentRepository.findByRecipeId(recipeId).stream()
Expand Down

0 comments on commit 2e8bb37

Please sign in to comment.