From 9fdae4a13006a2f42ce46fdf1fc4acf95eed8888 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 15 Sep 2021 16:32:46 +0530 Subject: [PATCH] updated APIs --- .project | 11 +++++++ .../ShopifyOrderAndReviewApplication.java | 2 +- .../cart/controller/OrderController.java | 30 ++++++++++++++----- .../cart/controller/ReviewController.java | 11 +++++-- .../java/com/shopify/cart/model/Order.java | 11 +++++++ .../java/com/shopify/cart/model/Product.java | 10 +++---- .../cart/repository/OrderRepository.java | 2 +- 7 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 .project diff --git a/.project b/.project new file mode 100644 index 0000000..bfb23a5 --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + ShopifyReviewAndOrder + + + + + + + + diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/ShopifyOrderAndReviewApplication.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/ShopifyOrderAndReviewApplication.java index e2c97a9..323dd9e 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/ShopifyOrderAndReviewApplication.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/ShopifyOrderAndReviewApplication.java @@ -10,6 +10,6 @@ public class ShopifyOrderAndReviewApplication { public static void main(String[] args) { SpringApplication.run(ShopifyOrderAndReviewApplication.class, args); - } + } } diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/OrderController.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/OrderController.java index 9a8add6..d09d6fb 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/OrderController.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/OrderController.java @@ -6,12 +6,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.shopify.cart.model.Order; import com.shopify.cart.repository.OrderRepository; +import com.shopify.cart.repository.ProductRepository; import com.shopify.cart.repository.UserRepository; @RestController @@ -20,11 +22,13 @@ public class OrderController { private OrderRepository orderRepository; @Autowired private UserRepository userRepository; - + @Autowired + private ProductRepository productRepository; - @PostMapping("/order/{uid}") - private Order postOrder(@RequestBody Order order, @PathVariable("uid")Long uid) { + @PostMapping("/order/{uid}/{pid}") + private Order postOrder(@RequestBody Order order, @PathVariable("uid")Long uid, @PathVariable("pid") Long pid) { order.setUser(userRepository.getById(uid)); + order.setProduct(productRepository.getById(pid)); return orderRepository.save(order); } @GetMapping("/order/all") @@ -33,9 +37,21 @@ public class OrderController { } -// @GetMapping("/order") -// private List getOrderByProduct(@RequestParam("uid") Long uid){ -// return orderRepository.findAllByUserUserId(uid); -// } + @GetMapping("/order/{uid}") + private List getOrderByProduct(@PathVariable("uid") Long uid){ + return orderRepository.findAllByUserId(uid); + } + + + @PutMapping("/order/{id}/{uid}/{pid}") + private Order editOrder(@RequestBody Order newOrder, @PathVariable("id") Long id, @PathVariable("uid") Long uid, @PathVariable("pid") Long pid) { + Order order=orderRepository.getById(id); + order.setPrice(newOrder.getPrice()); + order.setQuantity(newOrder.getQuantity()); + order.setStatus(newOrder.getStatus()); + order.setUser(userRepository.getById(uid)); + order.setProduct(productRepository.getById(pid)); + return orderRepository.save(order); + } } diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ReviewController.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ReviewController.java index efe23f4..6998acb 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ReviewController.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ReviewController.java @@ -3,6 +3,7 @@ package com.shopify.cart.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -40,7 +41,6 @@ public class ReviewController { return reviewRepository.save(review); } - @GetMapping("/review") private List getAllReviews(){ Listlist =reviewRepository.findAll(); @@ -53,6 +53,7 @@ public class ReviewController { @RequestBody Review newReviewVal) { Review reviewDB=reviewRepository.getById(id); reviewDB.setReviewText(newReviewVal.getReviewText()); + reviewDB.setRating(newReviewVal.getRating()); reviewDB.setProduct(productRepository.getById(pid)); reviewDB.setUser(userRepository.getById(uid)); return reviewRepository.save(reviewDB); @@ -62,5 +63,11 @@ public class ReviewController { public List getAllReviewsByProduct(@RequestParam("pid") Long pid){ return reviewRepository.findAllByProductId(pid); } - + + @DeleteMapping("/review/{id}") + public void deleteReview(@PathVariable("id") Long id) { + reviewRepository.deleteById(id); + + } + } diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Order.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Order.java index 0f90b48..c655870 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Order.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Order.java @@ -20,6 +20,17 @@ public class Order { @OneToOne private User user; + + @OneToOne + private Product product; + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } public User getUser() { return user; diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Product.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Product.java index 8a36bb1..664d138 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Product.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Product.java @@ -13,7 +13,7 @@ public class Product { private Long id; private String name; private String description; - private String rating; + private Integer rating; private Double marketRetailPrice; private Integer discount; private Double discountedPrice; @@ -27,7 +27,7 @@ public class Product { } public String getName() { - return name; + return name; } public void setName(String name) { @@ -42,11 +42,11 @@ public class Product { this.description = description; } - public String getRating() { + public Integer getRating() { return rating; } - public void setRating(String rating) { + public void setRating(Integer rating) { this.rating = rating; } @@ -72,6 +72,6 @@ public class Product { public void setDiscountedPrice(Double discountedPrice) { this.discountedPrice = discountedPrice; - } + } } \ No newline at end of file diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/repository/OrderRepository.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/repository/OrderRepository.java index 99802dc..f90ee49 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/repository/OrderRepository.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/repository/OrderRepository.java @@ -10,7 +10,7 @@ import com.shopify.cart.model.Order; public interface OrderRepository extends JpaRepository { -// List findAllByUserUserId(Long uid); + List findAllByUserId(Long uid);