1
1
package com .manir .springbootecommercerestapi .service .Impl ;
2
2
3
3
import com .manir .springbootecommercerestapi .dto .CommentDto ;
4
+ import com .manir .springbootecommercerestapi .exception .EcommerceApiException ;
4
5
import com .manir .springbootecommercerestapi .exception .ResourceNotFoundException ;
5
6
import com .manir .springbootecommercerestapi .repository .CommentRepository ;
6
7
import com .manir .springbootecommercerestapi .repository .ProductRepository ;
7
8
import com .manir .springbootecommercerestapi .model .Comment ;
8
9
import com .manir .springbootecommercerestapi .model .Product ;
9
10
import com .manir .springbootecommercerestapi .service .CommentService ;
10
11
import org .modelmapper .ModelMapper ;
12
+ import org .springframework .http .HttpStatus ;
11
13
import org .springframework .stereotype .Service ;
12
14
13
15
import javax .annotation .Resource ;
@@ -27,8 +29,7 @@ public class CommentServiceImpl implements CommentService {
27
29
@ Override
28
30
public CommentDto createComment (Long productId , CommentDto commentDto ) {
29
31
30
- Product product = productRepository .findById (productId ).orElseThrow (()->new ResourceNotFoundException ("Product" , productId ));
31
-
32
+ Product product = findProductById (productId );
32
33
//convert to entity
33
34
Comment comment = mapToEntity (commentDto );
34
35
//save to db
@@ -43,55 +44,58 @@ public CommentDto createComment(Long productId, CommentDto commentDto) {
43
44
@ Override
44
45
public List <CommentDto > getAllComments () {
45
46
List <Comment > comments = commentRepository .findAll ();
46
- List <CommentDto > commentDtoList = comments .stream ().map (comment -> mapToDto (comment )).collect (Collectors .toList ());
47
+ List <CommentDto > commentDtoList = comments .stream ()
48
+ .map (comment -> mapToDto (comment ))
49
+ .collect (Collectors .toList ());
47
50
return commentDtoList ;
48
51
}
49
52
50
53
@ Override
51
54
public List <CommentDto > getAllCommentsByProductId (Long productId ) {
52
55
List <Comment > comments = commentRepository .findByProductId (productId );
53
- List <CommentDto > commentDtoList = comments .stream ().map (comment -> mapToDto (comment )).collect (Collectors .toList ());
56
+ List <CommentDto > commentDtoList = comments .stream ()
57
+ .map (comment -> mapToDto (comment ))
58
+ .collect (Collectors .toList ());
54
59
return commentDtoList ;
55
60
}
56
61
57
62
@ Override
58
63
public CommentDto getCommentById (Long productId , Long commentId ) {
59
- Product product = productRepository .findById (productId ).orElseThrow (()->new ResourceNotFoundException ("Product" , productId ));
60
- Comment comment = commentRepository .findById (commentId ).orElseThrow (()->new ResourceNotFoundException ("Comment" , commentId ));
61
- CommentDto responseComment = null ;
62
- if (comment .getProduct ().getId () == product .getId ()){
63
- responseComment = mapToDto (comment );
64
+
65
+ Product product = findProductById (productId );
66
+ Comment comment = findCommentById (commentId );
67
+
68
+ if (!comment .getProduct ().getId ().equals (product .getId ())){
69
+ throw new EcommerceApiException ("Comment does not belong to product" , HttpStatus .BAD_REQUEST );
64
70
}
65
71
66
- return responseComment ;
72
+ return mapToDto ( comment ) ;
67
73
}
68
74
69
75
@ Override
70
76
public CommentDto updateComment (Long productId , CommentDto commentDto , Long commentId ) {
71
- Product product = productRepository .findById (productId ).orElseThrow (()->new ResourceNotFoundException ("Product" , productId ));
72
- Comment comment = commentRepository .findById (commentId ).orElseThrow (()->new ResourceNotFoundException ("Comment" , commentId ));
73
-
74
-
75
-
76
- if (comment .getProduct ().getId () == product .getId ()){
77
- comment .setReview (commentDto .getReview ());
78
- comment .setRate (commentDto .getRate ());
79
- comment .setStatus (commentDto .getStatus ());
77
+ Product product = findProductById (productId );
78
+ Comment comment = findCommentById (commentId );
80
79
80
+ if (!comment .getProduct ().getId ().equals (product .getId ())){
81
+ throw new EcommerceApiException ("Comment does not belong to product" , HttpStatus .BAD_REQUEST );
81
82
}
82
-
83
+ comment .setReview (commentDto .getReview ());
84
+ comment .setRate (commentDto .getRate ());
85
+ comment .setStatus (commentDto .getStatus ());
83
86
Comment updatedComment = commentRepository .save (comment );
84
87
85
88
return mapToDto (updatedComment );
86
89
}
87
90
88
91
@ Override
89
92
public void deleteComment (Long productId , Long commentId ) {
90
- Product product = productRepository . findById (productId ). orElseThrow (()-> new ResourceNotFoundException ( "Product" , productId ) );
91
- Comment comment = commentRepository . findById (commentId ). orElseThrow (()-> new ResourceNotFoundException ( "Comment" , commentId ) );
92
- if (comment .getProduct ().getId () == product .getId ()){
93
- commentRepository . delete ( comment );
93
+ Product product = findProductById (productId );
94
+ Comment comment = findCommentById (commentId );
95
+ if (! comment .getProduct ().getId (). equals ( product .getId () )){
96
+ throw new EcommerceApiException ( "Comment does not belong to product" , HttpStatus . BAD_REQUEST );
94
97
}
98
+ commentRepository .delete (comment );
95
99
}
96
100
97
101
//map to dto
@@ -105,4 +109,15 @@ private Comment mapToEntity(CommentDto commentDto){
105
109
Comment comment = modelMapper .map (commentDto , Comment .class );
106
110
return comment ;
107
111
}
112
+
113
+ private Product findProductById (Long productId ){
114
+ Product product = productRepository .findById (productId ).orElseThrow (()->new ResourceNotFoundException ("Product" , productId ));
115
+ return product ;
116
+ }
117
+
118
+ private Comment findCommentById (Long commentId ){
119
+ Comment comment = commentRepository .findById (commentId ).orElseThrow (()->new ResourceNotFoundException ("Comment" , commentId ));
120
+ return comment ;
121
+ }
122
+
108
123
}
0 commit comments