Skip to content

Commit

Permalink
Implement cart controller
Browse files Browse the repository at this point in the history
  • Loading branch information
lysalexy committed Dec 18, 2023
1 parent 267b13a commit 70c69c4
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/main/java/org/example/controller/cart/CartController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.example.controller.cart;

import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.example.controller.BaseController;
import org.example.service.cart.CartService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/cart")
@RequiredArgsConstructor
public class CartController extends BaseController {
private final CartService cartService;

@GetMapping("/{user_id}")
public ResponseEntity<?> getAllItemsInCart(@PathVariable(value = "user_id") Long userId) {
return ResponseEntity.ok(cartService.getUserCart(userId));
}

@GetMapping("/quantity_info")
public ResponseEntity<?> getItemQuantityInCart(
@RequestParam(value = "item_id") Long itemId, @RequestParam(value = "user_id") Long userId) {
return ResponseEntity.ok(cartService.getItemsAmountInUsersCart(userId, itemId));
}

@PostMapping("/add")
public ResponseEntity<?> addItemToCart(
@RequestParam(value = "item_id") Long itemId,
@RequestParam(value = "user_id") Long userId,
@RequestParam(value = "count") Optional<Long> count) {
return ResponseEntity.ok(
cartService.addItemToCart(userId, itemId, count.isPresent() ? count.get() : 1));
}

@DeleteMapping("/delete")
public ResponseEntity<?> deleteItemFromCart(
@RequestParam(value = "item_id") Long itemId,
@RequestParam(value = "user_id") Long userId) {
return ResponseEntity.ok(
cartService.deleteItemFromCart(userId,itemId));
}
}

0 comments on commit 70c69c4

Please sign in to comment.