Skip to content

Commit

Permalink
add: story-4
Browse files Browse the repository at this point in the history
  • Loading branch information
Celesca committed Mar 24, 2024
1 parent 7a59912 commit f6e0efc
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 98 deletions.
15 changes: 5 additions & 10 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/Cart.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kampus.kbazaar.cart;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -10,12 +11,9 @@
@Data
@Getter
@Setter
@AllArgsConstructor
public class Cart {

// public Cart(int userID) {
// this.userID = userID;
// }

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cart_id")
Expand All @@ -24,14 +22,11 @@ public class Cart {
@Column(name = "quantity")
private int quantity;

@Column(name = "user_id")
private int userId;
@Column(name = "username")
private String username;

@Column(name = "product_id")
private int productId;

@Column(name = "promotion_id")
private Long promotionId;
private Long product_id;

// private int userID;

Expand Down
45 changes: 24 additions & 21 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.kampus.kbazaar.cart;

import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -16,28 +12,35 @@ public CartController(CartService cartService) {
this.cartService = cartService;
}

@GetMapping("/carts")
public ResponseEntity getCart() { // NOSONAR
return ResponseEntity.ok().build();
}
// @GetMapping("/carts")
// public ResponseEntity getCart() { // NOSONAR
// return ResponseEntity.ok().build();
// }

// @ApiResponse(
// responseCode = "200",
// description = "add promotion to all products in cart",
// content = {
// @Content(
// mediaType = "application/json",
// schema = @Schema(implementation = Cart.class))
// })
// @PostMapping("/carts/{username}/promotions")
// public CartResponse appliedPromotionAll(
// @PathVariable String username, @RequestBody PromotionAllRequest cart) { // NOSONAR
// return cartService.addPromotionAll(username, cart);
// }

@ApiResponse(
responseCode = "200",
description = "add promotion to all products in cart",
content = {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = Cart.class))
})
@PostMapping("/carts/{username}/promotions")
public CartResponse appliedPromotionAll(
@PathVariable String username, @RequestBody PromotionAllRequest cart) { // NOSONAR
return cartService.addPromotionAll(username, cart);
@PostMapping("/carts/{username}/items")
public CartResponse addItemToCart(
@PathVariable String username, @RequestBody ItemRequest itemRequest) {
return cartService.addItemToCart("Iphone", 1, username);
}
}

// @PostMapping("/carts/{username}/promotions")
// public ResponseEntity createCartPromotions(@PathVariable String username) {
// cartService.createCartPromotions30UpTo200();
// return ResponseEntity.ok().build();
// }
}
// }
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kampus.kbazaar.cart;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -8,4 +9,5 @@ public interface CartRepository extends JpaRepository<Cart, Integer> {

// @Query("SELECT FROM Cart c WHERE c.userId = ?1")
// +Optional<Cart> findByUserId(int userId);
Optional<Cart> findByUsername(String username);
}
106 changes: 76 additions & 30 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,103 @@
package com.kampus.kbazaar.cart;

import com.kampus.kbazaar.product.ProductRepository;
import com.kampus.kbazaar.promotion.PromotionRepository;
import com.kampus.kbazaar.promotion.PromotionResponse;
import com.kampus.kbazaar.promotion.PromotionService;
import com.kampus.kbazaar.shopper.ShopperRepository;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;

@Service
public class CartService {
private final PromotionRepository promotionRepository;
private final PromotionService promotionService;
private final ShopperRepository ShopperRepository;
private final ProductRepository productRepository;
private final CartRepository cartRepository;

public CartService(
PromotionRepository promotionRepository,
PromotionService promotionService,
ShopperRepository ShopperRepository) {
ShopperRepository ShopperRepository,
ProductRepository productRepository,
CartRepository cartRepository) {
this.promotionRepository = promotionRepository;
this.promotionService = promotionService;
this.ShopperRepository = ShopperRepository;
this.productRepository = productRepository;
this.cartRepository = cartRepository;
}

// Story 5 : Add promotion to all products
public CartResponse addPromotionAll(String username, PromotionAllRequest promotion) {

if (ShopperRepository.findByUsername(username).isEmpty()) {
throw new RuntimeException("Shopper not found");
}

if (promotion.getCode().equals("FIXEDAMOUNT10")) {
PromotionResponse newPromotion =
promotionService.getPromotionByCode(promotion.getCode());
// if (isDateBetween(newPromotion.startDate(),
// newPromotion.endDate(),
// LocalDateTime.now()))
// {
return new CartResponse(username, null, 0, 0);
} else {
throw new RuntimeException("Promotion not found");
}
}
// Story 4 : Add specific product to my cart
public CartResponse addItemToCart(String productSku, int quantity, String username) {

// Optional<Product> product = productRepository.findBySku(productSku);

// public boolean isDateBetween(
// LocalDateTime startDate, LocalDateTime endDate, LocalDateTime dateToCheck) {
// return !dateToCheck.isBefore(startDate) && !dateToCheck.isAfter(endDate);
// }
// if (product.isEmpty()) {
// throw new NotFoundException("Product not found");
// }

public void createCartPromotions30UpTo200() {
// List<Cart> carts = cartRepository.findByUserId(1L);
// if (cartRepository.findByUserId(1L).isEmpty())
// throw new NotFoundException("Cart not found");
// Find the cart by username
Optional<Cart> cart = cartRepository.findByUsername(username);
// Product mockProduct = productRepository.findBySku(productSku);
Item mockItem =
new Item(
"MOBILE-APPLE-IPHONE-12-PRO",
"IPHONE12",
1,
new BigDecimal("10000.00"),
new BigDecimal("10.00"),
new BigDecimal("10.00"));

BigDecimal TotalPrice = new BigDecimal(0);
return new CartResponse(username, List.of(mockItem), 0, 0);
}

// if (cart.isEmpty()) {
// // Create a new cart
// Cart newCart = new Cart(1, 1, username, product.get().getId());
// cartRepository.save(newCart);
// }
// else {
// // Add the product to the cart
// Cart existingCart = cart.get();
// Product[] products = existingCart.getProducts();
// Product[] newProducts = new Product[products.length + 1];
// for (int i = 0; i < products.length; i++) {
// newProducts[i] = products[i];
// }
// newProducts[products.length] = product.get();
// existingCart.setProducts(newProducts);
// cartRepository.save(existingCart);
// }
//
// return new Cart(username, new Product[]{product.get()});
}
// }

// Story 5 : Add promotion to all products
// public CartResponse addPromotionAll(String username, PromotionAllRequest promotion) {
//
// if (ShopperRepository.findByUsername(username).isEmpty()) {
// throw new RuntimeException("Shopper not found");
// }
//
// if (promotion.getCode().equals("FIXEDAMOUNT10")) {
// PromotionResponse newPromotion =
// promotionService.getPromotionByCode(promotion.getCode());
// return new CartResponse(username, null, 0, 0);
// } else {
// throw new RuntimeException("Promotion not found");
// }
// }
//
//
//
// public void createCartPromotions30UpTo200() {
// // List<Cart> carts = cartRepository.findByUserId(1L);
// // if (cartRepository.findByUserId(1L).isEmpty())
// // throw new NotFoundException("Cart not found");
//
// BigDecimal TotalPrice = new BigDecimal(0);
// }
14 changes: 11 additions & 3 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/Item.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.kampus.kbazaar.cart;

import java.math.BigDecimal;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@AllArgsConstructor
@Setter
public class Item {
private String sku;
private String name;
private Integer quantity;
private Integer price;
private Integer discount;
private Integer finalPrice;
private BigDecimal price;
private BigDecimal discount;
private BigDecimal finalPrice;
}
19 changes: 19 additions & 0 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/ItemRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kampus.kbazaar.cart;

public class ItemRequest {
private String sku;
private Integer quantity;

public ItemRequest(String sku, Integer quantity) {
this.sku = sku;
this.quantity = quantity;
}

public String getSku() {
return sku;
}

public Integer getQuantity() {
return quantity;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.kampus.kbazaar.product;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {

Optional<Product> findBySku(String sku);
Product findBySku(String sku);
// @Query("SELECT p FROM Product p WHERE p.name = ?1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.kampus.kbazaar.exceptions.NotFoundException;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -19,11 +18,11 @@ public List<ProductResponse> getAll() {
}

public ProductResponse getBySku(String sku) {
Optional<Product> product = productRepository.findBySku(sku);
if (product.isEmpty()) {
Product product = productRepository.findBySku(sku);
if (product == null) {
throw new NotFoundException("Product not found");
}

return product.get().toResponse();
return product.toResponse();
}
}
8 changes: 3 additions & 5 deletions kbazaar/src/main/resources/sql/schema/cart.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ CREATE TABLE IF NOT EXISTS cart
INT
NOT
NULL,
user_id
INT
username
VARCHAR(255)
NOT
NULL,
product_id
INT
NOT
NULL,
promotion_id
INT
NULL
);
Loading

0 comments on commit f6e0efc

Please sign in to comment.