Jelajahi Sumber

created POST placeOrder and method gettAllItemsFromCart

Makumbacz 3 tahun lalu
induk
melakukan
040f691fd5

+ 10 - 2
src/main/java/com/example/plantsforyou/cart/CartController.java

@@ -3,7 +3,7 @@ package com.example.plantsforyou.cart;
 import com.example.plantsforyou.appuser.AppUser;
 import com.example.plantsforyou.appuser.AppUser;
 import com.example.plantsforyou.appuser.AppUserService;
 import com.example.plantsforyou.appuser.AppUserService;
 import com.example.plantsforyou.dto.CartDto;
 import com.example.plantsforyou.dto.CartDto;
-import com.example.plantsforyou.dto.ItemCartDto;
+import com.example.plantsforyou.dto.AddToCartDto;
 import com.example.plantsforyou.exceptions.RejectedRequestException;
 import com.example.plantsforyou.exceptions.RejectedRequestException;
 import com.example.plantsforyou.plant.Plant;
 import com.example.plantsforyou.plant.Plant;
 import com.example.plantsforyou.plant.PlantService;
 import com.example.plantsforyou.plant.PlantService;
@@ -29,11 +29,19 @@ public class CartController {
     }
     }
 
 
     @PostMapping
     @PostMapping
-    public ResponseEntity<Object> addToCart(@RequestBody ItemCartDto itemCartDto) throws RejectedRequestException {
+    public ResponseEntity<Object> addToCart(@RequestBody AddToCartDto itemCartDto) throws RejectedRequestException {
         String token = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("Authorization").substring("Bearer ".length());
         String token = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("Authorization").substring("Bearer ".length());
         AppUser appUser = appUserService.getUserFromToken(token);
         AppUser appUser = appUserService.getUserFromToken(token);
         Plant plant = plantService.findPlantById(itemCartDto.getPlantId());
         Plant plant = plantService.findPlantById(itemCartDto.getPlantId());
         cartService.addToCart(itemCartDto, plant, appUser);
         cartService.addToCart(itemCartDto, plant, appUser);
         return new ResponseEntity<>(HttpStatus.CREATED);
         return new ResponseEntity<>(HttpStatus.CREATED);
     }
     }
+    @GetMapping()
+    public ResponseEntity<CartDto> getItemsFromCart(){
+        String token = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("Authorization").substring("Bearer ".length());
+        AppUser appUser = appUserService.getUserFromToken(token);
+        CartDto cartDto = cartService.getAllItemsFromCart(appUser);
+
+        return new ResponseEntity<>(cartDto,HttpStatus.ACCEPTED);
+    }
 }
 }

+ 2 - 0
src/main/java/com/example/plantsforyou/cart/CartRepository.java

@@ -5,9 +5,11 @@ import com.example.plantsforyou.plant.Plant;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 import org.springframework.stereotype.Repository;
 
 
+import java.util.List;
 import java.util.Optional;
 import java.util.Optional;
 
 
 @Repository
 @Repository
 public interface CartRepository extends JpaRepository<Cart, Long> {
 public interface CartRepository extends JpaRepository<Cart, Long> {
     Optional<Cart> findByPlantAndAppUser(Plant plant, AppUser appUser);
     Optional<Cart> findByPlantAndAppUser(Plant plant, AppUser appUser);
+    List<Cart> findAllByAppUserOrderByCreatedDate(AppUser appUser);
 }
 }

+ 22 - 2
src/main/java/com/example/plantsforyou/cart/CartService.java

@@ -2,6 +2,7 @@ package com.example.plantsforyou.cart;
 
 
 import com.example.plantsforyou.appuser.AppUser;
 import com.example.plantsforyou.appuser.AppUser;
 import com.example.plantsforyou.dto.CartDto;
 import com.example.plantsforyou.dto.CartDto;
+import com.example.plantsforyou.dto.AddToCartDto;
 import com.example.plantsforyou.dto.ItemCartDto;
 import com.example.plantsforyou.dto.ItemCartDto;
 import com.example.plantsforyou.exceptions.RejectedRequestException;
 import com.example.plantsforyou.exceptions.RejectedRequestException;
 import com.example.plantsforyou.plant.Plant;
 import com.example.plantsforyou.plant.Plant;
@@ -9,6 +10,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
+import java.util.ArrayList;
+import java.util.List;
+
 @Service
 @Service
 public class CartService {
 public class CartService {
     private final CartRepository cartRepository;
     private final CartRepository cartRepository;
@@ -18,12 +22,28 @@ public class CartService {
         this.cartRepository = cartRepository;
         this.cartRepository = cartRepository;
     }
     }
 
 
-    public void addToCart(ItemCartDto itemCartDto, Plant plant, AppUser appUser) throws RejectedRequestException {
+    public void addToCart(AddToCartDto addToCartDto, Plant plant, AppUser appUser) throws RejectedRequestException {
         if(cartRepository.findByPlantAndAppUser(plant,appUser).isPresent()){
         if(cartRepository.findByPlantAndAppUser(plant,appUser).isPresent()){
             throw new RejectedRequestException("Plant is already in a cart", HttpStatus.BAD_REQUEST);
             throw new RejectedRequestException("Plant is already in a cart", HttpStatus.BAD_REQUEST);
         }
         }
-        Cart cart = new Cart(plant,itemCartDto.getAmountInBasket(),appUser);
+        Cart cart = new Cart(plant,addToCartDto.getAmountInBasket(),appUser);
         cartRepository.save(cart);
         cartRepository.save(cart);
     }
     }
 
 
+    public CartDto getAllItemsFromCart(AppUser appUser){
+        List<Cart> cartList = cartRepository.findAllByAppUserOrderByCreatedDate(appUser);
+        List<ItemCartDto> itemCartDto = new ArrayList<>();
+        double totalPrice = 0.0;
+        for (Cart cart: cartList) {
+            ItemCartDto itemCartDtoTemp = getDtoFromCart(cart);
+            itemCartDto.add(itemCartDtoTemp);
+            totalPrice += itemCartDtoTemp.getPlant().getPrice() * itemCartDtoTemp.getQuantity();
+        }
+
+        return new CartDto(itemCartDto,totalPrice);
+    }
+
+    private ItemCartDto getDtoFromCart(Cart cart) {
+        return new ItemCartDto(cart);
+    }
 }
 }

+ 38 - 0
src/main/java/com/example/plantsforyou/dto/AddToCartDto.java

@@ -0,0 +1,38 @@
+package com.example.plantsforyou.dto;
+
+import com.example.plantsforyou.cart.Cart;
+import com.example.plantsforyou.plant.Plant;
+import com.sun.istack.NotNull;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+@NoArgsConstructor
+@Setter
+@Getter
+@AllArgsConstructor
+public class AddToCartDto {
+
+    /*
+    * products: "[{\"id\":1,
+    * \"name\":\"Monstera deliciosa\",
+    * \"price\":25.12,
+    * \"quantity\":3,
+    * \"description\":\"Najbardziej pożądana roślina we wnętrzach ostatnich lat, czule nazwana przez nas Grzegorzem, zawdzięcza swoją popularność nie tylko wyjątkowej urodzie, ale też swojej bezproblemowości. Monstera jest jedną z tych roślin, z którymi będziesz żyć długo i szczęśliwie.\",
+    * \"typeOfLight\":\"diffused\",
+    * \"difficulty\":\"EASY\",
+    * \"size\":\"MEDIUM\",
+    * \"inStock\":true,
+    * \"amountInBasket\":2}]"
+    * */
+    Long id;
+    private @NotNull Long plantId;
+    private @NotNull Integer amountInBasket;
+
+    public AddToCartDto(Long plantId, Integer amountInBasket) {
+        this.plantId = plantId;
+        this.amountInBasket = amountInBasket;
+    }
+
+}

+ 6 - 3
src/main/java/com/example/plantsforyou/dto/CartDto.java

@@ -1,6 +1,5 @@
 package com.example.plantsforyou.dto;
 package com.example.plantsforyou.dto;
 
 
-import com.example.plantsforyou.plant.Plant;
 import lombok.AllArgsConstructor;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.NoArgsConstructor;
@@ -11,8 +10,12 @@ import java.util.List;
 @NoArgsConstructor
 @NoArgsConstructor
 @Setter
 @Setter
 @Getter
 @Getter
-@AllArgsConstructor
 public class CartDto {
 public class CartDto {
     List<ItemCartDto> plantsInCart;
     List<ItemCartDto> plantsInCart;
-    double totalCost;
+    Double totalCost;
+
+    public CartDto(List<ItemCartDto> plantsInCart, Double totalCost) {
+        this.plantsInCart = plantsInCart;
+        this.totalCost = totalCost;
+    }
 }
 }

+ 10 - 24
src/main/java/com/example/plantsforyou/dto/ItemCartDto.java

@@ -1,36 +1,22 @@
 package com.example.plantsforyou.dto;
 package com.example.plantsforyou.dto;
 
 
+import com.example.plantsforyou.cart.Cart;
 import com.example.plantsforyou.plant.Plant;
 import com.example.plantsforyou.plant.Plant;
-import com.sun.istack.NotNull;
-import lombok.AllArgsConstructor;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
 import lombok.Setter;
 
 
-@NoArgsConstructor
-@Setter
 @Getter
 @Getter
-@AllArgsConstructor
+@Setter
+@NoArgsConstructor
 public class ItemCartDto {
 public class ItemCartDto {
+    private Long id;
+    private Plant plant;
+    private Integer quantity;
 
 
-    /*
-    * products: "[{\"id\":1,
-    * \"name\":\"Monstera deliciosa\",
-    * \"price\":25.12,
-    * \"quantity\":3,
-    * \"description\":\"Najbardziej pożądana roślina we wnętrzach ostatnich lat, czule nazwana przez nas Grzegorzem, zawdzięcza swoją popularność nie tylko wyjątkowej urodzie, ale też swojej bezproblemowości. Monstera jest jedną z tych roślin, z którymi będziesz żyć długo i szczęśliwie.\",
-    * \"typeOfLight\":\"diffused\",
-    * \"difficulty\":\"EASY\",
-    * \"size\":\"MEDIUM\",
-    * \"inStock\":true,
-    * \"amountInBasket\":2}]"
-    * */
-    Long Id;
-    private @NotNull Long plantId;
-    private @NotNull Integer amountInBasket;
-
-    public ItemCartDto(Long plantId, Integer amountInBasket) {
-        this.plantId = plantId;
-        this.amountInBasket = amountInBasket;
+    public ItemCartDto(Cart cart){
+        this.id = getId();
+        this.plant = getPlant();
+        this.quantity = getQuantity();
     }
     }
 }
 }

+ 19 - 0
src/main/java/com/example/plantsforyou/dto/PlaceOrderDto.java

@@ -0,0 +1,19 @@
+package com.example.plantsforyou.dto;
+
+
+import lombok.*;
+
+@Getter
+@Setter
+@NoArgsConstructor
+@ToString
+@AllArgsConstructor
+public class PlaceOrderDto {
+    //todo: validation
+    private String phoneNumber;
+    private String postalCode;
+    private String street;
+    private String city;
+
+
+}

+ 4 - 4
src/main/java/com/example/plantsforyou/items_order/ItemsOrder.java → src/main/java/com/example/plantsforyou/items_order/ItemOrder.java

@@ -16,23 +16,23 @@ import java.util.Date;
 @Setter
 @Setter
 @NoArgsConstructor
 @NoArgsConstructor
 @Entity
 @Entity
-public class ItemsOrder {
+public class ItemOrder {
     @Id
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
     private Long id;
     private Date date;
     private Date date;
     @NotNull
     @NotNull
-    private Integer price;
+    private double price;
     @OneToOne
     @OneToOne
     @JoinColumn/*(name = "plant_id", referencedColumnName = "id")*/
     @JoinColumn/*(name = "plant_id", referencedColumnName = "id")*/
     private Plant plant;
     private Plant plant;
-    @OneToOne
+    @ManyToOne
     @JoinColumn/*(name = "order_id", referencedColumnName = "id")*/
     @JoinColumn/*(name = "order_id", referencedColumnName = "id")*/
     private Order order;
     private Order order;
     @NotNull
     @NotNull
     private Integer quantity;
     private Integer quantity;
 
 
-    public ItemsOrder(Integer price, Plant plant, Order order, Integer quantity) {
+    public ItemOrder(Integer price, Plant plant, Order order, Integer quantity) {
         this.date = new Date();
         this.date = new Date();
         this.price = price;
         this.price = price;
         this.plant = plant;
         this.plant = plant;

+ 1 - 1
src/main/java/com/example/plantsforyou/items_order/ItemsOrderController.java → src/main/java/com/example/plantsforyou/items_order/ItemOrderController.java

@@ -3,7 +3,7 @@ package com.example.plantsforyou.items_order;
 import org.springframework.stereotype.Controller;
 import org.springframework.stereotype.Controller;
 
 
 @Controller
 @Controller
-public class ItemsOrderController {
+public class ItemOrderController {
 
 
 
 
 }
 }

+ 1 - 1
src/main/java/com/example/plantsforyou/items_order/ItemsOrderRepository.java → src/main/java/com/example/plantsforyou/items_order/ItemOrderRepository.java

@@ -2,5 +2,5 @@ package com.example.plantsforyou.items_order;
 
 
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaRepository;
 
 
-public interface ItemsOrderRepository extends JpaRepository<ItemsOrder, Long> {
+public interface ItemOrderRepository extends JpaRepository<ItemOrder, Long> {
 }
 }

+ 16 - 0
src/main/java/com/example/plantsforyou/items_order/ItemOrderService.java

@@ -0,0 +1,16 @@
+package com.example.plantsforyou.items_order;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class ItemOrderService {
+    private ItemOrderRepository itemOrderRepository;
+
+    @Autowired
+    public ItemOrderService(ItemOrderRepository itemsOrderRepository) {
+        this.itemOrderRepository = itemsOrderRepository;
+    }
+
+
+}

+ 0 - 14
src/main/java/com/example/plantsforyou/items_order/ItemsOrderService.java

@@ -1,14 +0,0 @@
-package com.example.plantsforyou.items_order;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-@Service
-public class ItemsOrderService {
-    private ItemsOrderRepository itemsOrderRepository;
-
-    @Autowired
-    public ItemsOrderService(ItemsOrderRepository itemsOrderRepository) {
-        this.itemsOrderRepository = itemsOrderRepository;
-    }
-}

+ 22 - 5
src/main/java/com/example/plantsforyou/order/Order.java

@@ -1,31 +1,48 @@
 package com.example.plantsforyou.order;
 package com.example.plantsforyou.order;
 
 
 import com.example.plantsforyou.appuser.AppUser;
 import com.example.plantsforyou.appuser.AppUser;
-import com.example.plantsforyou.items_order.ItemsOrder;
+import com.example.plantsforyou.items_order.ItemOrder;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
 import lombok.Setter;
+import lombok.ToString;
 
 
 import javax.persistence.*;
 import javax.persistence.*;
+import java.util.Date;
 import java.util.List;
 import java.util.List;
 
 
 @Getter
 @Getter
 @Setter
 @Setter
 @NoArgsConstructor
 @NoArgsConstructor
+@ToString
 @Entity
 @Entity
 public class Order {
 public class Order {
+    //todo: validation
     @Id
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Long id;
     private Long id;
-    private String address;
-    private String status;
+
+    private String postalCode;
+
+    private String street;
+
+    private String city;
+
     private String phoneNumber;
     private String phoneNumber;
+
+    private Date createdDate;
+
+    private double totalPrice;
+
     @OneToOne
     @OneToOne
     @JoinColumn
     @JoinColumn
     private AppUser appUser;
     private AppUser appUser;
+
     @OneToMany
     @OneToMany
-    private List<ItemsOrder> itemsOrders;
-    private Integer totalPrice;
+    @ToString.Exclude
+    private List<ItemOrder> itemsOrders;
+
+    private String status;
 
 
 
 
 }
 }

+ 32 - 0
src/main/java/com/example/plantsforyou/order/OrderController.java

@@ -1,7 +1,39 @@
 package com.example.plantsforyou.order;
 package com.example.plantsforyou.order;
 
 
+import com.example.plantsforyou.appuser.AppUser;
+import com.example.plantsforyou.appuser.AppUserService;
+import com.example.plantsforyou.cart.CartService;
+import com.example.plantsforyou.dto.PlaceOrderDto;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Controller;
 import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
 
 @Controller
 @Controller
+@RequestMapping("api/v1/order")
 public class OrderController {
 public class OrderController {
+
+    private final AppUserService appUserService;
+    private final CartService cartService;
+    private final OrderService orderService;
+
+
+    public OrderController(AppUserService appUserService, CartService cartService, OrderService orderService) {
+        this.appUserService = appUserService;
+        this.cartService = cartService;
+        this.orderService = orderService;
+    }
+
+    @PostMapping()
+    public ResponseEntity<Object>placeOrder(@RequestBody PlaceOrderDto placeOrderDto){
+        String token = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("Authorization").substring("Bearer ".length());
+        AppUser appUser = appUserService.getUserFromToken(token);
+        orderService.placeOrder(placeOrderDto,appUser);
+        return new ResponseEntity<>(HttpStatus.CREATED);
+    }
+
 }
 }

+ 42 - 1
src/main/java/com/example/plantsforyou/order/OrderService.java

@@ -1,12 +1,53 @@
 package com.example.plantsforyou.order;
 package com.example.plantsforyou.order;
 
 
+import com.example.plantsforyou.appuser.AppUser;
+import com.example.plantsforyou.cart.CartService;
+import com.example.plantsforyou.dto.CartDto;
+import com.example.plantsforyou.dto.ItemCartDto;
+import com.example.plantsforyou.dto.PlaceOrderDto;
+import com.example.plantsforyou.items_order.ItemOrder;
+import com.example.plantsforyou.items_order.ItemOrderRepository;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
+import java.util.Date;
+import java.util.List;
+
 @Service
 @Service
 public class OrderService {
 public class OrderService {
     private OrderRepository orderRepository;
     private OrderRepository orderRepository;
+    private final CartService cartService;
+    private final ItemOrderRepository itemOrderRepository;
 
 
-    public OrderService(OrderRepository orderRepository) {
+    public OrderService(OrderRepository orderRepository, CartService cartService, ItemOrderRepository itemOrderRepository) {
         this.orderRepository = orderRepository;
         this.orderRepository = orderRepository;
+        this.cartService = cartService;
+        this.itemOrderRepository = itemOrderRepository;
+    }
+
+    public void placeOrder(PlaceOrderDto placeOrderDto, AppUser appUser) {
+        CartDto cartDto = cartService.getAllItemsFromCart(appUser);
+        List<ItemCartDto> itemCartDtos = cartDto.getPlantsInCart();
+
+        Order order = new Order();
+        order.setAppUser(appUser);
+        order.setCity(placeOrderDto.getCity());
+        order.setCreatedDate(new Date());
+        order.setPhoneNumber(placeOrderDto.getPhoneNumber());
+        order.setPostalCode(placeOrderDto.getPostalCode());
+        order.setTotalPrice(cartDto.getTotalCost());
+        order.setStreet(placeOrderDto.getStreet());
+        order.setStatus("Waiting for payment");
+
+        for (ItemCartDto itemCartDto:
+             itemCartDtos) {
+            ItemOrder itemOrder = new ItemOrder();
+            itemOrder.setDate(new Date());
+            itemOrder.setPrice(itemCartDto.getPlant().getPrice());
+            itemOrder.setPlant(itemCartDto.getPlant());
+            itemOrder.setQuantity(itemCartDto.getQuantity());
+            itemOrder.setOrder(order);
+            itemOrderRepository.save(itemOrder);
+        }
+        //todo:delete items from cart
     }
     }
 }
 }