diff --git a/.github/workflows/frontend_test.yml b/.github/workflows/frontend_test.yml new file mode 100644 index 00000000..b57d72a0 --- /dev/null +++ b/.github/workflows/frontend_test.yml @@ -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 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..71804121 100644 --- a/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java +++ b/backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java @@ -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; @@ -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; @@ -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 = recipeService.getCommentsByRecipeId(recipeId); + if (commentsDto.isEmpty()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No comments found for this recipe."); + } + return ResponseEntity.ok(commentsDto); + } + } diff --git a/backend/src/main/java/com/group1/cuisines/dto/CommentsDto.java b/backend/src/main/java/com/group1/cuisines/dto/CommentsDto.java new file mode 100644 index 00000000..7a532538 --- /dev/null +++ b/backend/src/main/java/com/group1/cuisines/dto/CommentsDto.java @@ -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; +} 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..2e419955 100644 --- a/backend/src/main/java/com/group1/cuisines/repositories/CommentRepository.java +++ b/backend/src/main/java/com/group1/cuisines/repositories/CommentRepository.java @@ -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 { + // Method to find all comments for a given recipe by its ID + List findByRecipeId(Integer recipeId); -} +} \ No newline at end of file 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..cc811ffe 100644 --- a/backend/src/main/java/com/group1/cuisines/services/RecipeService.java +++ b/backend/src/main/java/com/group1/cuisines/services/RecipeService.java @@ -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; @@ -13,6 +14,7 @@ import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; @Service public class RecipeService { @@ -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 = userRepository.findByUsername(username); @@ -145,4 +150,16 @@ public List getWhoBookmarked(Integer recipeId) { return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList(); } + public List 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()); + } }