diff --git a/src/main/java/com/consola/lis/controller/InventoryController.java b/src/main/java/com/consola/lis/controller/InventoryController.java index 0e7ac49..2ef1bff 100644 --- a/src/main/java/com/consola/lis/controller/InventoryController.java +++ b/src/main/java/com/consola/lis/controller/InventoryController.java @@ -11,6 +11,8 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; + +import org.springframework.data.domain.Pageable; import java.util.List; import java.util.Map; @@ -23,7 +25,7 @@ public class InventoryController { private final InventoryItemService inventoryItemService; @ResponseStatus(HttpStatus.CREATED) - @PostMapping(EndpointConstant.ENDPOINT_INVENTORY_ITEM ) + @PostMapping() public ResponseEntity createInventoryItem(@RequestBody InventoryItemDTO inventoryItemDTO) throws JsonProcessingException { InventoryItem createdGeneralItem = inventoryItemService.createInventoryItem(inventoryItemDTO); return ResponseEntity.ok(createdGeneralItem); @@ -42,8 +44,8 @@ public void deleteItem(@PathVariable("itemId") String itemId){ @GetMapping(EndpointConstant.ENDPOINT_INVENTORY_TABLE) - public Map inventoryItems() { - return inventoryItemService.getAllItemsMapped(); + public Map inventoryItems(Pageable pageable) { + return inventoryItemService.getAllItemsMapped(pageable); } @GetMapping(EndpointConstant.ENDPOINT_ONE_ITEM) @@ -64,6 +66,11 @@ public ResponseEntity updateInventoryItemQuantity(@PathVariable(" } + @GetMapping(EndpointConstant.ENDPOINT_HEADERS_ITEM) + public List getHeaders(){ + return inventoryItemService.getHeaders(); + } + } diff --git a/src/main/java/com/consola/lis/controller/LoanController.java b/src/main/java/com/consola/lis/controller/LoanController.java index 44db1c4..7cd3447 100644 --- a/src/main/java/com/consola/lis/controller/LoanController.java +++ b/src/main/java/com/consola/lis/controller/LoanController.java @@ -7,6 +7,7 @@ import com.consola.lis.util.constans.EndpointConstant; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -45,7 +46,12 @@ public List gelAllLoans(){ } @GetMapping(EndpointConstant.ENDPOINT_ALL_LOANS_TABLE) - public Map loans(){ - return loanService.getAllLoansMapper(); + public Map loans(Pageable pageable){ + return loanService.getAllLoansMapper(pageable); + } + + @GetMapping(EndpointConstant.ENDPOINT_HEADERS_LOAN) + public List getHeaders(){ + return loanService.getHeaders(); } } diff --git a/src/main/java/com/consola/lis/dto/ReturnLoanDTO.java b/src/main/java/com/consola/lis/dto/ReturnLoanDTO.java index dad7139..cbc73b8 100644 --- a/src/main/java/com/consola/lis/dto/ReturnLoanDTO.java +++ b/src/main/java/com/consola/lis/dto/ReturnLoanDTO.java @@ -11,7 +11,6 @@ @NoArgsConstructor public class ReturnLoanDTO { private int loanId; - private String borrowerUser; private String lenderUser; private String observation; diff --git a/src/main/java/com/consola/lis/model/repository/InventoryItemRepository.java b/src/main/java/com/consola/lis/model/repository/InventoryItemRepository.java index b39c272..c69b454 100644 --- a/src/main/java/com/consola/lis/model/repository/InventoryItemRepository.java +++ b/src/main/java/com/consola/lis/model/repository/InventoryItemRepository.java @@ -1,9 +1,17 @@ package com.consola.lis.model.repository; import com.consola.lis.model.entity.InventoryItem; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; + @Repository public interface InventoryItemRepository extends JpaRepository { -} + + @Query("SELECT p FROM InventoryItem p ") + Page findAllItems(Pageable pageable); +} \ No newline at end of file diff --git a/src/main/java/com/consola/lis/model/repository/LoanRepository.java b/src/main/java/com/consola/lis/model/repository/LoanRepository.java index 133be1e..8f8d56a 100644 --- a/src/main/java/com/consola/lis/model/repository/LoanRepository.java +++ b/src/main/java/com/consola/lis/model/repository/LoanRepository.java @@ -1,11 +1,18 @@ package com.consola.lis.model.repository; +import com.consola.lis.model.entity.InventoryItem; import com.consola.lis.model.entity.Loan; import com.consola.lis.model.enums.LoanState; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import java.util.List; public interface LoanRepository extends JpaRepository { List findByLoanState(LoanState loanState); + + @Query("SELECT p FROM Loan p ") + Page findAllLoans(Pageable pageable); } diff --git a/src/main/java/com/consola/lis/service/InventoryItemService.java b/src/main/java/com/consola/lis/service/InventoryItemService.java index 6c1f812..bcaa50c 100644 --- a/src/main/java/com/consola/lis/service/InventoryItemService.java +++ b/src/main/java/com/consola/lis/service/InventoryItemService.java @@ -2,8 +2,6 @@ import com.consola.lis.dto.ItemInfoDTO; -import com.consola.lis.dto.LoanDTO; -import com.consola.lis.model.entity.Loan; import com.consola.lis.util.constans.Util; import com.consola.lis.dto.InventoryItemDTO; import com.consola.lis.util.exception.AlreadyExistsException; @@ -19,10 +17,13 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.transaction.Transactional; +import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import org.springframework.data.domain.Pageable; import java.util.*; +import java.util.stream.Collectors; @Service @@ -61,8 +62,8 @@ public InventoryItem createInventoryItem(InventoryItemDTO inventoryItemRequest) validateCategoryExists(inventoryItemRequest.getCategoryId()); Category category = categoryRepository.findCategoryById(inventoryItemRequest.getCategoryId()); - boolean isQuantizable = category.getQuantizable() !=null ? category.getQuantizable() :false; - boolean isLendable = inventoryItemRequest.getLendable() !=null ? inventoryItemRequest.getLendable() : false; + boolean isQuantizable = category.getQuantizable() != null ? category.getQuantizable() : false; + boolean isLendable = inventoryItemRequest.getLendable() != null ? inventoryItemRequest.getLendable() : false; if (existItem(inventoryItemRequest.getItemId())) { @@ -74,7 +75,7 @@ public InventoryItem createInventoryItem(InventoryItemDTO inventoryItemRequest) if (inventoryItemRequest.getWallet() == null || inventoryItemRequest.getWallet().name().trim().isEmpty()) { inventoryItemRequest.setWallet(WalletOwners.NOT_APPLY); } - + InventoryItem inventoryItem = createNewItem(inventoryItemRequest, attributesJson); return inventoryItemRepository.save(inventoryItem); @@ -82,7 +83,7 @@ public InventoryItem createInventoryItem(InventoryItemDTO inventoryItemRequest) } private InventoryItem createNewItem(InventoryItemDTO inventoryItemRequest, String attributesJson) { - return InventoryItem.builder() + return InventoryItem.builder() .itemId(inventoryItemRequest.getItemId()) .categoryId(inventoryItemRequest.getCategoryId()) .wallet(WalletOwners.valueOf(inventoryItemRequest.getWallet().name())) @@ -140,15 +141,28 @@ public boolean existItem(String itemId) { } - public Map getAllItemsMapped() { - List inventoryItems = getAllItems(); + public Map getAllItemsMapped(Pageable pageable) { + Page inventoryItemsPage = getAllInventoryItems(pageable); + Map result = new HashMap<>(); - result.put("header", createHeader()); - result.put("allRegisters", inventoryItems); + result.put("totalElements", inventoryItemsPage.getTotalElements()); + result.put("totalPages", inventoryItemsPage.getTotalPages()); + result.put("currentPage", inventoryItemsPage.getNumber()); + result.put("items", mapToItemInfoList(inventoryItemsPage.getContent())); + return result; } - private List createHeader() { + private List mapToItemInfoList(List items) { + return items.stream() + .map(item -> InventoryItemMapper.mapToItemInfo(item, findCategory(item))) + .toList(); + } + + private Page getAllInventoryItems(Pageable pageable) { + return inventoryItemRepository.findAllItems(pageable); + } + public List getHeaders() { List header = new ArrayList<>(); header.add("Id"); header.add("Estado"); @@ -159,25 +173,16 @@ private List createHeader() { return header; } - public List getAllItems() { - List allItems = getAllInventoryItems(); - List inventoryItems = new ArrayList<>(); - - allItems.forEach(item -> inventoryItems.add(InventoryItemMapper.mapToItemInfo(item, findCategory(item)))); - return inventoryItems; - } - - - public Category findCategory(InventoryItem generalItem){ + public Category findCategory(InventoryItem generalItem) { return categoryRepository.findCategoryById(generalItem.getCategoryId()); } public InventoryItem findItemById(String itemId) { - return inventoryItemRepository.findById(itemId) + return inventoryItemRepository.findById(itemId) .orElseThrow(() -> new NotExistingException("409", HttpStatus.CONFLICT, "Item not exists into inventary")); } - public void updateInventoryItemState (String itemId, ItemState state) { + public void updateInventoryItemState(String itemId, ItemState state) { InventoryItem existingItem = findItemById(itemId); existingItem.setState(state); @@ -191,7 +196,7 @@ public void updateInventoryItemTotal(String itemId, int quantityChange) { inventoryItemRepository.save(existingItem); } - public void changeStateNoQuantizableItem(InventoryItem item, ItemState state){ + public void changeStateNoQuantizableItem(InventoryItem item, ItemState state) { if (Boolean.FALSE.equals(item.getCategory().getQuantizable())) { updateInventoryItemState(item.getItemId(), state); } @@ -201,11 +206,15 @@ public void changeStateNoQuantizableItem(InventoryItem item, ItemState state){ public InventoryItem updateInventoryItemQuantity(String itemId, int quantity) { InventoryItem existingItem = findItemById(itemId); - if(existingItem.getQuantity() + quantity < 0){ + if (existingItem.getQuantity() + quantity < 0) { throw new IllegalParameterInRequest("400", HttpStatus.BAD_REQUEST, "The quantity " + itemId + " you want to restore is greater than current"); } existingItem.setQuantity(existingItem.getQuantity() + quantity); - existingItem.setTotal(existingItem.getTotal()+quantity); + existingItem.setTotal(existingItem.getTotal() + quantity); return inventoryItemRepository.save(existingItem); } -} + + + + +} \ No newline at end of file diff --git a/src/main/java/com/consola/lis/service/LoanService.java b/src/main/java/com/consola/lis/service/LoanService.java index 9a0e27c..f586c8d 100644 --- a/src/main/java/com/consola/lis/service/LoanService.java +++ b/src/main/java/com/consola/lis/service/LoanService.java @@ -15,10 +15,13 @@ import com.consola.lis.util.mapper.LoanMapper; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import java.util.*; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -108,15 +111,28 @@ public List getAllLoans() { - public Map getAllLoansMapper() { - List loans = getAllLoansMap(); + public Map getAllLoansMapper(Pageable pageable) { + Page loanPage = getAllLoans(pageable); + Map result = new HashMap<>(); - result.put("header", createHeader()); - result.put("allRegisters", loans); + result.put("totalElements", loanPage.getTotalElements()); + result.put("totalPages", loanPage.getTotalPages()); + result.put("currentPage", loanPage.getNumber()); + result.put("items", mapToLoanInfoList(loanPage.getContent())); return result; } - private List createHeader() { + private List mapToLoanInfoList(List loans) { + return loans.stream() + .map(loan -> LoanMapper.mapLoanToDTO(loan, inventoryItemService.findInventoryItem(loan.getItemId()))) + .toList(); + } + + private Page getAllLoans(Pageable pageable) { + return loanRepository.findAllLoans(pageable); + } + + public List getHeaders() { List header = new ArrayList<>(); header.add("Id"); header.add("Elemento"); @@ -128,19 +144,6 @@ private List createHeader() { return header; } - public List getAllLoansMap() { - List allLoans = getAllLoans(); - List loans = new ArrayList<>(); - - allLoans.forEach(loan -> { - InventoryItem item = inventoryItemService.findInventoryItem(loan.getItemId()); - loans.add(LoanMapper.mapLoanToDTO(loan,item)); - - }); - - return loans; - } - public void updateReturnLoanState (int loanId, LoanState state) { Loan existingLoan = loanRepository.findById(loanId) .orElseThrow(() -> new NotExistingException("409", HttpStatus.CONFLICT, "Item not exists into inventary")); @@ -150,4 +153,5 @@ public void updateReturnLoanState (int loanId, LoanState state) { } + } diff --git a/src/main/java/com/consola/lis/service/ReturnLoanService.java b/src/main/java/com/consola/lis/service/ReturnLoanService.java index 4c0439e..2c07027 100644 --- a/src/main/java/com/consola/lis/service/ReturnLoanService.java +++ b/src/main/java/com/consola/lis/service/ReturnLoanService.java @@ -48,7 +48,7 @@ public void createReturnLoan(ReturnLoanDTO returnLoanRequest) { ReturnLoan returnLoan = ReturnLoan.builder() .loanId(returnLoanRequest.getLoanId()) - .borrowerUser(returnLoanRequest.getBorrowerUser()) + .borrowerUser(loanService.getOneLoan(returnLoanRequest.getLoanId()).getBorrowerUser()) .lenderUser(returnLoanRequest.getLenderUser()) .observation(returnLoanRequest.getObservation()) .build(); diff --git a/src/main/java/com/consola/lis/util/constans/EndpointConstant.java b/src/main/java/com/consola/lis/util/constans/EndpointConstant.java index 794f982..a57cee2 100644 --- a/src/main/java/com/consola/lis/util/constans/EndpointConstant.java +++ b/src/main/java/com/consola/lis/util/constans/EndpointConstant.java @@ -18,16 +18,16 @@ public class EndpointConstant { //Endpoints Inventory Item - public static final String ENDPOINT_INVENTORY = "/api/console-lis/user/inventory"; + public static final String ENDPOINT_INVENTORY = "/api/console-lis/user/inventory/item"; public static final String ENDPOINT_INVENTORY_TABLE = "/tableRegisters"; - public static final String ENDPOINT_INVENTORY_ITEM = "/item"; - public static final String ENDPOINT_DELETE_ITEM="/item/delete/{itemId}"; + public static final String ENDPOINT_DELETE_ITEM="/delete/{itemId}"; public static final String ENDPOINT_EDIT_ITEM_STATE="/item/state/{itemId}"; - public static final String ENDPOINT_ONE_ITEM = "/item/{itemId}"; - public static final String ENDPOINT_EDIT_QUANTITY = "/item/quantity/{itemId}"; + public static final String ENDPOINT_ONE_ITEM = "/{itemId}"; + public static final String ENDPOINT_EDIT_QUANTITY = "/quantity/{itemId}"; + public static final String ENDPOINT_HEADERS_ITEM = "/tableHeaders/"; //user public static final String ENDPOINT_USER = "/api/console-lis/user"; @@ -44,6 +44,7 @@ public class EndpointConstant { public static final String ENDPOINT_GET_ONE_LOAN = "/{loanId}"; public static final String ENDPOINT_ALL_LOANS_TABLE = "/tableRegisters"; + public static final String ENDPOINT_HEADERS_LOAN = "/tableHeaders/"; //Return Loan public static final String ENDPOINT_RETURN_LOAN = "/api/console-lis/user/returnLoan";