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..2c086a6d 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,23 @@ public ResponseEntity getBookmarks(@PathVariable Integer recipeId) { } + + @PostMapping("/recipes/{recipeId}/comments") + 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); + } 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/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; + } + }