Skip to content

Commit

Permalink
Refactor: Refactor to use mapstruct
Browse files Browse the repository at this point in the history
  • Loading branch information
hnd14 committed Sep 11, 2024
1 parent 56fda46 commit 99558b5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 49 deletions.
30 changes: 30 additions & 0 deletions order/src/main/java/com/yas/order/mapper/CheckoutMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.yas.order.mapper;

import com.yas.order.model.Checkout;
import com.yas.order.model.CheckoutItem;
import com.yas.order.viewmodel.checkout.CheckoutItemPostVm;
import com.yas.order.viewmodel.checkout.CheckoutItemVm;
import com.yas.order.viewmodel.checkout.CheckoutPostVm;
import com.yas.order.viewmodel.checkout.CheckoutVm;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.stereotype.Component;

@Mapper(componentModel = "spring")
@Component
public interface CheckoutMapper {
@Mapping(target = "id", ignore = true)
@Mapping(target = "checkoutId", ignore = true)
CheckoutItem toModel(CheckoutItemPostVm checkoutItemPostVm);

@Mapping(target = "id", ignore = true)
@Mapping(target = "checkoutState", ignore = true)
@Mapping(target = "checkoutItem", source = "checkoutItemPostVms")
Checkout toModel(CheckoutPostVm checkoutPostVm);

@Mapping(target = "checkoutId", source = "checkoutId.id")
CheckoutItemVm toVm(CheckoutItem checkoutItem);

@Mapping(target = "checkoutItemVms", source = "checkoutItem")
CheckoutVm toVm(Checkout checkout);
}
42 changes: 8 additions & 34 deletions order/src/main/java/com/yas/order/service/CheckoutService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

import com.yas.order.exception.Forbidden;
import com.yas.order.exception.NotFoundException;
import com.yas.order.mapper.CheckoutMapper;
import com.yas.order.model.Checkout;
import com.yas.order.model.CheckoutItem;
import com.yas.order.model.Order;
import com.yas.order.model.enumeration.CheckoutState;
import com.yas.order.repository.CheckoutItemRepository;
import com.yas.order.repository.CheckoutRepository;
import com.yas.order.utils.AuthenticationUtils;
import com.yas.order.utils.Constants;
import com.yas.order.viewmodel.checkout.CheckoutPostVm;
import com.yas.order.viewmodel.checkout.CheckoutStatusPutVm;
import com.yas.order.viewmodel.checkout.CheckoutVm;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -29,38 +26,15 @@
@RequiredArgsConstructor
public class CheckoutService {
private final CheckoutRepository checkoutRepository;
private final CheckoutItemRepository checkoutItemRepository;
private final OrderService orderService;
private final CheckoutMapper checkoutMapper;

public CheckoutVm createCheckout(CheckoutPostVm checkoutPostVm) {
UUID uuid = UUID.randomUUID();
Checkout checkout = Checkout.builder()
.id(uuid.toString())
.email(checkoutPostVm.email())
.note(checkoutPostVm.note())
.couponCode(checkoutPostVm.couponCode())
.checkoutState(CheckoutState.PENDING)
.build();
checkoutRepository.save(checkout);

Set<CheckoutItem> checkoutItems = checkoutPostVm.checkoutItemPostVms().stream()
.map(item -> CheckoutItem.builder()
.productId(item.productId())
.productName(item.productName())
.quantity(item.quantity())
.productPrice(item.productPrice())
.note(item.note())
.discountAmount(item.discountAmount())
.taxPercent(item.taxPercent())
.taxAmount(item.taxAmount())
.checkoutId(checkout)
.build())
.collect(Collectors.toSet());
checkoutItemRepository.saveAll(checkoutItems);

//setCheckoutItem so that we able to return checkout with checkoutItems
checkout.setCheckoutItem(checkoutItems);
return CheckoutVm.fromModel(checkout);
Checkout checkout = checkoutMapper.toModel(checkoutPostVm);
checkout.setCheckoutState(CheckoutState.PENDING);
checkout.setId(UUID.randomUUID().toString());
checkout.getCheckoutItem().forEach(item -> item.setCheckoutId(checkout));
return checkoutMapper.toVm(checkoutRepository.save(checkout));
}

public CheckoutVm getCheckoutPendingStateWithItemsById(String id) {
Expand All @@ -70,7 +44,7 @@ public CheckoutVm getCheckoutPendingStateWithItemsById(String id) {
if (!checkout.getCreatedBy().equals(AuthenticationUtils.getCurrentUserId())) {
throw new Forbidden(Constants.ErrorCode.FORBIDDEN);
}
return CheckoutVm.fromModel(checkout);
return checkoutMapper.toVm(checkout);
}

public Long updateCheckoutStatus(CheckoutStatusPutVm checkoutStatusPutVm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ public record CheckoutPostVm(
String couponCode,
@NotNull
List<CheckoutItemPostVm> checkoutItemPostVms

) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,12 @@
import java.util.stream.Collectors;
import lombok.Builder;

@Builder
@Builder(toBuilder = true)
public record CheckoutVm(
String id,
String email,
String note,
String couponCode,
Set<CheckoutItemVm> checkoutItemVms

) {
public static CheckoutVm fromModel(Checkout checkout) {
Set<CheckoutItemVm> checkoutItemVms = checkout.getCheckoutItem().stream().map(
item -> CheckoutItemVm.fromModel(item))
.collect(Collectors.toSet());
return CheckoutVm.builder()
.id(checkout.getId())
.email(checkout.getEmail())
.note(checkout.getNote())
.couponCode(checkout.getCouponCode())
.checkoutItemVms(checkoutItemVms)
.build();
}
}

0 comments on commit 99558b5

Please sign in to comment.