1 | package tech.techharbor.Service.Impl;
|
---|
2 |
|
---|
3 | import org.springframework.stereotype.Service;
|
---|
4 | import tech.techharbor.Model.Exceptions.ReviewNotFoundException;
|
---|
5 | import tech.techharbor.Model.ReviewModel;
|
---|
6 | import tech.techharbor.Repository.ReviewRepository;
|
---|
7 | import tech.techharbor.Service.ReviewService;
|
---|
8 |
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Optional;
|
---|
11 |
|
---|
12 | @Service
|
---|
13 | public class ReviewServiceImpl implements ReviewService {
|
---|
14 |
|
---|
15 | private final ReviewRepository reviewRepository;
|
---|
16 |
|
---|
17 | public ReviewServiceImpl(ReviewRepository reviewRepository) {
|
---|
18 | this.reviewRepository = reviewRepository;
|
---|
19 | }
|
---|
20 |
|
---|
21 | @Override
|
---|
22 | public List<ReviewModel> listReviews() {
|
---|
23 | return this.reviewRepository.findAll();
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public Optional<ReviewModel> findById(Integer id) {
|
---|
28 | return this.reviewRepository.findById(id);
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public ReviewModel create(Integer rating, String description, Integer customerId, Integer productId) {
|
---|
33 | ReviewModel review = new ReviewModel(rating, description, customerId, productId);
|
---|
34 | return this.reviewRepository.save(review);
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public ReviewModel update(Integer id, Integer rating, String description) {
|
---|
39 | ReviewModel review = this.reviewRepository.findById(id).orElseThrow(() -> new ReviewNotFoundException(id));
|
---|
40 | review.setReviewRating(rating);
|
---|
41 | review.setReviewDescription(description);
|
---|
42 | return this.reviewRepository.save(review);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void delete(Integer id) {
|
---|
47 | this.reviewRepository.deleteById(id);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public List<ReviewModel> getReviewsByProductId(Integer productId) {
|
---|
52 | return this.reviewRepository.findByProductId(productId);
|
---|
53 | }
|
---|
54 | }
|
---|