Skip to content

Commit

Permalink
Merge pull request #5 from KBTG-Kampus-ClassNest-SE-Java/story-7
Browse files Browse the repository at this point in the history
Adding getCartByUserName Service
  • Loading branch information
Zhuuuun authored Mar 24, 2024
2 parents a646187 + b0db4e3 commit dff9979
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion kbazaar/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ services:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- '5433:5432'
- '5432:5432'
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public ResponseEntity<CartResponse> 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);
}
}
18 changes: 16 additions & 2 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +20,6 @@ public class CartService {
private final ShopperService shopperService;
private final ProductService productService;

@Autowired
public CartService(
CartRepository cartRepository,
ShopperService shopperService,
Expand Down Expand Up @@ -84,4 +82,20 @@ public CartResponse addCart(String userName, CartRequest cartRequest) {

return new CartResponse(userName, productList, totalPrice, 0);
}

public List<ProductResponse> getCartByUserName(String userName) {
List<Cart> 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();
}
}
2 changes: 1 addition & 1 deletion kbazaar/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dff9979

Please sign in to comment.