Skip to content

Commit 7b58925

Browse files
committed
Add Delete Upvote Endpoint
1 parent 039fec5 commit 7b58925

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,19 @@ public ResponseEntity<?> getBookmarks(@PathVariable Integer recipeId) {
9898
return ResponseEntity.ok().body(whoBookmarked);
9999
}
100100

101+
@DeleteMapping("/recipes/{recipeId}/comments/{commentId}/upvote")
102+
public ResponseEntity<?> deleteUpvote(@PathVariable Integer commentId) {
103+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
104+
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
105+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
106+
}
107+
String username = authentication.getName();
108+
boolean success = recipeService.deleteUpvote(commentId, username);
109+
if (success) {
110+
return ResponseEntity.ok().body("Upvote removed successfully.");
111+
} else {
112+
return ResponseEntity.badRequest().body("Failed to remove upvote.");
113+
}
114+
}
101115

102116
}

backend/src/main/java/com/group1/cuisines/repositories/UpvoteRepository.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
66

7+
import java.util.Optional;
8+
79
@Repository
8-
public interface UpvoteRepository extends JpaRepository<Upvote,Integer> {
10+
public interface UpvoteRepository extends JpaRepository<Upvote, Integer> {
11+
Optional<Upvote> findByCommentIdAndUserId(Integer commentId, Integer userId);
912
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public class RecipeService {
2929
private RatingRepository ratingRepository;
3030
@Autowired
3131
private BookmarkRepository bookmarkRepository;
32+
@Autowired
33+
private UpvoteRepository upvoteRepository;
3234

3335
@Transactional
3436
public RecipeDetailDto createRecipe(NewRecipeDto newRecipe, String username) throws Exception {
@@ -145,4 +147,19 @@ public List<User> getWhoBookmarked(Integer recipeId) {
145147
return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList();
146148
}
147149

150+
@Transactional
151+
public boolean deleteUpvote(Integer commentId, String username) {
152+
User user = userRepository.findByUsername(username).orElse(null);
153+
if (user == null) {
154+
return false;
155+
}
156+
Optional<Upvote> upvote = upvoteRepository.findByCommentIdAndUserId(commentId, user.getId());
157+
if (upvote.isPresent()) {
158+
upvoteRepository.delete(upvote.get());
159+
return true;
160+
}
161+
return false;
162+
}
163+
164+
148165
}

0 commit comments

Comments
 (0)