Skip to content

Commit

Permalink
Nueva API "/shopping-lists/{id}/products-selected"
Browse files Browse the repository at this point in the history
- Actualización del estado de selección de los productos de una lista de la compra.
- Se agregó la anotación "@transactional" (necesaria) para que la actualización funcione correctamente.
  • Loading branch information
nmarulo committed Dec 11, 2024
1 parent 6b3a8cc commit 7fdc408
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,4 +26,9 @@ List<ProductHasShoppingList> findAllByShoppingList_IdAndShoppingList_UserAndProd
List<Long> productsId,
List<Long> 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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@ public ResponseEntity<FindByIdProductListRes> findAllProducts(@PathVariable Long
pageable));
}

@PutMapping("/{id}/products-selected")
public ResponseEntity<Void> productsSelected(@PathVariable Long id, @RequestBody ProductsSelectedReq request) {
this.shoppingListService.productsSelected(id, request, this.dataRequestScope.getAuthenticationPrincipal());

return ResponseEntity.noContent()
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface ShoppingListRepository extends JpaRepository<ShoppingList, Long

Optional<ShoppingList> findByIdAndUser(Long id, User user);

boolean existsByIdAndUser(Long id, User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -203,4 +204,19 @@ private List<SelectOption<SelectedProducts, String>> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit 7fdc408

Please sign in to comment.