Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. @RestController
  2. @RequestMapping(UserController.USER_CONTROLLER_BASIC_URL)
  3. public class UserController {
  4.  
  5.     public static final String USER_CONTROLLER_BASIC_URL = "/api/v1/users";
  6.  
  7.     private final UserService userService;
  8.     private final ProductService productService;
  9.     private final ShoppingCartService shoppingCartService;
  10.  
  11.     @Autowired
  12.     public UserController(UserService userService, ProductService productService, ShoppingCartService shoppingCartService) {
  13.         this.userService = userService;
  14.         this.productService = productService;
  15.         this.shoppingCartService = shoppingCartService;
  16.     }
  17.  
  18.     @GetMapping
  19.     @ResponseStatus(HttpStatus.OK)
  20.     public UserListDTO getListOfUsers() {
  21.  
  22.         return userService.getAllUsers();
  23.     }
  24.  
  25.     @GetMapping("/{id}")
  26.     @ResponseStatus(HttpStatus.OK)
  27.     public UserDTO getUserById(@PathVariable Long id) {
  28.  
  29.         return userService.getUserById(id);
  30.     }
  31.  
  32.     @PostMapping
  33.     @ResponseStatus(HttpStatus.CREATED)
  34.     public UserDTO createNewUser(@RequestBody @Valid UserDTO userDTO) {
  35.  
  36.        return userService.createNewUser(userDTO);
  37.     }
  38.  
  39.     @PutMapping("/{id}")
  40.     @ResponseStatus(HttpStatus.OK)
  41.     public UserDTO updateUser(@PathVariable Long id, @RequestBody @Valid UserDTO userDTO) {
  42.  
  43.         return userService.updateUser(id, userDTO);
  44.     }
  45.  
  46.     @PatchMapping("/{id}")
  47.     @ResponseStatus(HttpStatus.OK)
  48.     public UserDTO patchUser(@PathVariable Long id, @RequestBody @Valid UserDTO userDTO) {
  49.  
  50.         return userService.patchUser(id, userDTO);
  51.     }
  52.  
  53.     @DeleteMapping("/{id}")
  54.     @ResponseStatus(HttpStatus.OK)
  55.     public void deleteUserById(@PathVariable Long id) {
  56.  
  57.         userService.deleteUserById(id);
  58.     }
  59.  
  60.     @GetMapping("/{id}/shoppingCarts")  //todo refactor this to retrieve All shopping Carts
  61.     @ResponseStatus(HttpStatus.OK)
  62.     public ShoppingCartDTO getListOfAllShoppingCarts(@PathVariable Long id) {
  63.  
  64.         return userService.getUserById(id).getShoppingCartDTO();    //todo update it to pass a list ( after auditing problem)
  65.     }
  66.  
  67.     @PostMapping("/{id}/shoppingCarts")
  68.     @ResponseStatus(HttpStatus.CREATED)
  69.     public ShoppingCartDTO addNewShoppingCart(@PathVariable Long id,  @RequestBody @Valid  ShoppingCartDTO shoppingCartDTO) {
  70.  
  71.         shoppingCartDTO.setUserDTO(userService.getUserById(id));
  72.         ShoppingCartDTO savedShoppingCard = shoppingCartService.createNewShoppingCart(shoppingCartDTO);
  73.  
  74.         userService.getUserById(id).setShoppingCartDTO(savedShoppingCard);    //todo add exception for already active cart
  75.         return savedShoppingCard;
  76.     }
  77.  
  78.     @GetMapping("/{id}/products")
  79.     @ResponseStatus(HttpStatus.OK)
  80.     public ProductListDTO getListOfAllUsersProducts(@PathVariable Long id) {
  81.  
  82.         return new ProductListDTO(userService.getUserById(id).getProductDTOs());
  83.     }
  84.  
  85.     @PostMapping("/{id}/products")
  86.     @ResponseStatus(HttpStatus.CREATED)
  87.     public ProductDTO addNewProductToUser(@PathVariable Long id, @RequestBody @Valid ProductDTO productDTO) {
  88.  
  89.         productDTO.setUserDTO( userService.getUserById(id));
  90.  
  91.         ProductDTO savedProductDTO = productService.createNewProduct(productDTO);
  92.  
  93.         userService.getUserById(id).getProductDTOs().add(savedProductDTO);
  94.         userService.saveUser(userService.getUserById(id));
  95.  
  96.         return savedProductDTO;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement