Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend/feature/174 add comment #217

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

@Repository
public interface CommentRepository extends JpaRepository<Comment, Integer> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -145,4 +147,26 @@ public List<User> 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;
}

}