Skip to content

Commit

Permalink
create CartDetail entity
Browse files Browse the repository at this point in the history
  • Loading branch information
naruepracht-s committed Mar 24, 2024
1 parent cc6511b commit b0ad55c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@NoArgsConstructor
public class ApplyCodeResponse {
private String username;
private List<CartItem> items;
private List<CartDetail> items;
private BigDecimal totalPrice;
private BigDecimal totalDiscount;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.kampus.kbazaar.cart;

import com.kampus.kbazaar.promotion.ApplyCodeRequest;
import com.kampus.kbazaar.promotion.ApplyCodeResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down
39 changes: 39 additions & 0 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartDetail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.kampus.kbazaar.cart;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

@Entity(name = "CartDetail")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CartDetail {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;

@Column(name = "sku")
private String sku;

@Column(name = "name")
private String name;

@Column(name = "quantity")
private Integer quantity;

@Column(name = "price")
private BigDecimal price;

@Column(name = "discount")
private BigDecimal discount;

@Column(name = "finalPrice")
private BigDecimal finalPrice;

}
21 changes: 0 additions & 21 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartItem.java

This file was deleted.

11 changes: 2 additions & 9 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.kampus.kbazaar.exceptions.BadRequestException;
import com.kampus.kbazaar.exceptions.NotFoundException;
import com.kampus.kbazaar.promotion.ApplyCodeRequest;
import com.kampus.kbazaar.promotion.ApplyCodeResponse;
import com.kampus.kbazaar.promotion.Promotion;
import com.kampus.kbazaar.promotion.PromotionRepository;
import com.kampus.kbazaar.shopper.Shopper;
Expand Down Expand Up @@ -31,20 +30,14 @@ public ApplyCodeResponse applyCode(String username, ApplyCodeRequest request) {

if (promotion.isPresent()) {
if (promotion.get().getProductSkus().isBlank()) {
return new ApplyCodeResponse(
request.getCode(),
promotion.get().getDiscountAmount(),
promotion.get().getMaxDiscountAmount());
return new ApplyCodeResponse();
}

boolean skuMatch =
request.getProductSkus().stream()
.anyMatch(x -> promotion.get().getProductSkus().contains(x));
if (skuMatch) {
return new ApplyCodeResponse(
request.getCode(),
promotion.get().getDiscountAmount(),
promotion.get().getMaxDiscountAmount());
return new ApplyCodeResponse();
}
throw new BadRequestException("Cannot use discount code.");
} else {
Expand Down

0 comments on commit b0ad55c

Please sign in to comment.