Skip to content

Commit 8ff969f

Browse files
committed
API GET /shopping-lists/{id}
- Ahora retorna nuevos campos. (total de unidades por producto, total de productos seleccionados y precio total de productos seleccionados)
1 parent 2dbdafc commit 8ff969f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListService.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import dev.nmarulo.depensaapp.app.users.UserRepository;
88
import dev.nmarulo.depensaapp.commons.exception.NotFoundException;
99
import dev.nmarulo.depensaapp.commons.service.BasicServiceImp;
10+
import dev.nmarulo.depensaapp.commons.util.BigDecimalUtil;
11+
import dev.nmarulo.depensaapp.commons.util.IntegerUtil;
1012
import lombok.Getter;
1113
import lombok.RequiredArgsConstructor;
1214
import org.apache.commons.lang3.StringUtils;
@@ -41,7 +43,27 @@ public FindByIdShoppingListRes findById(Long id, User user) {
4143
throw new NotFoundException(getLocalMessage().getMessage("error.record-not-exist"));
4244
}
4345

44-
return ShoppingListMapper.toFindByIdShoppingListRes(findById.get());
46+
final var shoppingList = findById.get();
47+
final var response = ShoppingListMapper.toFindByIdShoppingListRes(shoppingList);
48+
final var totalUnitsPerProducts = IntegerUtil.newAtomicReference(response.getTotalUnitsPerProducts());
49+
final var totalSelectedProducts = IntegerUtil.newAtomicReference(response.getTotalSelectedProducts());
50+
final var totalPriceSelectedProducts = BigDecimalUtil.newAtomicReference(response.getTotalPriceSelectedProducts());
51+
52+
shoppingList.getProductHasShoppingList()
53+
.forEach(value -> {
54+
if (value.isSelected()) {
55+
totalSelectedProducts.accumulateAndGet(1, Integer::sum);
56+
totalPriceSelectedProducts.accumulateAndGet(value.getTotalPrice(), BigDecimal::add);
57+
}
58+
59+
totalUnitsPerProducts.accumulateAndGet(value.getUnitsPerProduct(), Integer::sum);
60+
});
61+
62+
response.setTotalUnitsPerProducts(totalUnitsPerProducts.get());
63+
response.setTotalSelectedProducts(totalSelectedProducts.get());
64+
response.setTotalPriceSelectedProducts(totalPriceSelectedProducts.get());
65+
66+
return response;
4567
}
4668

4769
public FindByIdProductShoppingListRest findByIdProduct(Integer id, Long productId, User user) {

src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/dtos/FindByIdShoppingListRes.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public class FindByIdShoppingListRes {
1616

1717
private BigDecimal totalPrice;
1818

19+
private Integer totalUnitsPerProducts;
20+
21+
private Integer totalSelectedProducts;
22+
23+
private BigDecimal totalPriceSelectedProducts;
24+
1925
private List<ProductShoppingList> products;
2026

2127
@Data

0 commit comments

Comments
 (0)