|
|
@@ -2,11 +2,13 @@ package com.example.plantsforyou.cart;
|
|
|
|
|
|
import com.example.plantsforyou.appuser.AppUser;
|
|
|
import com.example.plantsforyou.appuser.AppUserService;
|
|
|
+import com.example.plantsforyou.dto.AddAllToCartDto;
|
|
|
import com.example.plantsforyou.dto.CartDto;
|
|
|
import com.example.plantsforyou.dto.AddToCartDto;
|
|
|
import com.example.plantsforyou.exceptions.RejectedRequestException;
|
|
|
import com.example.plantsforyou.plant.Plant;
|
|
|
import com.example.plantsforyou.plant.PlantService;
|
|
|
+import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
@@ -14,6 +16,9 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
@RestController
|
|
|
@RequestMapping("api/v1/cart")
|
|
|
public class CartController {
|
|
|
@@ -36,7 +41,21 @@ public class CartController {
|
|
|
cartService.addToCart(itemCartDto, plant, appUser);
|
|
|
return new ResponseEntity<>(HttpStatus.CREATED);
|
|
|
}
|
|
|
- @GetMapping()
|
|
|
+ @PostMapping(path = "/all")
|
|
|
+ public ResponseEntity<Object> addAllItemsToCart(@RequestBody AddAllToCartDto addAllToCartDto) throws RejectedRequestException{
|
|
|
+ String token = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("Authorization").substring("Bearer ".length());
|
|
|
+ AppUser appUser = appUserService.getUserFromToken(token);
|
|
|
+
|
|
|
+ for (AddToCartDto item:
|
|
|
+ addAllToCartDto.getAddToCartDtoList()) {
|
|
|
+ Plant plant = plantService.findPlantById(item.getPlantId());
|
|
|
+ cartService.addToCart(item, plant, appUser);
|
|
|
+
|
|
|
+ }
|
|
|
+ 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);
|
|
|
@@ -44,4 +63,5 @@ public class CartController {
|
|
|
|
|
|
return new ResponseEntity<>(cartDto,HttpStatus.ACCEPTED);
|
|
|
}
|
|
|
+
|
|
|
}
|