File tree 3 files changed +35
-1
lines changed
backend/src/main/java/com/group1/cuisines
3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -98,5 +98,19 @@ public ResponseEntity<?> getBookmarks(@PathVariable Integer recipeId) {
98
98
return ResponseEntity .ok ().body (whoBookmarked );
99
99
}
100
100
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
+ }
101
115
102
116
}
Original file line number Diff line number Diff line change 4
4
import org .springframework .data .jpa .repository .JpaRepository ;
5
5
import org .springframework .stereotype .Repository ;
6
6
7
+ import java .util .Optional ;
8
+
7
9
@ 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 );
9
12
}
Original file line number Diff line number Diff line change @@ -29,6 +29,8 @@ public class RecipeService {
29
29
private RatingRepository ratingRepository ;
30
30
@ Autowired
31
31
private BookmarkRepository bookmarkRepository ;
32
+ @ Autowired
33
+ private UpvoteRepository upvoteRepository ;
32
34
33
35
@ Transactional
34
36
public RecipeDetailDto createRecipe (NewRecipeDto newRecipe , String username ) throws Exception {
@@ -145,4 +147,19 @@ public List<User> getWhoBookmarked(Integer recipeId) {
145
147
return bookmarkRepository .findByRecipeId (recipeId ).stream ().map (Bookmark ::getUser ).toList ();
146
148
}
147
149
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
+
148
165
}
You can’t perform that action at this time.
0 commit comments