Skip to content

Commit

Permalink
API GET /shopping-lists/{id}
Browse files Browse the repository at this point in the history
- Ahora retorna nuevos campos. (total de unidades por producto, total de productos seleccionados y precio total de productos seleccionados)
  • Loading branch information
nmarulo committed Nov 21, 2024
1 parent 2dbdafc commit 8ff969f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class FindByIdShoppingListRes {

private BigDecimal totalPrice;

private Integer totalUnitsPerProducts;

private Integer totalSelectedProducts;

private BigDecimal totalPriceSelectedProducts;

private List<ProductShoppingList> products;

@Data
Expand Down

0 comments on commit 8ff969f

Please sign in to comment.