-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/kasina/automobileapi/controller/ProductReviewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.kasina.automobileapi.controller; | ||
|
||
import com.kasina.automobileapi.model.Order; | ||
import com.kasina.automobileapi.model.ProductReview; | ||
import com.kasina.automobileapi.model.User; | ||
import com.kasina.automobileapi.service.ProductReviewService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/productReview") | ||
@RequiredArgsConstructor | ||
public class ProductReviewController { | ||
private final ProductReviewService productReviewService; | ||
|
||
@PostMapping("/{id}") | ||
public ResponseEntity<ProductReview> saveReview(@RequestBody ProductReview productReview, @PathVariable Long id){ | ||
ProductReview newReview = productReviewService.createReview(productReview, id); | ||
return ResponseEntity.ok(newReview); | ||
} | ||
|
||
@GetMapping("/product/{productId}") | ||
public ResponseEntity<List<ProductReview>> getProductReviews(@PathVariable Long productId) { | ||
List<ProductReview> productReviews = productReviewService.getProductReviews(productId); | ||
return ResponseEntity.ok(productReviews); | ||
} | ||
|
||
@DeleteMapping("/reviewId") | ||
public String deleteProductReview(@PathVariable Long reviewId){ | ||
return productReviewService.deleteProductReview(reviewId); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/kasina/automobileapi/model/ProductReview.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.kasina.automobileapi.model; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import java.time.Instant; | ||
|
||
@Data | ||
@Entity | ||
@Builder | ||
@Table(name = "product_review") | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ProductReview { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(length = 500) | ||
private String reviewText; | ||
private int rating; | ||
@CreationTimestamp | ||
private Instant createdOn; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "product_id") | ||
private Product product; | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/kasina/automobileapi/repository/ProductReviewRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.kasina.automobileapi.repository; | ||
|
||
import com.kasina.automobileapi.model.Product; | ||
import com.kasina.automobileapi.model.ProductReview; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface ProductReviewRepository extends JpaRepository<ProductReview, Long> { | ||
List<ProductReview> findByProduct(Product product); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/com/kasina/automobileapi/service/ProductReviewService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.kasina.automobileapi.service; | ||
|
||
import com.kasina.automobileapi.model.Product; | ||
import com.kasina.automobileapi.model.ProductReview; | ||
import com.kasina.automobileapi.model.User; | ||
import com.kasina.automobileapi.repository.ProductReviewRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ProductReviewService { | ||
@Autowired | ||
private final ProductReviewRepository productReviewRepository; | ||
@Autowired | ||
private final UserService userService; | ||
@Autowired | ||
private final ProductService productService; | ||
|
||
public ProductReview createReview(ProductReview productReview, Long productId){ | ||
User currentUser = userService.getCurrentUser(); | ||
Product product = productService.getProductById(productId); | ||
|
||
var newReview = ProductReview.builder() | ||
.user(currentUser) | ||
.product(product) | ||
.reviewText(productReview.getReviewText()) | ||
.rating(productReview.getRating()) | ||
.build(); | ||
|
||
return productReviewRepository.save(newReview) ; | ||
} | ||
// Get all reviews for a particular product | ||
|
||
public List<ProductReview> getProductReviews(Long productId) { | ||
Product product = productService.getProductById(productId); | ||
return productReviewRepository.findByProduct(product); | ||
} | ||
|
||
public List<ProductReview> getAllReviews() { | ||
return productReviewRepository.findAll(); | ||
} | ||
|
||
public ProductReview getReviewById(Long reviewId) { | ||
return productReviewRepository.findById(reviewId) | ||
.orElseThrow(() -> new NoSuchElementException("Review not found with ID: " + reviewId)); | ||
} | ||
|
||
// Delete a Category by ID | ||
public String deleteProductReview(Long reviewId) { | ||
productReviewRepository.deleteById(reviewId); | ||
return "Review deleted Successfully"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters