Skip to content

Commit df17029

Browse files
committed
Product Comment CRUD is done successfully.
1 parent e692bb4 commit df17029

File tree

9 files changed

+247
-0
lines changed

9 files changed

+247
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.manir.springbootecommercerestapi.controller;
2+
3+
import com.manir.springbootecommercerestapi.dto.CommentDto;
4+
import com.manir.springbootecommercerestapi.service.CommentService;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import javax.annotation.Resource;
10+
import java.util.List;
11+
12+
@RestController
13+
@RequestMapping(value = "api/v1/products")
14+
public class CommentController {
15+
16+
@Resource
17+
private CommentService commentService;
18+
19+
//create comment api
20+
@PostMapping("/{productId}/createComment")
21+
public ResponseEntity<CommentDto> createComment(@PathVariable Long productId,
22+
@RequestBody CommentDto commentDto){
23+
CommentDto responseComment = commentService.createComment(productId, commentDto);
24+
return new ResponseEntity<>(responseComment, HttpStatus.CREATED);
25+
}
26+
27+
//get all comments api
28+
@GetMapping("/getAllComments")
29+
public List<CommentDto> getAllComments(){
30+
return commentService.getAllComments();
31+
}
32+
33+
//get comments by product id api
34+
@GetMapping("/{productId}/getCommentByProductId")
35+
public List<CommentDto> getCommentByProductId(@PathVariable Long productId){
36+
return commentService.getAllCommentsByProductId(productId);
37+
}
38+
39+
//get comment by id api
40+
@GetMapping("/{productId}/comment/{commentId}")
41+
public ResponseEntity<CommentDto> getCommentById(@PathVariable Long productId, @PathVariable Long commentId){
42+
CommentDto responseComment = commentService.getCommentById(productId, commentId);
43+
return ResponseEntity.ok(responseComment);
44+
}
45+
46+
//update comment api
47+
@PutMapping("/{productId}/update/{commentId}")
48+
public ResponseEntity<CommentDto> updateComment(@PathVariable Long productId,
49+
@RequestBody CommentDto commentDto,
50+
@PathVariable Long commentId){
51+
CommentDto responseComment = commentService.updateComment(productId, commentDto, commentId);
52+
return ResponseEntity.ok(responseComment);
53+
}
54+
55+
//delete comment api
56+
@DeleteMapping("/{productId}/delete/{commentId}")
57+
public ResponseEntity<String> deleteComment(@PathVariable Long productId, @PathVariable Long commentId){
58+
commentService.deleteComment(productId, commentId);
59+
return ResponseEntity.ok("Comment with id: "+commentId+" is successfully:)");
60+
}
61+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.manir.springbootecommercerestapi.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class CommentDto {
7+
8+
private Long id;
9+
private String review;
10+
private Integer rate;
11+
private String status;
12+
13+
}

src/main/java/com/manir/springbootecommercerestapi/dto/ProductDto.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ public class ProductDto {
2121
private CategoryDto category;
2222

2323
private Set<ImageDataDto> images;
24+
25+
private Set<CommentDto> comments;
2426
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.manir.springbootecommercerestapi.repository;
2+
3+
import com.manir.springbootecommercerestapi.resource.Comment;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
public interface CommentRepository extends JpaRepository<Comment, Long> {
10+
List<Comment> findByProductId(Long productId);
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.manir.springbootecommercerestapi.repository;
22

3+
import com.manir.springbootecommercerestapi.resource.Comment;
34
import com.manir.springbootecommercerestapi.resource.Product;
45
import org.springframework.data.jpa.repository.JpaRepository;
56

7+
import java.util.List;
8+
69
public interface ProductRepository extends JpaRepository<Product, Long> {
10+
711
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.manir.springbootecommercerestapi.resource;
2+
3+
import lombok.*;
4+
5+
import javax.persistence.*;
6+
@AllArgsConstructor
7+
@NoArgsConstructor
8+
@Setter
9+
@Getter
10+
@Entity
11+
@Table(name = "product_comments")
12+
public class Comment {
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.IDENTITY)
15+
private Long id;
16+
@Column(name = "review")
17+
private String review;
18+
@Column(name = "rate")
19+
private Integer rate;
20+
@Column(name = "status")
21+
private String status;
22+
23+
@ManyToOne(fetch = FetchType.LAZY)
24+
@JoinColumn(name = "product_id")
25+
private Product product;
26+
}

src/main/java/com/manir/springbootecommercerestapi/resource/Product.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ public class Product {
4545
//relation image gallery
4646
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
4747
private Set<ImageData> images;
48+
49+
//relation to product comment
50+
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL, orphanRemoval = true)
51+
private Set<Comment> comments;
4852
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.manir.springbootecommercerestapi.service;
2+
3+
4+
import com.manir.springbootecommercerestapi.dto.CommentDto;
5+
6+
import java.util.List;
7+
8+
public interface CommentService {
9+
10+
CommentDto createComment(Long productId, CommentDto commentDto);
11+
12+
List<CommentDto> getAllComments();
13+
List<CommentDto> getAllCommentsByProductId(Long productId);
14+
CommentDto getCommentById(Long productId, Long commentId);
15+
CommentDto updateComment(Long productId, CommentDto commentDto, Long commentId);
16+
void deleteComment(Long productId, Long commentId);
17+
18+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.manir.springbootecommercerestapi.service.Impl;
2+
3+
import com.manir.springbootecommercerestapi.dto.CommentDto;
4+
import com.manir.springbootecommercerestapi.exception.ResourceNotFoundException;
5+
import com.manir.springbootecommercerestapi.repository.CommentRepository;
6+
import com.manir.springbootecommercerestapi.repository.ProductRepository;
7+
import com.manir.springbootecommercerestapi.resource.Comment;
8+
import com.manir.springbootecommercerestapi.resource.Product;
9+
import com.manir.springbootecommercerestapi.service.CommentService;
10+
import org.modelmapper.ModelMapper;
11+
import org.springframework.stereotype.Service;
12+
13+
import javax.annotation.Resource;
14+
import java.util.List;
15+
import java.util.stream.Collectors;
16+
17+
@Service
18+
public class CommentServiceImpl implements CommentService {
19+
20+
@Resource
21+
private ModelMapper modelMapper;
22+
@Resource
23+
private CommentRepository commentRepository;
24+
@Resource
25+
private ProductRepository productRepository;
26+
27+
@Override
28+
public CommentDto createComment(Long productId, CommentDto commentDto) {
29+
30+
Product product = productRepository.findById(productId).orElseThrow(()->new ResourceNotFoundException("Product", productId));
31+
32+
//convert to entity
33+
Comment comment = mapToEntity(commentDto);
34+
//save to db
35+
comment.setProduct(product);
36+
Comment createdComment = commentRepository.save(comment);
37+
//convert to dto
38+
CommentDto responseComment = mapToDto(createdComment);
39+
40+
return responseComment;
41+
}
42+
43+
@Override
44+
public List<CommentDto> getAllComments() {
45+
List<Comment> comments = commentRepository.findAll();
46+
List<CommentDto> commentDtoList = comments.stream().map(comment -> mapToDto(comment)).collect(Collectors.toList());
47+
return commentDtoList;
48+
}
49+
50+
@Override
51+
public List<CommentDto> getAllCommentsByProductId(Long productId) {
52+
List<Comment> comments = commentRepository.findByProductId(productId);
53+
List<CommentDto> commentDtoList = comments.stream().map(comment -> mapToDto(comment)).collect(Collectors.toList());
54+
return commentDtoList;
55+
}
56+
57+
@Override
58+
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+
66+
return responseComment;
67+
}
68+
69+
@Override
70+
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());
80+
81+
}
82+
83+
Comment updatedComment = commentRepository.save(comment);
84+
85+
return mapToDto(updatedComment);
86+
}
87+
88+
@Override
89+
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);
94+
}
95+
}
96+
97+
//map to dto
98+
private CommentDto mapToDto(Comment comment){
99+
CommentDto commentDto = modelMapper.map(comment, CommentDto.class);
100+
return commentDto;
101+
}
102+
103+
//map to entity
104+
private Comment mapToEntity(CommentDto commentDto){
105+
Comment comment = modelMapper.map(commentDto, Comment.class);
106+
return comment;
107+
}
108+
}

0 commit comments

Comments
 (0)