From b0db4e38e6d79c7f2d4f3808aa30f415caac5fca Mon Sep 17 00:00:00 2001 From: zhunismp Date: Sun, 24 Mar 2024 10:39:27 +0700 Subject: [PATCH] Adding getCartByUserName Service --- kbazaar/docker-compose.yml | 2 +- .../kampus/kbazaar/cart/CartController.java | 5 +++++ .../com/kampus/kbazaar/cart/CartService.java | 18 ++++++++++++++++-- .../src/main/resources/application.properties | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/kbazaar/docker-compose.yml b/kbazaar/docker-compose.yml index a7b47df..944c354 100644 --- a/kbazaar/docker-compose.yml +++ b/kbazaar/docker-compose.yml @@ -6,4 +6,4 @@ services: POSTGRES_PASSWORD: postgres POSTGRES_DB: postgres ports: - - '5433:5432' + - '5432:5432' diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java index 26c5e0f..4aeb9fc 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java @@ -28,4 +28,9 @@ public ResponseEntity addCart( CartResponse cartResponse = cartService.addCart(userName, cartRequest); return ResponseEntity.status(HttpStatus.CREATED).body(cartResponse); } + + @GetMapping("/carts/{username}") + public ResponseEntity getUserItemsByUserName(@PathVariable String username) { + return new ResponseEntity<>(cartService.getCartByUserName(username), HttpStatus.OK); + } } diff --git a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java index 4782029..0672c95 100644 --- a/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java +++ b/kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java @@ -11,7 +11,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @@ -21,7 +20,6 @@ public class CartService { private final ShopperService shopperService; private final ProductService productService; - @Autowired public CartService( CartRepository cartRepository, ShopperService shopperService, @@ -84,4 +82,20 @@ public CartResponse addCart(String userName, CartRequest cartRequest) { return new CartResponse(userName, productList, totalPrice, 0); } + + public List getCartByUserName(String userName) { + List orders = cartRepository.findAllByUsername(userName); + return orders.stream() + .map( + order -> { + ProductResponse buff = productService.getBySku(order.getSku()); + return new ProductResponse( + buff.id(), + buff.name(), + buff.sku(), + buff.price(), + order.getQuantity()); + }) + .toList(); + } } diff --git a/kbazaar/src/main/resources/application.properties b/kbazaar/src/main/resources/application.properties index a75b178..4fee976 100644 --- a/kbazaar/src/main/resources/application.properties +++ b/kbazaar/src/main/resources/application.properties @@ -8,7 +8,7 @@ spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true -spring.datasource.url=jdbc:postgresql://localhost:5433/postgres +spring.datasource.url=jdbc:postgresql://localhost:5432/postgres spring.datasource.username=postgres spring.datasource.password=postgres spring.datasource.driver-class-name=org.postgresql.Driver