diff --git a/src/main/java/dev/nmarulo/despensaapp/app/productshoppinglist/ProductHasShoppingListRepository.java b/src/main/java/dev/nmarulo/despensaapp/app/productshoppinglist/ProductHasShoppingListRepository.java index bf117c7..3561cba 100644 --- a/src/main/java/dev/nmarulo/despensaapp/app/productshoppinglist/ProductHasShoppingListRepository.java +++ b/src/main/java/dev/nmarulo/despensaapp/app/productshoppinglist/ProductHasShoppingListRepository.java @@ -3,6 +3,8 @@ import dev.nmarulo.despensaapp.app.users.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; @@ -24,4 +26,9 @@ List findAllByShoppingList_IdAndShoppingList_UserAndProd List productsId, List unitTypesId); + @Modifying + @Query( + "UPDATE ProductHasShoppingList phsl SET phsl.selected = :selected WHERE phsl.shoppingList.id = :shoppingListId AND phsl.shoppingList.user = :user") + void updateSelectedByShoppingList_IdShoppingList_User(boolean selected, Long shoppingListId, User user); + } diff --git a/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListController.java b/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListController.java index b278bb9..fb266fd 100644 --- a/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListController.java +++ b/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListController.java @@ -83,4 +83,12 @@ public ResponseEntity findAllProducts(@PathVariable Long pageable)); } + @PutMapping("/{id}/products-selected") + public ResponseEntity productsSelected(@PathVariable Long id, @RequestBody ProductsSelectedReq request) { + this.shoppingListService.productsSelected(id, request, this.dataRequestScope.getAuthenticationPrincipal()); + + return ResponseEntity.noContent() + .build(); + } + } diff --git a/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListRepository.java b/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListRepository.java index 47cb92f..eb09a8d 100644 --- a/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListRepository.java +++ b/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListRepository.java @@ -15,4 +15,6 @@ public interface ShoppingListRepository extends JpaRepository findByIdAndUser(Long id, User user); + boolean existsByIdAndUser(Long id, User user); + } diff --git a/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListService.java b/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListService.java index 51466ed..bda6a78 100644 --- a/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListService.java +++ b/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/ShoppingListService.java @@ -16,6 +16,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.List; @@ -203,4 +204,19 @@ private List> getSelectOptionList(FindByI new SelectOption<>(SelectedProducts.ALL, "Todos", SelectedProducts.ALL == selected)); } + @Transactional + public void productsSelected(Long id, ProductsSelectedReq request, User user) { + if (request.getAction() == null) { + return; + } + + if (!this.shoppingListRepository.existsByIdAndUser(id, user)) { + throw new NotFoundException(getLocalMessage().getMessage("error.record-not-exist")); + } + + final var select = ProductsSelectedReq.ActionType.SELECT == request.getAction(); + + this.productHasShoppingListRepository.updateSelectedByShoppingList_IdShoppingList_User(select, id, user); + } + } diff --git a/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/dtos/ProductsSelectedReq.java b/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/dtos/ProductsSelectedReq.java new file mode 100644 index 0000000..86148f4 --- /dev/null +++ b/src/main/java/dev/nmarulo/despensaapp/app/shoppinglist/dtos/ProductsSelectedReq.java @@ -0,0 +1,15 @@ +package dev.nmarulo.despensaapp.app.shoppinglist.dtos; + +import lombok.Data; + +@Data +public class ProductsSelectedReq { + + private ActionType action; + + public enum ActionType { + SELECT, + DESELECT + } + +}