Skip to content

Commit

Permalink
πŸš€ :: Api-v0.0.5
Browse files Browse the repository at this point in the history
πŸš€ :: Api-v0.0.5
  • Loading branch information
ImNM authored Jan 18, 2023
2 parents 3dab3b8 + 4a9e041 commit 9a4a1cf
Show file tree
Hide file tree
Showing 97 changed files with 1,738 additions and 288 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package band.gosrock.api.cart.controller;


import band.gosrock.api.cart.docs.CreateCartExceptionDocs;
import band.gosrock.api.cart.model.dto.request.AddCartRequest;
import band.gosrock.api.cart.model.dto.response.CreateCartResponse;
import band.gosrock.api.cart.model.dto.response.CartResponse;
import band.gosrock.api.cart.service.CreateCartUseCase;
import band.gosrock.api.cart.service.ReadCartLineUseCase;
import band.gosrock.api.cart.service.ReadCartUseCase;
import band.gosrock.common.annotation.ApiErrorExceptionsExample;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -23,16 +26,17 @@
public class CartController {

private final CreateCartUseCase createCartUseCase;
private final ReadCartLineUseCase readOrderLineUseCase;
private final ReadCartUseCase readCartUseCase;

// @Operation(summary = "μƒν’ˆ 아이디에 닡변을 ν•΄μ•Όν•˜λŠ” μ˜΅μ…˜μ΄ μžˆλŠ”μ§€ ν™•μΈν•©λ‹ˆλ‹€.(μΆ”ν›„ μ•„μ΄ν…œ λ„λ©”μΈμœΌλ‘œ 이전?)")
// @GetMapping("/check/answer")
// public void createCartLines() {
// createOrderLineUseCase.execute(ticketItemId);
// }
@Operation(summary = "μƒν’ˆμ„ μž₯λ°”κ΅¬λ‹ˆμ— λ‹΄μŠ΅λ‹ˆλ‹€. μƒν’ˆμ— λ‹΅λ³€ν•΄μ•Όν•˜λŠ” 응닡이 μžˆλ‹€λ©΄, 응닡도 λ³΄λ‚΄μ£Όμ‹œλ©΄ λ©λ‹ˆλ‹€.")
@ApiErrorExceptionsExample(CreateCartExceptionDocs.class)
@PostMapping
public CreateCartResponse createCartLines(@RequestBody @Valid AddCartRequest addCartRequest) {
public CartResponse createCartLines(@RequestBody @Valid AddCartRequest addCartRequest) {
return createCartUseCase.execute(addCartRequest);
}

Expand Down Expand Up @@ -61,8 +65,8 @@ public CreateCartResponse createCartLines(@RequestBody @Valid AddCartRequest add
// createOrderLineUseCase.execute(ticketItemId);
// }
@Operation(summary = "μ‚¬μš©μžκ°€ μ΅œκ·Όμ— λ§Œλ“€μ—ˆλ˜ μž₯λ°”κ΅¬λ‹ˆλ₯Ό λΆˆλŸ¬μ˜΅λ‹ˆλ‹€. μ—†μœΌλ©΄ data null (κ΅¬ν˜„ μ•ˆν•΄λ„ 됨)")
@PostMapping("/recent")
public void createOrder() {
readOrderLineUseCase.getRecentOrderLine();
@GetMapping("/recent")
public CartResponse getRecentMyCart() {
return readCartUseCase.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package band.gosrock.api.cart.docs;


import band.gosrock.common.annotation.ExceptionDoc;
import band.gosrock.common.annotation.ExplainError;
import band.gosrock.common.exception.DuDoongCodeException;
import band.gosrock.common.interfaces.SwaggerExampleExceptions;
import band.gosrock.domain.domains.cart.exception.CartInvalidItemKindPolicyException;
import band.gosrock.domain.domains.cart.exception.CartInvalidOptionAnswerException;

@ExceptionDoc
public class CreateCartExceptionDocs implements SwaggerExampleExceptions {

@ExplainError("μ•„μ΄ν…œμ— λŒ€ν•œ μ˜΅μ…˜μ— λŒ€ν•œ 응닡을 μ˜¬λ°”λ₯΄κ²Œ μ•ˆν–ˆμ„λ•Œ ( ν•œ μ˜΅μ…˜κ·Έλ£Ήμ— ν•œ 응닡, μ˜΅μ…˜κ·Έλ£Ήμ— μ‘λ‹΅μ•ˆν–ˆμ„λ•Œ)")
public DuDoongCodeException 응닡_μ˜¬λ°”λ₯΄κ²Œ_μ•ˆν–ˆμ„_λ•Œ = CartInvalidOptionAnswerException.EXCEPTION;

@ExplainError("카트λ₯Ό 담을 λ•Œ ν•œ μ•„μ΄ν…œ μ’…λ₯˜ ( ν•œ μ•„μ΄λ””λ‘œλ§Œ λ‹΄μ•„μ£Όμ„Έμš” ), μ •μ±… μœ„λ°˜μž…λ‹ˆλ‹€.")
public DuDoongCodeException ν•œ_μ’…λ₯˜_μ•„μ΄ν…œλ§Œ_μž₯λ°”κ΅¬λ‹ˆμ— = CartInvalidItemKindPolicyException.EXCEPTION;
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
package band.gosrock.api.cart.model.dto.request;


import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import javax.validation.constraints.Min;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class AddCartLineDto {
@Schema(description = "μ£Όλ¬Έν•  μ•„μ΄ν…œ 아이디", defaultValue = "1")
private final Long itemId;
private Long itemId;

@Schema(description = "μƒν’ˆ μˆ˜λŸ‰", defaultValue = "1")
@Min(1)
private final Long quantity;
private Long quantity;

@Schema(description = "μƒν’ˆ κ΄€λ ¨ μ˜΅μ…˜μ— λŒ€ν•œ λ‹΅λ³€")
private List<AddCartOptionAnswerDto> options;

@JsonIgnore
public List<Long> getOptionIds() {
return options.stream().map(AddCartOptionAnswerDto::getOptionId).toList();
}

// public CartLineItem toCartLineItem(Long userId) {
// List<CartOptionAnswer> cartOptionAnswers =
// addCartOptionAnswerDtos.stream()
// .map(AddCartOptionAnswerDto::toCartOptionAnswer)
// .toList();
// return CartLineItem.builder()
// .itemId(itemId)
// .quantity(quantity)
// .cartOptionAnswers(cartOptionAnswers)
// .build();
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,15 @@
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class AddCartOptionAnswerDto {

@Schema(description = "μ˜΅μ…˜ 아이디")
@NotNull
private final Long optionId;
private Long optionId;

@Schema(description = "μ˜΅μ…˜ 그룹에 λŒ€ν•œ 응닡/ T/Fλ©΄ 예,μ•„λ‹ˆμ˜€ ,μ„œμˆ ν˜•μ΄λ©΄ μ„œμˆ μ‘λ‹΅", defaultValue = "λ„€")
@NotBlank
private final String answer;

// public CartOptionAnswer toCartOptionAnswer(Option option, OptionGroup optionGroup) {
// return CartOptionAnswer.builder()
// .option(option)
// .optionGroup(optionGroup)
// .build();
// }
private String answer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class AddCartRequest {

@Schema(description = "μƒν’ˆμ— μ˜΅μ…˜μ΄ μžˆμ„μ‹œμ— 각기 λ‹΅λ³€λ§ˆλ‹€ μ—¬λŸ¬κ°œλ₯Ό λ³΄λ‚΄μ£Όμ‹œλ©΄λ©λ‹ˆλ‹€. ν•œλ²ˆμ— λ‹΅λ³€ν•˜κΈ°λ©΄ ν•˜λ‚˜μ— quantity λ₯Ό 늘리면 λ©λ‹ˆλ‹€.")
private List<AddCartLineDto> items;

// public List<CartLineItem> getCartLines(Long userId) {
// return addCartLineDtos.stream()
// .map(addCartLineDto -> addCartLineDto.toCartLineItem(userId))
// .toList();
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Getter
@Builder
public class CreateCartResponse {
public class CartResponse {
@Schema(description = "μž₯λ°”κ΅¬λ‹ˆλͺ… μž…λ‹ˆλ‹€.", defaultValue = "")
private final String title;
// λ‚΄ν‹°μΌ“ ν™•μΈν•˜κΈ°
Expand All @@ -29,8 +29,8 @@ public class CreateCartResponse {
@Schema(description = "κ²°μ œκ°€ ν•„μš”ν•œμ§€μ— λŒ€ν•œ μ—¬λΆ€λ₯Ό κ²°μ •ν•©λ‹ˆλ‹€. ν•„μš”ν•œ trueλ©΄ 결제창 λ„μš°μ‹œλ©΄λ©λ‹ˆλ‹€.", defaultValue = "true")
private final Boolean isNeedPayment;

public static CreateCartResponse of(List<CartItemResponse> cartItemResponses, Cart cart) {
return CreateCartResponse.builder()
public static CartResponse of(List<CartItemResponse> cartItemResponses, Cart cart) {
return CartResponse.builder()
.items(cartItemResponses)
.totalPrice(cart.getTotalPrice())
.cartId(cart.getId())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package band.gosrock.api.cart.model.mapper;


import band.gosrock.api.cart.model.dto.request.AddCartLineDto;
import band.gosrock.api.cart.model.dto.request.AddCartOptionAnswerDto;
import band.gosrock.api.cart.model.dto.request.AddCartRequest;
import band.gosrock.api.cart.model.dto.response.CartItemResponse;
import band.gosrock.api.cart.model.dto.response.CartResponse;
import band.gosrock.common.annotation.Mapper;
import band.gosrock.domain.domains.cart.adaptor.CartAdaptor;
import band.gosrock.domain.domains.cart.domain.Cart;
import band.gosrock.domain.domains.cart.domain.CartLineItem;
import band.gosrock.domain.domains.cart.domain.CartOptionAnswer;
import band.gosrock.domain.domains.ticket_item.adaptor.OptionAdaptor;
import band.gosrock.domain.domains.ticket_item.adaptor.TicketItemAdaptor;
import band.gosrock.domain.domains.ticket_item.domain.TicketItem;
import band.gosrock.domain.domains.ticket_item.repository.OptionRepository;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.transaction.annotation.Transactional;

@Mapper
@RequiredArgsConstructor
public class CartMapper {
private final TicketItemAdaptor ticketItemAdaptor;
private final OptionAdaptor optionAdaptor;
private final OptionRepository optionRepository;

private final CartAdaptor cartAdaptor;

@Transactional(readOnly = true)
public CartResponse toCartResponse(Long cartId) {
Cart cart = cartAdaptor.queryCart(cartId);
return getCartResponse(cart);
}

@Transactional(readOnly = true)
public CartResponse toCartResponse(Cart cart) {
return getCartResponse(cart);
}

private CartResponse getCartResponse(Cart cart) {
List<CartLineItem> newCartLineItems = cart.getCartLineItems();
Long totalQuantity = cart.getTotalQuantity();

List<CartItemResponse> cartItemResponses =
getCartItemResponses(newCartLineItems, totalQuantity);

return CartResponse.of(cartItemResponses, cart);
}

private List<CartItemResponse> getCartItemResponses(
List<CartLineItem> newCartLineItems, Long totalQuantity) {
int startNum = 1;
List<CartItemResponse> cartItemResponses = new ArrayList<>();
for (CartLineItem cartLineItem : newCartLineItems) {
cartItemResponses.add(
CartItemResponse.of(
generateCartLineName(cartLineItem, startNum, totalQuantity),
cartLineItem));
startNum += cartLineItem.getQuantity();
}
return cartItemResponses;
}

public Cart toEntity(AddCartRequest addCartRequest, Long currentUserId) {
List<AddCartLineDto> addCartLineDtos = addCartRequest.getItems();

List<CartLineItem> cartLineItems =
addCartLineDtos.stream()
.map(
addCartLineDto ->
CartLineItem.builder()
.ticketItem(getTicketItem(addCartLineDto))
.cartOptionAnswers(
getCartOptionAnswers(addCartLineDto))
.quantity(addCartLineDto.getQuantity())
.build())
.toList();
return Cart.builder().cartLineItems(cartLineItems).userId(currentUserId).build();
}

@NotNull
private TicketItem getTicketItem(AddCartLineDto addCartLineDto) {
return ticketItemAdaptor.queryTicketItem(addCartLineDto.getItemId());
}

/**
* μΌλ°˜ν‹°μΌ“(1/3) - 4000원 κ³Ό 같은 μž₯λ°”κ΅¬λ‹ˆ μƒν’ˆμ˜ 응닡을 λ°˜ν™˜ν•©λ‹ˆλ‹€ 카트라인 κΈ°μ€€μž…λ‹ˆλ‹€. μΉ΄νŠΈλΌμΈμ— 3개의 μƒν’ˆμ΄ 같이 λ‹΄κΈ°λ©΄ ( μ˜΅μ…˜μ΄ 같은 상황 )
* μΌλ°˜ν‹°μΌ“(1-3/3) μ΄λŸ°μ‹μœΌλ‘œ ν‘œν˜„λ©λ‹ˆλ‹€.
*
* @param cartLineItem
* @param startNum
* @param totalQuantity
* @return
*/
private String generateCartLineName(
CartLineItem cartLineItem, int startNum, Long totalQuantity) {
Long cartLineQuantity = cartLineItem.getQuantity();
if (cartLineQuantity.equals(1L)) {
return String.format(
"%s (%s/%d) - %s",
cartLineItem.getTicketName(),
startNum,
totalQuantity,
cartLineItem.getTotalCartLinePrice().toString());
}
int endNum = (int) (cartLineQuantity - 1 + startNum);
return String.format(
"%s (%s/%d) - %s",
cartLineItem.getTicketName(),
startNum + "-" + endNum,
totalQuantity,
cartLineItem.getTotalCartLinePrice().toString());
}

private List<CartOptionAnswer> getCartOptionAnswers(AddCartLineDto addCartLineDto) {
return addCartLineDto.getOptions().stream().map(this::getCartOptionAnswer).toList();
}

private CartOptionAnswer getCartOptionAnswer(AddCartOptionAnswerDto addCartOptionAnswerDto) {
return CartOptionAnswer.builder()
.option(optionAdaptor.queryOption(addCartOptionAnswerDto.getOptionId()))
.answer(addCartOptionAnswerDto.getAnswer())
.build();
}
}
Loading

0 comments on commit 9a4a1cf

Please sign in to comment.