Skip to content

Commit

Permalink
Merge pull request #188 from bounswe/backend/feature/175-get-comments…
Browse files Browse the repository at this point in the history
…-endpoint

Backend/feature/175 get comments endpoint
  • Loading branch information
boraykasap authored May 12, 2024
2 parents 039fec5 + 50c5722 commit 32ec451
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .github/workflows/frontend_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Front-End CI

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: frontend

steps:
- uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20'
cache: 'yarn'

- name: Enable Corepack
run: corepack enable

- name: Install dependencies
run: yarn install --immutable

- name: Run formatter
run: yarn format

- name: Run linter
run: yarn lint

- name: Run tests
run: yarn test
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.group1.cuisines.controllers;
import com.group1.cuisines.dto.CommentsDto;
import com.group1.cuisines.dto.NewRecipeDto;
import com.group1.cuisines.dto.RatingDto;
import com.group1.cuisines.dto.RecipeDetailDto;
import com.group1.cuisines.entities.Comment;
import com.group1.cuisines.entities.User;
import com.group1.cuisines.services.RecipeService;
import org.springframework.http.HttpStatus;
Expand All @@ -11,6 +13,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import java.util.List;

import java.util.List;

Expand Down Expand Up @@ -98,5 +101,14 @@ public ResponseEntity<?> getBookmarks(@PathVariable Integer recipeId) {
return ResponseEntity.ok().body(whoBookmarked);
}

@GetMapping("/recipes/{recipeId}/comments")
public ResponseEntity<?> getComments(@PathVariable Integer recipeId) {
List<CommentsDto> commentsDto = recipeService.getCommentsByRecipeId(recipeId);
if (commentsDto.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No comments found for this recipe.");
}
return ResponseEntity.ok(commentsDto);
}


}
19 changes: 19 additions & 0 deletions backend/src/main/java/com/group1/cuisines/dto/CommentsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.group1.cuisines.dto;

import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CommentsDto {
private Integer id;
private Integer userId;
private Integer recipeId;
private String text;
private LocalDateTime createdDate;
private int upvoteCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import com.group1.cuisines.entities.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;



@Repository
public interface CommentRepository extends JpaRepository<Comment, Integer> {
// Method to find all comments for a given recipe by its ID
List<Comment> findByRecipeId(Integer recipeId);

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.group1.cuisines.services;

import com.group1.cuisines.dto.CommentsDto;
import com.group1.cuisines.dto.IngredientsDto;
import com.group1.cuisines.dto.NewRecipeDto;
import com.group1.cuisines.dto.RecipeDetailDto;
Expand All @@ -13,6 +14,7 @@

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class RecipeService {
Expand All @@ -30,6 +32,9 @@ public class RecipeService {
@Autowired
private BookmarkRepository bookmarkRepository;

@Autowired
private CommentRepository commentRepository;

@Transactional
public RecipeDetailDto createRecipe(NewRecipeDto newRecipe, String username) throws Exception {
Optional<User> user = userRepository.findByUsername(username);
Expand Down Expand Up @@ -145,4 +150,16 @@ public List<User> getWhoBookmarked(Integer recipeId) {
return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList();
}

public List<CommentsDto> getCommentsByRecipeId(Integer recipeId) {
return commentRepository.findByRecipeId(recipeId).stream()
.map(comment -> CommentsDto.builder()
.id(comment.getId())
.userId(comment.getUser().getId())
.recipeId(comment.getRecipe().getId())
.text(comment.getText())
.createdDate(comment.getCreatedDate())
.upvoteCount(comment.getUpvotes() != null ? comment.getUpvotes().size() : 0)
.build())
.collect(Collectors.toList());
}
}

0 comments on commit 32ec451

Please sign in to comment.