Skip to content

Commit

Permalink
product review added
Browse files Browse the repository at this point in the history
  • Loading branch information
casyazmon committed Jul 23, 2023
1 parent d40f032 commit 867f931
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public ResponseEntity<List<Order>> getOrdersByUser(@PathVariable Long userId) {
}

@PutMapping("/{orderId}")
public ResponseEntity<?> upDateOrder(@RequestBody OrderRequest orderRequest, @PathVariable Long orderId){
public ResponseEntity<?> updateOrder(@RequestBody OrderRequest orderRequest, @PathVariable Long orderId){
Order order = orderService.updateOrder(orderRequest, orderId);
return ResponseEntity.ok(order);
}


@GetMapping("/{orderId}")
public ResponseEntity<Order> getOrderById(@PathVariable Long orderId) {
User currentUser = userService.getCurrentUser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@RequiredArgsConstructor
public class ProductController {
private final ProductService productService;

@GetMapping
public ResponseEntity<List<Product>> getAllProducts() {
List<Product> products = productService.getAllProducts();
Expand Down
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 src/main/java/com/kasina/automobileapi/model/ProductReview.java
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;
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.kasina.automobileapi.repository.OrderRepository;
import com.kasina.automobileapi.repository.ProductRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.crossstore.ChangeSetPersister;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
Expand Down
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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ public Product getProductById(Long productId) {
return productRepository.findById(productId).orElseThrow(() -> new IllegalArgumentException("Product not found"));
}

// Update a product


// Delete a product by ID
public void deleteProductById(Long productId) {
productRepository.deleteById(productId);
Expand Down

0 comments on commit 867f931

Please sign in to comment.