Skip to content

Commit

Permalink
Modificada clase "ShoppingListService"
Browse files Browse the repository at this point in the history
- Método "getShoppingList" para extraer la consulta y comprobación de la lista de la compra.
  • Loading branch information
nmarulo committed Nov 26, 2024
1 parent 81d5d18 commit 8e11680
Showing 1 changed file with 16 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ public FindByIdShoppingListRes findById(final Long id,
final User user,
final FindByIdProductListReq request,
final Pageable pageable) {
var findById = this.shoppingListRepository.findByIdAndUser(id, user);

if (findById.isEmpty()) {
throw new NotFoundException(getLocalMessage().getMessage("error.record-not-exist"));
}

final var shoppingList = findById.get();
final var shoppingList = getShoppingList(id, user);
final var productListPage = findAllProductList(shoppingList, user, request, pageable);
final var response = ShoppingListMapper.toFindByIdShoppingListRes(shoppingList);
final var totalUnitsPerProducts = IntegerUtil.newAtomicReference(response.getTotalUnitsPerProducts());
Expand Down Expand Up @@ -86,14 +80,7 @@ public FindByIdProductShoppingListRest findByIdProduct(Integer id, Long productI
}

public void deleteProducts(Long id, DeleteProductsShoppingListReq request, User user) {
var shoppingListOptional = this.shoppingListRepository.findByIdAndUser(id, user);

if (shoppingListOptional.isEmpty()) {
throw new NotFoundException(getLocalMessage().getMessage("error.record-not-exist"));
}

var shoppingList = shoppingListOptional.get();

final var shoppingList = getShoppingList(id, user);
var productsShoppingList = this.productHasShoppingListRepository.findAllByShoppingListIdAndUserAndProductIdIn(id,
user,
request.getProductsId());
Expand Down Expand Up @@ -126,16 +113,10 @@ public SaveShoppingListRes save(SaveShoppingListReq request, User user) {
}

public UpdateShoppingListRes update(Long id, UpdateShoppingListReq request, User user) {
var shoppingListOptional = this.shoppingListRepository.findByIdAndUser(id, user);

if (shoppingListOptional.isEmpty()) {
throw new NotFoundException(getLocalMessage().getMessage("error.record-not-exist"));
}
final var shoppingList = getShoppingList(id, user);

updateProducts(id, request.getProducts(), user);

var shoppingList = shoppingListOptional.get();

shoppingList.setName(request.getName());

var update = this.shoppingListRepository.save(shoppingList);
Expand All @@ -144,13 +125,7 @@ public UpdateShoppingListRes update(Long id, UpdateShoppingListReq request, User
}

public void delete(Long id, User user) {
var shoppingListOptional = this.shoppingListRepository.findByIdAndUser(id, user);

if (shoppingListOptional.isEmpty()) {
throw new NotFoundException(getLocalMessage().getMessage("error.record-not-exist"));
}

var shoppingList = shoppingListOptional.get();
final var shoppingList = getShoppingList(id, user);

this.productHasShoppingListRepository.deleteAll(shoppingList.getProductHasShoppingList());
this.shoppingListRepository.delete(shoppingList);
Expand All @@ -160,13 +135,9 @@ public FindByIdProductListRes findByIdProductList(final Long id,
final User user,
final FindByIdProductListReq request,
final Pageable pageable) {
var shoppingListOptional = this.shoppingListRepository.findByIdAndUser(id, user);

if (shoppingListOptional.isEmpty()) {
throw new NotFoundException(getLocalMessage().getMessage("error.record-not-exist"));
}
final var shoppingList = getShoppingList(id, user);

return findAllProductList(shoppingListOptional.get(), user, request, pageable);
return findAllProductList(shoppingList, user, request, pageable);
}

private FindByIdProductListRes findAllProductList(final ShoppingList shoppingList,
Expand Down Expand Up @@ -211,4 +182,14 @@ private void updateProducts(Long shoppingListId,
});
}

private ShoppingList getShoppingList(final Long id, final User user) {
final var shoppingListOptional = this.shoppingListRepository.findByIdAndUser(id, user);

if (shoppingListOptional.isEmpty()) {
throw new NotFoundException(getLocalMessage().getMessage("error.record-not-exist"));
}

return shoppingListOptional.get();
}

}

0 comments on commit 8e11680

Please sign in to comment.