diff --git a/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListService.java b/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListService.java index b9cc1bb..619d164 100644 --- a/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListService.java +++ b/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListService.java @@ -7,6 +7,8 @@ import dev.nmarulo.depensaapp.app.users.UserRepository; import dev.nmarulo.depensaapp.commons.exception.NotFoundException; import dev.nmarulo.depensaapp.commons.service.BasicServiceImp; +import dev.nmarulo.depensaapp.commons.util.BigDecimalUtil; +import dev.nmarulo.depensaapp.commons.util.IntegerUtil; import lombok.Getter; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; @@ -41,7 +43,27 @@ public FindByIdShoppingListRes findById(Long id, User user) { throw new NotFoundException(getLocalMessage().getMessage("error.record-not-exist")); } - return ShoppingListMapper.toFindByIdShoppingListRes(findById.get()); + final var shoppingList = findById.get(); + final var response = ShoppingListMapper.toFindByIdShoppingListRes(shoppingList); + final var totalUnitsPerProducts = IntegerUtil.newAtomicReference(response.getTotalUnitsPerProducts()); + final var totalSelectedProducts = IntegerUtil.newAtomicReference(response.getTotalSelectedProducts()); + final var totalPriceSelectedProducts = BigDecimalUtil.newAtomicReference(response.getTotalPriceSelectedProducts()); + + shoppingList.getProductHasShoppingList() + .forEach(value -> { + if (value.isSelected()) { + totalSelectedProducts.accumulateAndGet(1, Integer::sum); + totalPriceSelectedProducts.accumulateAndGet(value.getTotalPrice(), BigDecimal::add); + } + + totalUnitsPerProducts.accumulateAndGet(value.getUnitsPerProduct(), Integer::sum); + }); + + response.setTotalUnitsPerProducts(totalUnitsPerProducts.get()); + response.setTotalSelectedProducts(totalSelectedProducts.get()); + response.setTotalPriceSelectedProducts(totalPriceSelectedProducts.get()); + + return response; } public FindByIdProductShoppingListRest findByIdProduct(Integer id, Long productId, User user) { diff --git a/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/dtos/FindByIdShoppingListRes.java b/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/dtos/FindByIdShoppingListRes.java index 413c730..18692a1 100644 --- a/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/dtos/FindByIdShoppingListRes.java +++ b/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/dtos/FindByIdShoppingListRes.java @@ -16,6 +16,12 @@ public class FindByIdShoppingListRes { private BigDecimal totalPrice; + private Integer totalUnitsPerProducts; + + private Integer totalSelectedProducts; + + private BigDecimal totalPriceSelectedProducts; + private List products; @Data