From 7b589254ee4ff13e9136d3ef0147438646bd927a Mon Sep 17 00:00:00 2001 From: CagatayColak Date: Sun, 12 May 2024 00:08:36 +0300 Subject: [PATCH] Add Delete Upvote Endpoint --- .../cuisines/controllers/RecipeController.java | 14 ++++++++++++++ .../cuisines/repositories/UpvoteRepository.java | 5 ++++- .../group1/cuisines/services/RecipeService.java | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) 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 cabcc6ae..282f9e58 100644 --- a/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java +++ b/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java @@ -98,5 +98,19 @@ public ResponseEntity getBookmarks(@PathVariable Integer recipeId) { return ResponseEntity.ok().body(whoBookmarked); } + @DeleteMapping("/recipes/{recipeId}/comments/{commentId}/upvote") + public ResponseEntity deleteUpvote(@PathVariable Integer commentId) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required."); + } + String username = authentication.getName(); + boolean success = recipeService.deleteUpvote(commentId, username); + if (success) { + return ResponseEntity.ok().body("Upvote removed successfully."); + } else { + return ResponseEntity.badRequest().body("Failed to remove upvote."); + } + } } diff --git a/backend/src/main/java/com/group1/cuisines/repositories/UpvoteRepository.java b/backend/src/main/java/com/group1/cuisines/repositories/UpvoteRepository.java index 6e1e3c1d..7411ad36 100644 --- a/backend/src/main/java/com/group1/cuisines/repositories/UpvoteRepository.java +++ b/backend/src/main/java/com/group1/cuisines/repositories/UpvoteRepository.java @@ -4,6 +4,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository -public interface UpvoteRepository extends JpaRepository { +public interface UpvoteRepository extends JpaRepository { + Optional findByCommentIdAndUserId(Integer commentId, Integer userId); } 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 8079503a..d3019538 100644 --- a/backend/src/main/java/com/group1/cuisines/services/RecipeService.java +++ b/backend/src/main/java/com/group1/cuisines/services/RecipeService.java @@ -29,6 +29,8 @@ public class RecipeService { private RatingRepository ratingRepository; @Autowired private BookmarkRepository bookmarkRepository; + @Autowired + private UpvoteRepository upvoteRepository; @Transactional public RecipeDetailDto createRecipe(NewRecipeDto newRecipe, String username) throws Exception { @@ -145,4 +147,19 @@ public List getWhoBookmarked(Integer recipeId) { return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList(); } + @Transactional + public boolean deleteUpvote(Integer commentId, String username) { + User user = userRepository.findByUsername(username).orElse(null); + if (user == null) { + return false; + } + Optional upvote = upvoteRepository.findByCommentIdAndUserId(commentId, user.getId()); + if (upvote.isPresent()) { + upvoteRepository.delete(upvote.get()); + return true; + } + return false; + } + + }