From 218a2cdfce75b5325aaeb87bc79e12b1770c19cd Mon Sep 17 00:00:00 2001 From: superpollo2 Date: Mon, 13 May 2024 13:23:42 -0500 Subject: [PATCH 1/3] Implement pagination in the endpoint returning mapped records for tables Modified the endpoint returning mapped records for tables to use pagination using the Pageable return type. Added the Pageable method to the corresponding JPA repository to enable pagination of records. The following changes were made: - Adjusted the inventory service (InventoryItem) to use pagination when accessing records. - Adjusted the loan service to accommodate the changes and use pagination when accessing records. - Adjusted de Controller inventory y loan These changes will improve performance when handling large data sets and enable efficient navigation through mapped records for tables. --- .../lis/controller/InventoryController.java | 12 +++- .../lis/controller/LoanController.java | 5 +- .../java/com/consola/lis/dto/Response.java | 12 ++++ .../repository/InventoryItemRepository.java | 10 ++- .../lis/model/repository/LoanRepository.java | 7 +++ .../lis/service/InventoryItemService.java | 62 ++++++++++++------- .../com/consola/lis/service/LoanService.java | 32 +++++----- 7 files changed, 99 insertions(+), 41 deletions(-) create mode 100644 src/main/java/com/consola/lis/dto/Response.java diff --git a/src/main/java/com/consola/lis/controller/InventoryController.java b/src/main/java/com/consola/lis/controller/InventoryController.java index 0e7ac49..c70b15e 100644 --- a/src/main/java/com/consola/lis/controller/InventoryController.java +++ b/src/main/java/com/consola/lis/controller/InventoryController.java @@ -8,9 +8,12 @@ import com.fasterxml.jackson.core.JsonProcessingException; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; 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; @@ -42,8 +45,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) @@ -63,6 +66,11 @@ public ResponseEntity updateInventoryItemQuantity(@PathVariable(" } + @GetMapping(value = "/products") + public ResponseEntity getAllProducts(Pageable pageable) { + return ResponseEntity.ok(inventoryItemService.getAllProducts(pageable)); + } + diff --git a/src/main/java/com/consola/lis/controller/LoanController.java b/src/main/java/com/consola/lis/controller/LoanController.java index 44db1c4..dfdfc81 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,7 @@ public List gelAllLoans(){ } @GetMapping(EndpointConstant.ENDPOINT_ALL_LOANS_TABLE) - public Map loans(){ - return loanService.getAllLoansMapper(); + public Map loans(Pageable pageable){ + return loanService.getAllLoansMapper(pageable); } } diff --git a/src/main/java/com/consola/lis/dto/Response.java b/src/main/java/com/consola/lis/dto/Response.java new file mode 100644 index 0000000..87b79f0 --- /dev/null +++ b/src/main/java/com/consola/lis/dto/Response.java @@ -0,0 +1,12 @@ +package com.consola.lis.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class Response{ + private Long size; + private Integer pages; + private T collection; +} 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..b0daadb 100644 --- a/src/main/java/com/consola/lis/service/InventoryItemService.java +++ b/src/main/java/com/consola/lis/service/InventoryItemService.java @@ -3,6 +3,7 @@ import com.consola.lis.dto.ItemInfoDTO; import com.consola.lis.dto.LoanDTO; +import com.consola.lis.dto.Response; import com.consola.lis.model.entity.Loan; import com.consola.lis.util.constans.Util; import com.consola.lis.dto.InventoryItemDTO; @@ -19,10 +20,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 +65,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 +78,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 +86,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,14 +144,25 @@ 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 mapToItemInfoList(List items) { + return items.stream() + .map(item -> InventoryItemMapper.mapToItemInfo(item, findCategory(item))) + .collect(Collectors.toList()); + } + private List createHeader() { List header = new ArrayList<>(); header.add("Id"); @@ -159,25 +174,20 @@ 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; + private Page getAllInventoryItems(Pageable pageable) { + return inventoryItemRepository.findAllItems(pageable); } - - 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 +201,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 +211,21 @@ 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); } -} + + + public Response> getAllProducts(Pageable pageable) { + Page listOfProducts = inventoryItemRepository.findAllItems(pageable); + return new Response<>( + listOfProducts.getTotalElements(), + listOfProducts.getTotalPages(), + listOfProducts.getContent() + ); + } +} \ 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..b797c31 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,14 +111,23 @@ 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 mapToLoanInfoList(List loans) { + return loans.stream() + .map(loan -> LoanMapper.mapLoanToDTO(loan, inventoryItemService.findInventoryItem(loan.getItemId()))) + .collect(Collectors.toList()); + } private List createHeader() { List header = new ArrayList<>(); header.add("Id"); @@ -127,18 +139,8 @@ private List createHeader() { header.add("Fecha DevoluciĆ³n"); 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; + private Page getAllLoans(Pageable pageable) { + return loanRepository.findAllLoans(pageable); } public void updateReturnLoanState (int loanId, LoanState state) { From 296deacefafb89768711ee681a2d48dde2fb87a4 Mon Sep 17 00:00:00 2001 From: superpollo2 Date: Mon, 13 May 2024 13:45:53 -0500 Subject: [PATCH 2/3] Separation of data in the response for tables, moving headers to a separate endpoint for both inventory and loans. Changes made include: - Headers are now fetched from a separate endpoint. - Adjustments made to both inventory and loans services to accommodate these changes. This restructuring improves the organization of data retrieval and allows for cleaner separation of concerns between data and headers in table responses. --- .../consola/lis/controller/InventoryController.java | 6 +++++- .../com/consola/lis/controller/LoanController.java | 5 +++++ .../com/consola/lis/service/InventoryItemService.java | 6 ++++-- .../java/com/consola/lis/service/LoanService.java | 5 +++-- .../consola/lis/util/constans/EndpointConstant.java | 11 ++++++----- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/consola/lis/controller/InventoryController.java b/src/main/java/com/consola/lis/controller/InventoryController.java index c70b15e..0506d21 100644 --- a/src/main/java/com/consola/lis/controller/InventoryController.java +++ b/src/main/java/com/consola/lis/controller/InventoryController.java @@ -26,7 +26,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); @@ -71,6 +71,10 @@ public ResponseEntity getAllProducts(Pageable pageable) { return ResponseEntity.ok(inventoryItemService.getAllProducts(pageable)); } + @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 dfdfc81..7cd3447 100644 --- a/src/main/java/com/consola/lis/controller/LoanController.java +++ b/src/main/java/com/consola/lis/controller/LoanController.java @@ -49,4 +49,9 @@ public List gelAllLoans(){ 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/service/InventoryItemService.java b/src/main/java/com/consola/lis/service/InventoryItemService.java index b0daadb..4a35011 100644 --- a/src/main/java/com/consola/lis/service/InventoryItemService.java +++ b/src/main/java/com/consola/lis/service/InventoryItemService.java @@ -148,7 +148,6 @@ public Map getAllItemsMapped(Pageable pageable) { Page inventoryItemsPage = getAllInventoryItems(pageable); Map result = new HashMap<>(); - result.put("header", createHeader()); result.put("totalElements", inventoryItemsPage.getTotalElements()); result.put("totalPages", inventoryItemsPage.getTotalPages()); result.put("currentPage", inventoryItemsPage.getNumber()); @@ -163,7 +162,7 @@ private List mapToItemInfoList(List items) { .collect(Collectors.toList()); } - private List createHeader() { + public List getHeaders() { List header = new ArrayList<>(); header.add("Id"); header.add("Estado"); @@ -174,6 +173,8 @@ private List createHeader() { return header; } + + private Page getAllInventoryItems(Pageable pageable) { return inventoryItemRepository.findAllItems(pageable); } @@ -228,4 +229,5 @@ public Response> getAllProducts(Pageable pageable) { listOfProducts.getContent() ); } + } \ 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 b797c31..8f63f26 100644 --- a/src/main/java/com/consola/lis/service/LoanService.java +++ b/src/main/java/com/consola/lis/service/LoanService.java @@ -115,7 +115,6 @@ public Map getAllLoansMapper(Pageable pageable) { Page loanPage = getAllLoans(pageable); Map result = new HashMap<>(); - result.put("header", createHeader()); result.put("totalElements", loanPage.getTotalElements()); result.put("totalPages", loanPage.getTotalPages()); result.put("currentPage", loanPage.getNumber()); @@ -128,7 +127,8 @@ private List mapToLoanInfoList(List loans) { .map(loan -> LoanMapper.mapLoanToDTO(loan, inventoryItemService.findInventoryItem(loan.getItemId()))) .collect(Collectors.toList()); } - private List createHeader() { + + public List getHeaders() { List header = new ArrayList<>(); header.add("Id"); header.add("Elemento"); @@ -152,4 +152,5 @@ public void updateReturnLoanState (int loanId, LoanState state) { } + } 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"; From 3ce6aa7baf8b8429c692dfc34eaec46041d4a2e6 Mon Sep 17 00:00:00 2001 From: superpollo2 Date: Mon, 13 May 2024 14:13:06 -0500 Subject: [PATCH 3/3] Removed from a controller and method in the inventory item test service to use pageable --- .../lis/controller/InventoryController.java | 5 ---- .../java/com/consola/lis/dto/Response.java | 12 ---------- .../com/consola/lis/dto/ReturnLoanDTO.java | 1 - .../lis/service/InventoryItemService.java | 23 ++++--------------- .../com/consola/lis/service/LoanService.java | 9 ++++---- .../lis/service/ReturnLoanService.java | 2 +- 6 files changed, 11 insertions(+), 41 deletions(-) delete mode 100644 src/main/java/com/consola/lis/dto/Response.java diff --git a/src/main/java/com/consola/lis/controller/InventoryController.java b/src/main/java/com/consola/lis/controller/InventoryController.java index 0506d21..2ef1bff 100644 --- a/src/main/java/com/consola/lis/controller/InventoryController.java +++ b/src/main/java/com/consola/lis/controller/InventoryController.java @@ -8,7 +8,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Page; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -66,10 +65,6 @@ public ResponseEntity updateInventoryItemQuantity(@PathVariable(" } - @GetMapping(value = "/products") - public ResponseEntity getAllProducts(Pageable pageable) { - return ResponseEntity.ok(inventoryItemService.getAllProducts(pageable)); - } @GetMapping(EndpointConstant.ENDPOINT_HEADERS_ITEM) public List getHeaders(){ diff --git a/src/main/java/com/consola/lis/dto/Response.java b/src/main/java/com/consola/lis/dto/Response.java deleted file mode 100644 index 87b79f0..0000000 --- a/src/main/java/com/consola/lis/dto/Response.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.consola.lis.dto; - -import lombok.AllArgsConstructor; -import lombok.Data; - -@Data -@AllArgsConstructor -public class Response{ - private Long size; - private Integer pages; - private T collection; -} 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/service/InventoryItemService.java b/src/main/java/com/consola/lis/service/InventoryItemService.java index 4a35011..bcaa50c 100644 --- a/src/main/java/com/consola/lis/service/InventoryItemService.java +++ b/src/main/java/com/consola/lis/service/InventoryItemService.java @@ -2,9 +2,6 @@ import com.consola.lis.dto.ItemInfoDTO; -import com.consola.lis.dto.LoanDTO; -import com.consola.lis.dto.Response; -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; @@ -159,9 +156,12 @@ public Map getAllItemsMapped(Pageable pageable) { private List mapToItemInfoList(List items) { return items.stream() .map(item -> InventoryItemMapper.mapToItemInfo(item, findCategory(item))) - .collect(Collectors.toList()); + .toList(); } + private Page getAllInventoryItems(Pageable pageable) { + return inventoryItemRepository.findAllItems(pageable); + } public List getHeaders() { List header = new ArrayList<>(); header.add("Id"); @@ -173,12 +173,6 @@ public List getHeaders() { return header; } - - - private Page getAllInventoryItems(Pageable pageable) { - return inventoryItemRepository.findAllItems(pageable); - } - public Category findCategory(InventoryItem generalItem) { return categoryRepository.findCategoryById(generalItem.getCategoryId()); } @@ -221,13 +215,6 @@ public InventoryItem updateInventoryItemQuantity(String itemId, int quantity) { } - public Response> getAllProducts(Pageable pageable) { - Page listOfProducts = inventoryItemRepository.findAllItems(pageable); - return new Response<>( - listOfProducts.getTotalElements(), - listOfProducts.getTotalPages(), - listOfProducts.getContent() - ); - } + } \ 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 8f63f26..f586c8d 100644 --- a/src/main/java/com/consola/lis/service/LoanService.java +++ b/src/main/java/com/consola/lis/service/LoanService.java @@ -125,7 +125,11 @@ public Map getAllLoansMapper(Pageable pageable) { private List mapToLoanInfoList(List loans) { return loans.stream() .map(loan -> LoanMapper.mapLoanToDTO(loan, inventoryItemService.findInventoryItem(loan.getItemId()))) - .collect(Collectors.toList()); + .toList(); + } + + private Page getAllLoans(Pageable pageable) { + return loanRepository.findAllLoans(pageable); } public List getHeaders() { @@ -139,9 +143,6 @@ public List getHeaders() { header.add("Fecha DevoluciĆ³n"); return header; } - private Page getAllLoans(Pageable pageable) { - return loanRepository.findAllLoans(pageable); - } public void updateReturnLoanState (int loanId, LoanState state) { Loan existingLoan = loanRepository.findById(loanId) 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();