Skip to content

Commit 07058de

Browse files
committed
some code refactor is done...
1 parent a71bbfd commit 07058de

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

src/main/java/com/manir/springbootecommercerestapi/service/Impl/CommentServiceImpl.java

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.manir.springbootecommercerestapi.service.Impl;
22

33
import com.manir.springbootecommercerestapi.dto.CommentDto;
4+
import com.manir.springbootecommercerestapi.exception.EcommerceApiException;
45
import com.manir.springbootecommercerestapi.exception.ResourceNotFoundException;
56
import com.manir.springbootecommercerestapi.repository.CommentRepository;
67
import com.manir.springbootecommercerestapi.repository.ProductRepository;
78
import com.manir.springbootecommercerestapi.model.Comment;
89
import com.manir.springbootecommercerestapi.model.Product;
910
import com.manir.springbootecommercerestapi.service.CommentService;
1011
import org.modelmapper.ModelMapper;
12+
import org.springframework.http.HttpStatus;
1113
import org.springframework.stereotype.Service;
1214

1315
import javax.annotation.Resource;
@@ -27,8 +29,7 @@ public class CommentServiceImpl implements CommentService {
2729
@Override
2830
public CommentDto createComment(Long productId, CommentDto commentDto) {
2931

30-
Product product = productRepository.findById(productId).orElseThrow(()->new ResourceNotFoundException("Product", productId));
31-
32+
Product product = findProductById(productId);
3233
//convert to entity
3334
Comment comment = mapToEntity(commentDto);
3435
//save to db
@@ -43,55 +44,58 @@ public CommentDto createComment(Long productId, CommentDto commentDto) {
4344
@Override
4445
public List<CommentDto> getAllComments() {
4546
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());
4750
return commentDtoList;
4851
}
4952

5053
@Override
5154
public List<CommentDto> getAllCommentsByProductId(Long productId) {
5255
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());
5459
return commentDtoList;
5560
}
5661

5762
@Override
5863
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);
6470
}
6571

66-
return responseComment;
72+
return mapToDto(comment);
6773
}
6874

6975
@Override
7076
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);
8079

80+
if (!comment.getProduct().getId().equals(product.getId())){
81+
throw new EcommerceApiException("Comment does not belong to product", HttpStatus.BAD_REQUEST);
8182
}
82-
83+
comment.setReview(commentDto.getReview());
84+
comment.setRate(commentDto.getRate());
85+
comment.setStatus(commentDto.getStatus());
8386
Comment updatedComment = commentRepository.save(comment);
8487

8588
return mapToDto(updatedComment);
8689
}
8790

8891
@Override
8992
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);
9497
}
98+
commentRepository.delete(comment);
9599
}
96100

97101
//map to dto
@@ -105,4 +109,15 @@ private Comment mapToEntity(CommentDto commentDto){
105109
Comment comment = modelMapper.map(commentDto, Comment.class);
106110
return comment;
107111
}
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+
108123
}

0 commit comments

Comments
 (0)