1
1
package com .group1 .cuisines .services ;
2
2
3
+ import com .group1 .cuisines .dto .CommentsDto ;
3
4
import com .group1 .cuisines .dto .IngredientsDto ;
4
5
import com .group1 .cuisines .dto .NewRecipeDto ;
5
6
import com .group1 .cuisines .dto .RecipeDetailDto ;
13
14
14
15
import java .util .List ;
15
16
import java .util .Optional ;
17
+ import java .util .stream .Collectors ;
16
18
17
19
@ Service
18
20
public class RecipeService {
@@ -30,6 +32,9 @@ public class RecipeService {
30
32
@ Autowired
31
33
private BookmarkRepository bookmarkRepository ;
32
34
35
+ @ Autowired
36
+ private CommentRepository commentRepository ;
37
+
33
38
@ Transactional
34
39
public RecipeDetailDto createRecipe (NewRecipeDto newRecipe , String username ) throws Exception {
35
40
Optional <User > user = userRepository .findByUsername (username );
@@ -145,4 +150,16 @@ public List<User> getWhoBookmarked(Integer recipeId) {
145
150
return bookmarkRepository .findByRecipeId (recipeId ).stream ().map (Bookmark ::getUser ).toList ();
146
151
}
147
152
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
+ }
148
165
}
0 commit comments