diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/ShopifyOrderAndReviewApplication.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/ShopifyOrderAndReviewApplication.java index 323dd9e..329dfb8 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/ShopifyOrderAndReviewApplication.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/ShopifyOrderAndReviewApplication.java @@ -12,4 +12,4 @@ public class ShopifyOrderAndReviewApplication { 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 d09d6fb..5e28a50 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/OrderController.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/OrderController.java @@ -11,40 +11,66 @@ 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.Cart; import com.shopify.cart.model.Order; +import com.shopify.cart.model.Product; +import com.shopify.cart.repository.CartRepository; import com.shopify.cart.repository.OrderRepository; import com.shopify.cart.repository.ProductRepository; import com.shopify.cart.repository.UserRepository; @RestController public class OrderController { + + + @Autowired private OrderRepository orderRepository; @Autowired private UserRepository userRepository; @Autowired private ProductRepository productRepository; + @Autowired + private CartRepository cartRepository; + - @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); - } + @PostMapping("/order/{uid}") + public void postOrder(@PathVariable("uid")Long uid) { + List allItems=cartRepository.findAllByUserId(uid); + Long orderNo=(long) 1; + List allOrders = orderRepository.findAll(); + if(allOrders.size()>0) { + Long num=orderRepository.maxOrderNumber(); + orderNo=num+1; + } + + for(Cart c : allItems) { + Order orderNew=new Order(); + orderNew.setUser(c.getUser()); + orderNew.setProduct(c.getProduct()); + orderNew.setStatus("not delivered"); + orderNew.setQuantity(c.getQuantity()); + orderNew.setPrice(c.getPrice()); + orderNew.setOrderNumber(orderNo); + orderRepository.save(orderNew); + + } + + } @GetMapping("/order/all") - private List getOrders(){ + public List getOrders(){ return orderRepository.findAll(); - } - + } + @GetMapping("/order/{uid}") - private List getOrderByProduct(@PathVariable("uid") Long uid){ + public 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) { + public 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()); diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ProductController.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ProductController.java index 2b49ed8..5407ae3 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ProductController.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ProductController.java @@ -13,7 +13,7 @@ public class ProductController { private ProductRepository productRepository; @PostMapping("/product") - private Product postProduct(@RequestBody Product product) { + public Product postProduct(@RequestBody Product product) { return productRepository.save(product); } } 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 6998acb..c833503 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ReviewController.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/ReviewController.java @@ -1,5 +1,6 @@ package com.shopify.cart.controller; +import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; @@ -12,7 +13,10 @@ 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.Product; import com.shopify.cart.model.Review; +import com.shopify.cart.model.ReviewDto; +import com.shopify.cart.model.User; import com.shopify.cart.repository.ProductRepository; import com.shopify.cart.repository.ReviewRepository; import com.shopify.cart.repository.UserRepository; @@ -30,7 +34,7 @@ public class ReviewController { //post the details of review. Of which product, followed by which User. @PostMapping("/review/{pid}/{uid}") - private Review postReview(@RequestBody Review review, @PathVariable("pid") Long pid, + public Review postReview(@RequestBody Review review, @PathVariable("pid") Long pid, @PathVariable("uid") Long uid) { //add the product and User System.out.println("adding product"); @@ -38,18 +42,36 @@ public class ReviewController { System.out.println("adding User"); review.setUser(userRepository.getById(uid)); System.out.println("review Added successfully!"); + System.out.println("post rewview"); return reviewRepository.save(review); } @GetMapping("/review") - private List getAllReviews(){ + public List getAllReviews(){ Listlist =reviewRepository.findAll(); System.out.println("showing reviews succesfully"); return list; } + @GetMapping("/review/dto") + public List getReviewDto(){ + Listlist =reviewRepository.findAll(); + + List dtoList=new ArrayList<>(); + for(Review r : list) { + ReviewDto dto=new ReviewDto(); + dto.setReviewText(r.getReviewText()); + dto.setProduct_name(r.getProduct().getName()); + dto.setUsername(r.getUser().getName()); + dto.setRating(r.getRating()); + dtoList.add(dto); + } + return dtoList; + } + + @PutMapping("/review/{id}/{pid}/{uid}") - private Review updateReview(@PathVariable("id") Long id, @PathVariable("pid") Long pid, @PathVariable("uid") Long uid, + public Review updateReview(@PathVariable("id") Long id, @PathVariable("pid") Long pid, @PathVariable("uid") Long uid, @RequestBody Review newReviewVal) { Review reviewDB=reviewRepository.getById(id); reviewDB.setReviewText(newReviewVal.getReviewText()); @@ -60,8 +82,20 @@ public class ReviewController { } @GetMapping("/review/product") - public List getAllReviewsByProduct(@RequestParam("pid") Long pid){ - return reviewRepository.findAllByProductId(pid); + public List getAllReviewsByProduct(@RequestParam("pid") Long pid){ + + Listlist =reviewRepository.findAllByProductId(pid); + + List dtoList=new ArrayList<>(); + for(Review r : list) { + ReviewDto dto=new ReviewDto(); + dto.setReviewText(r.getReviewText()); + dto.setProduct_name(r.getProduct().getName()); + dto.setUsername(r.getUser().getName()); + dto.setRating(r.getRating()); + dtoList.add(dto); + } + return dtoList; } @DeleteMapping("/review/{id}") diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/UserController.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/UserController.java index c9b953c..ba98de6 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/UserController.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/controller/UserController.java @@ -13,7 +13,7 @@ public class UserController { @Autowired private UserRepository userRepository; @PostMapping("/user") - private User postUser(@RequestBody User user) { + public User postUser(@RequestBody User user) { return userRepository.save(user); } } diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Cart.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Cart.java new file mode 100644 index 0000000..4a675ed --- /dev/null +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Cart.java @@ -0,0 +1,62 @@ +package com.shopify.cart.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class Cart { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long cart_id; + @OneToOne + private User user; + @OneToOne + private Product product; + private Integer quantity; + private Double price; + + public Long getCart_id() { + return cart_id; + } + + public void setCart_id(Long cart_id) { + this.cart_id = cart_id; + } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + +} \ No newline at end of file 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 c655870..992c5b9 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Order.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Order.java @@ -16,7 +16,8 @@ public class Order { private Long id; private Integer quantity; private Double price; - private Boolean status; + private String status; + private Long orderNumber; @OneToOne private User user; @@ -24,6 +25,23 @@ public class Order { @OneToOne private Product product; + public String getStatus() { + return status; + } + + public Long getOrderNumber() { + return orderNumber; + } + + public void setOrderNumber(Long orderNumber) { + this.orderNumber = orderNumber; + } + + public void setStatus(String status) { + this.status = status; + } + + public Product getProduct() { return product; } @@ -64,32 +82,6 @@ public class Order { this.price = price; } - public Boolean getStatus() { - return status; - } - - public void setStatus(Boolean status) { - this.status = status; - } - - public Order(Long id, Integer quantity, Double price, Boolean status, User user) { - super(); - this.id = id; - this.quantity = quantity; - this.price = price; - this.status = status; - this.user = user; - } - - public Order() { - super(); - // TODO Auto-generated constructor stub - } - @Override - public String toString() { - return "Order [id=" + id + ", quantity=" + quantity + ", price=" + price + ", status=" + status + ", user=" - + user + "]"; - } } diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/OrderDto.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/OrderDto.java new file mode 100644 index 0000000..f81e883 --- /dev/null +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/OrderDto.java @@ -0,0 +1,5 @@ +package com.shopify.cart.model; + +public class OrderDto { + +} 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 664d138..4a190e8 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Product.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/Product.java @@ -12,12 +12,22 @@ public class Product { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; + private String imageUrl; private String description; private Integer rating; private Double marketRetailPrice; private Integer discount; private Double discountedPrice; + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + public Long getId() { return id; } diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/ReviewDto.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/ReviewDto.java new file mode 100644 index 0000000..d0abbe3 --- /dev/null +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/model/ReviewDto.java @@ -0,0 +1,38 @@ +package com.shopify.cart.model; + +public class ReviewDto { + private String reviewText; + private Integer rating; + private String username; + private String product_name; + public ReviewDto() { + super(); + // TODO Auto-generated constructor stub + } + public String getReviewText() { + return reviewText; + } + public void setReviewText(String reviewText) { + this.reviewText = reviewText; + } + public Integer getRating() { + return rating; + } + public void setRating(Integer rating) { + this.rating = rating; + } + public String getUsername() { + return username; + } + public void setUsername(String username) { + this.username = username; + } + public String getProduct_name() { + return product_name; + } + public void setProduct_name(String product_name) { + this.product_name = product_name; + } + + +} diff --git a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/repository/CartRepository.java b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/repository/CartRepository.java new file mode 100644 index 0000000..f2cae1b --- /dev/null +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/repository/CartRepository.java @@ -0,0 +1,15 @@ +package com.shopify.cart.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import com.shopify.cart.model.Cart; + +public interface CartRepository extends JpaRepository { + + @Query("select c from Cart c join c.user u where u.id=?1") + List findAllByUserId(Long uid); + +} 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 f90ee49..31b64b5 100644 --- a/ShopifyOrderAndReview/src/main/java/com/shopify/cart/repository/OrderRepository.java +++ b/ShopifyOrderAndReview/src/main/java/com/shopify/cart/repository/OrderRepository.java @@ -5,13 +5,15 @@ package com.shopify.cart.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import com.shopify.cart.model.Order; public interface OrderRepository extends JpaRepository { List findAllByUserId(Long uid); - - + + @Query("select max(o.orderNumber) from Order o") + Long maxOrderNumber(); } diff --git a/ShopifyOrderAndReview/src/main/resources/application.properties b/ShopifyOrderAndReview/src/main/resources/application.properties index 01d3c87..2440ee4 100644 --- a/ShopifyOrderAndReview/src/main/resources/application.properties +++ b/ShopifyOrderAndReview/src/main/resources/application.properties @@ -1,8 +1,8 @@ -server.port=9999 +server.port=8003 -spring.datasource.url=jdbc:mysql://localhost:3306/ShopifyDB?createDatabaseIfNotExist=true -spring.datasource.username=root -spring.datasource.password=root +spring.datasource.url=jdbc:mysql://10.3.117.21:3306/Shopify_DB?createDatabaseIfNotExist=true +spring.datasource.username=testuser +spring.datasource.password=PASSWORD123 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect