Skip to content

Commit 32ec451

Browse files
authored
Merge pull request #188 from bounswe/backend/feature/175-get-comments-endpoint
Backend/feature/175 get comments endpoint
2 parents 039fec5 + 50c5722 commit 32ec451

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

.github/workflows/frontend_test.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Front-End CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
defaults:
9+
run:
10+
working-directory: frontend
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
15+
- name: Set up Node.js
16+
uses: actions/setup-node@v2
17+
with:
18+
node-version: '20'
19+
cache: 'yarn'
20+
21+
- name: Enable Corepack
22+
run: corepack enable
23+
24+
- name: Install dependencies
25+
run: yarn install --immutable
26+
27+
- name: Run formatter
28+
run: yarn format
29+
30+
- name: Run linter
31+
run: yarn lint
32+
33+
- name: Run tests
34+
run: yarn test

backend/src/main/java/com/group1/cuisines/controllers/RecipeController.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.group1.cuisines.controllers;
2+
import com.group1.cuisines.dto.CommentsDto;
23
import com.group1.cuisines.dto.NewRecipeDto;
34
import com.group1.cuisines.dto.RatingDto;
45
import com.group1.cuisines.dto.RecipeDetailDto;
6+
import com.group1.cuisines.entities.Comment;
57
import com.group1.cuisines.entities.User;
68
import com.group1.cuisines.services.RecipeService;
79
import org.springframework.http.HttpStatus;
@@ -11,6 +13,7 @@
1113
import org.springframework.beans.factory.annotation.Autowired;
1214
import org.springframework.http.ResponseEntity;
1315
import org.springframework.security.access.prepost.PreAuthorize;
16+
import java.util.List;
1417

1518
import java.util.List;
1619

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

104+
@GetMapping("/recipes/{recipeId}/comments")
105+
public ResponseEntity<?> getComments(@PathVariable Integer recipeId) {
106+
List<CommentsDto> commentsDto = recipeService.getCommentsByRecipeId(recipeId);
107+
if (commentsDto.isEmpty()) {
108+
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No comments found for this recipe.");
109+
}
110+
return ResponseEntity.ok(commentsDto);
111+
}
112+
101113

102114
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.group1.cuisines.dto;
2+
3+
import lombok.*;
4+
5+
import java.time.LocalDateTime;
6+
7+
@Getter
8+
@Setter
9+
@Builder
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
public class CommentsDto {
13+
private Integer id;
14+
private Integer userId;
15+
private Integer recipeId;
16+
private String text;
17+
private LocalDateTime createdDate;
18+
private int upvoteCount;
19+
}

backend/src/main/java/com/group1/cuisines/repositories/CommentRepository.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
import com.group1.cuisines.entities.Comment;
44
import org.springframework.data.jpa.repository.JpaRepository;
55
import org.springframework.stereotype.Repository;
6+
import java.util.List;
7+
8+
69

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

10-
}
15+
}

backend/src/main/java/com/group1/cuisines/services/RecipeService.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.group1.cuisines.services;
22

3+
import com.group1.cuisines.dto.CommentsDto;
34
import com.group1.cuisines.dto.IngredientsDto;
45
import com.group1.cuisines.dto.NewRecipeDto;
56
import com.group1.cuisines.dto.RecipeDetailDto;
@@ -13,6 +14,7 @@
1314

1415
import java.util.List;
1516
import java.util.Optional;
17+
import java.util.stream.Collectors;
1618

1719
@Service
1820
public class RecipeService {
@@ -30,6 +32,9 @@ public class RecipeService {
3032
@Autowired
3133
private BookmarkRepository bookmarkRepository;
3234

35+
@Autowired
36+
private CommentRepository commentRepository;
37+
3338
@Transactional
3439
public RecipeDetailDto createRecipe(NewRecipeDto newRecipe, String username) throws Exception {
3540
Optional<User> user = userRepository.findByUsername(username);
@@ -145,4 +150,16 @@ public List<User> getWhoBookmarked(Integer recipeId) {
145150
return bookmarkRepository.findByRecipeId(recipeId).stream().map(Bookmark::getUser).toList();
146151
}
147152

153+
public List<CommentsDto> getCommentsByRecipeId(Integer recipeId) {
154+
return commentRepository.findByRecipeId(recipeId).stream()
155+
.map(comment -> CommentsDto.builder()
156+
.id(comment.getId())
157+
.userId(comment.getUser().getId())
158+
.recipeId(comment.getRecipe().getId())
159+
.text(comment.getText())
160+
.createdDate(comment.getCreatedDate())
161+
.upvoteCount(comment.getUpvotes() != null ? comment.getUpvotes().size() : 0)
162+
.build())
163+
.collect(Collectors.toList());
164+
}
148165
}

0 commit comments

Comments
 (0)