From f5bfef9b412f506f827581fab040005c65b17456 Mon Sep 17 00:00:00 2001 From: pocemucka Date: Sat, 11 May 2024 19:56:56 +0300 Subject: [PATCH 1/3] Add a comment feature to the recipe page is added --- .../controllers/RecipeController.java | 18 ++++++++++++++ .../repositories/CommentRepository.java | 1 - .../repositories/RecipeRepository.java | 2 ++ .../cuisines/services/RecipeService.java | 24 +++++++++++++++++++ 4 files changed, 44 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..9202f72c 100644 --- a/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java +++ b/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java @@ -2,6 +2,7 @@ import com.group1.cuisines.dto.NewRecipeDto; import com.group1.cuisines.dto.RatingDto; import com.group1.cuisines.dto.RecipeDetailDto; +import com.group1.cuisines.entities.Recipe; import com.group1.cuisines.entities.User; import com.group1.cuisines.services.RecipeService; import org.springframework.http.HttpStatus; @@ -99,4 +100,21 @@ public ResponseEntity getBookmarks(@PathVariable Integer recipeId) { } + + @PostMapping("/recipes/{recipeId}/comments") + public ResponseEntity addComment(@PathVariable Integer recipeId, @RequestBody String username, @RequestBody String comment) { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required."); + } + + boolean savedComment = recipeService.addComment(recipeId, username, comment); + if (savedComment != false) { + return ResponseEntity.status(HttpStatus.CREATED).body(savedComment); + } else { + return ResponseEntity.badRequest().body("Failed to add comment."); + } + } + + } diff --git a/backend/src/main/java/com/group1/cuisines/repositories/CommentRepository.java b/backend/src/main/java/com/group1/cuisines/repositories/CommentRepository.java index 33a3b141..ae176a12 100644 --- a/backend/src/main/java/com/group1/cuisines/repositories/CommentRepository.java +++ b/backend/src/main/java/com/group1/cuisines/repositories/CommentRepository.java @@ -6,5 +6,4 @@ @Repository public interface CommentRepository extends JpaRepository { - } diff --git a/backend/src/main/java/com/group1/cuisines/repositories/RecipeRepository.java b/backend/src/main/java/com/group1/cuisines/repositories/RecipeRepository.java index 9d027dd9..543b4b4d 100644 --- a/backend/src/main/java/com/group1/cuisines/repositories/RecipeRepository.java +++ b/backend/src/main/java/com/group1/cuisines/repositories/RecipeRepository.java @@ -9,4 +9,6 @@ public interface RecipeRepository extends JpaRepository { @Query("SELECT AVG(r.ratingValue) FROM Rating r WHERE r.recipe.id = :recipeId") Double findAverageByRecipeId(Integer recipeId); + // Method to add a comment to a recipe by a user with the given username and comment and return a boolean value to indicate success + boolean addComment(Integer recipeId, String username, String comment); } 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..46f5ce5e 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 CommentRepository commentRepository; @Transactional public RecipeDetailDto createRecipe(NewRecipeDto newRecipe, String username) throws Exception { @@ -145,4 +147,26 @@ public List getWhoBookmarked(Integer recipeId) { return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList(); } + public boolean addComment(Integer recipeId, String username, String comment) { + User user = userRepository.findByUsername(username).orElse(null); + if (user == null){ + return false; + } + Recipe recipe = recipeRepository.findById(recipeId).orElse(null); + if (recipe == null){ + return false; + } + if(comment.length() > 1024 || comment.length() < 1) { + return false; + } + Comment newComment = Comment.builder() + .user(user) + .recipe(recipe) + .text(comment) + .build(); + commentRepository.save(newComment); + + return true; + } + } From 9da08fc838c4aaa0191ebfb279bb34d10cba2f07 Mon Sep 17 00:00:00 2001 From: pocemucka Date: Tue, 14 May 2024 08:50:37 +0300 Subject: [PATCH 2/3] Fixed version v.1 --- .../com/group1/cuisines/controllers/RecipeController.java | 4 +++- 1 file changed, 3 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 9202f72c..2c086a6d 100644 --- a/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java +++ b/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java @@ -102,12 +102,14 @@ public ResponseEntity getBookmarks(@PathVariable Integer recipeId) { @PostMapping("/recipes/{recipeId}/comments") - public ResponseEntity addComment(@PathVariable Integer recipeId, @RequestBody String username, @RequestBody String comment) { + public ResponseEntity addComment(@PathVariable Integer recipeId, @RequestBody String comment) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required."); } + String username = authentication.getName(); // Assuming the username can be obtained like this + boolean savedComment = recipeService.addComment(recipeId, username, comment); if (savedComment != false) { return ResponseEntity.status(HttpStatus.CREATED).body(savedComment); From 723c7c79dcaaf8b0b49b52cd61a1cd7941f5c390 Mon Sep 17 00:00:00 2001 From: pocemucka Date: Tue, 14 May 2024 14:37:52 +0300 Subject: [PATCH 3/3] edited 1 --- .../java/com/group1/cuisines/repositories/RecipeRepository.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/src/main/java/com/group1/cuisines/repositories/RecipeRepository.java b/backend/src/main/java/com/group1/cuisines/repositories/RecipeRepository.java index 543b4b4d..9d027dd9 100644 --- a/backend/src/main/java/com/group1/cuisines/repositories/RecipeRepository.java +++ b/backend/src/main/java/com/group1/cuisines/repositories/RecipeRepository.java @@ -9,6 +9,4 @@ public interface RecipeRepository extends JpaRepository { @Query("SELECT AVG(r.ratingValue) FROM Rating r WHERE r.recipe.id = :recipeId") Double findAverageByRecipeId(Integer recipeId); - // Method to add a comment to a recipe by a user with the given username and comment and return a boolean value to indicate success - boolean addComment(Integer recipeId, String username, String comment); }