Skip to content

Commit

Permalink
feat: moving manipulations to mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli authored Oct 6, 2023
1 parent 672ccf9 commit d13a3dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ Licensed under MIT License Copyright (c) 2021-2023 Raja Kolli.
import com.example.orderservice.model.request.OrderRequest;
import com.example.orderservice.model.response.OrderItemResponse;
import com.example.orderservice.model.response.OrderResponse;
import java.math.RoundingMode;
import org.mapstruct.AfterMapping;
import org.mapstruct.DecoratedWith;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValueCheckStrategy;

@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
@Mapper(
componentModel = "spring",
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
imports = RoundingMode.class)
@DecoratedWith(OrderMapperDecorator.class)
public interface OrderMapper {

Expand Down Expand Up @@ -68,9 +72,17 @@ default void addOrderItemRequestToOrderEntity(
@Mapping(target = "lastModifiedDate", ignore = true)
void updateOrderFromOrderRequest(OrderRequest orderRequest, @MappingTarget Order order);

@Mapping(
target = "totalPrice",
expression =
"java(items.stream().map(OrderItemResponse::price).reduce(BigDecimal.ZERO, BigDecimal::add))")
@Mapping(source = "id", target = "orderId")
OrderResponse toResponse(Order order);

@Mapping(
target = "price",
expression =
"java(productPrice.multiply(new BigDecimal(quantity)).setScale(2, RoundingMode.HALF_UP))")
@Mapping(target = "itemId", source = "id")
@Mapping(target = "productId", source = "productCode")
OrderItemResponse orderItemToOrderItemResponse(OrderItem orderItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli.
package com.example.orderservice.model.response;

import java.math.BigDecimal;
import java.math.RoundingMode;

public record OrderItemResponse(
Long itemId, String productId, int quantity, BigDecimal productPrice, BigDecimal price) {

public OrderItemResponse(
Long itemId,
String productId,
int quantity,
BigDecimal productPrice,
BigDecimal price) {
this.itemId = itemId;
this.productId = productId;
this.quantity = quantity;
this.productPrice = productPrice;
this.price =
productPrice.multiply(new BigDecimal(quantity)).setScale(2, RoundingMode.HALF_UP);
}
}
Long itemId, String productId, int quantity, BigDecimal productPrice, BigDecimal price) {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,4 @@ public record OrderResponse(
String source,
LocalDateTime createdDate,
BigDecimal totalPrice,
List<OrderItemResponse> items) {

public OrderResponse(
Long orderId,
Long customerId,
String status,
String source,
LocalDateTime createdDate,
BigDecimal totalPrice,
List<OrderItemResponse> items) {
this.orderId = orderId;
this.customerId = customerId;
this.status = status;
this.source = source;
this.createdDate = createdDate;
this.totalPrice =
items.stream()
.map(OrderItemResponse::price)
.reduce(BigDecimal.ZERO, BigDecimal::add);
this.items = items;
}
}
List<OrderItemResponse> items) {}

0 comments on commit d13a3dc

Please sign in to comment.