From 97c48a7773f2b3c650d895b26bf3165f6ee7e2ca Mon Sep 17 00:00:00 2001 From: superpollo2 Date: Thu, 25 Apr 2024 23:32:07 -0500 Subject: [PATCH] Refactoring: Decrease in cognitive complexity in Loan Service a Inventory Item --- .../lis/controller/CategoryController.java | 8 ++-- .../consola/lis/service/CategoryService.java | 15 +++--- .../lis/service/InventoryItemService.java | 26 ++++++----- .../com/consola/lis/service/LoanService.java | 46 ++++++++++++------- .../lis/service/ReturnLoanService.java | 2 +- .../lis/util/constans/EndpointConstant.java | 4 +- .../consola/lis/util/mapper/LoanMapper.java | 1 - src/main/resources/application-dev.properties | 2 +- .../resources/application-prod.properties | 2 +- .../lis/service/CategoryServiceTest.java | 16 +++---- 10 files changed, 70 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/consola/lis/controller/CategoryController.java b/src/main/java/com/consola/lis/controller/CategoryController.java index 904f5b0..7f45300 100644 --- a/src/main/java/com/consola/lis/controller/CategoryController.java +++ b/src/main/java/com/consola/lis/controller/CategoryController.java @@ -34,13 +34,13 @@ public void createCategory(@RequestBody CategoryDTO category) throws JacksonExce @DeleteMapping(EndpointConstant.ENDPOINT_DELETE_CATEGORY) - public void deleteCategory(@PathVariable("categoryName") String categoryName){ - categoryService.deleteCategory(categoryName); + public void deleteCategory(@PathVariable("categoryId") int categoryId){ + categoryService.deleteCategory(categoryId); } @GetMapping(EndpointConstant.ENDPOINT_ONE_CATEGORY) - public ResponseEntity findCategory(@PathVariable("categoryName") String categoryName){ - return ResponseEntity.ok(categoryService.findCategory(categoryName)); + public ResponseEntity findCategory(@PathVariable("categoryId") String categoryId){ + return ResponseEntity.ok(categoryService.findCategory(categoryId)); } @GetMapping(EndpointConstant.ENDPOINT_ALL_NAMES_CATEGORIES) diff --git a/src/main/java/com/consola/lis/service/CategoryService.java b/src/main/java/com/consola/lis/service/CategoryService.java index 21bd0a4..8d0236a 100644 --- a/src/main/java/com/consola/lis/service/CategoryService.java +++ b/src/main/java/com/consola/lis/service/CategoryService.java @@ -50,12 +50,12 @@ public void createCategory(CategoryDTO categoryRequest) throws JsonProcessingExc } @Transactional - public void deleteCategory(String categoryName) { + public void deleteCategory(int categoryId) { - if (existCategory(categoryName)) { - categoryRepository.deleteByCategoryName(categoryName); + if (existCategory(categoryId)) { + categoryRepository.deleteById(categoryId); } else { - throw new NotExistingException("404", HttpStatus.NOT_FOUND, " the category whit name " + categoryName + " not exist"); + throw new NotExistingException("404", HttpStatus.NOT_FOUND, " the category whit name " + categoryId + " not exist"); } } @@ -68,11 +68,14 @@ public Category findCategory(String categoryName) { } } - public boolean existCategory(String categoryName) { - return categoryRepository.existsByCategoryName(categoryName); + + + public boolean existCategory(int categoryId) { + return categoryRepository.existsById(categoryId); } + public List getCategoryNames() { List categories = categoryRepository.findAll(); if (!categories.isEmpty()) { diff --git a/src/main/java/com/consola/lis/service/InventoryItemService.java b/src/main/java/com/consola/lis/service/InventoryItemService.java index 8809cf1..f438349 100644 --- a/src/main/java/com/consola/lis/service/InventoryItemService.java +++ b/src/main/java/com/consola/lis/service/InventoryItemService.java @@ -74,21 +74,25 @@ public InventoryItem createInventoryItem(InventoryItemDTO inventoryItemRequest) inventoryItemRequest.setWallet(WalletOwners.NOT_APPLY); } - InventoryItem inventoryItem = InventoryItem.builder() - .itemId(inventoryItemRequest.getItemId()) - .categoryId(inventoryItemRequest.getCategoryId()) - .wallet(WalletOwners.valueOf(inventoryItemRequest.getWallet().name())) - .lendable(inventoryItemRequest.getLendable()) - .state(ItemState.valueOf(inventoryItemRequest.getState().name())) - .attributes(attributesJson) - .quantity(inventoryItemRequest.getQuantity()) - .total(inventoryItemRequest.getQuantity()) - .build(); + InventoryItem inventoryItem = createNewItem(inventoryItemRequest, attributesJson); return inventoryItemRepository.save(inventoryItem); } } + private InventoryItem createNewItem(InventoryItemDTO inventoryItemRequest, String attributesJson) { + return InventoryItem.builder() + .itemId(inventoryItemRequest.getItemId()) + .categoryId(inventoryItemRequest.getCategoryId()) + .wallet(WalletOwners.valueOf(inventoryItemRequest.getWallet().name())) + .lendable(inventoryItemRequest.getLendable()) + .state(ItemState.valueOf(inventoryItemRequest.getState().name())) + .attributes(attributesJson) + .quantity(inventoryItemRequest.getQuantity()) + .total(inventoryItemRequest.getQuantity()) + .build(); + } + public void validateCategoryExists(Integer categoryId) { if (!categoryRepository.existsById(categoryId)) { @@ -183,7 +187,7 @@ public void updateInventoryItemTotal(String itemId, int quantityChange) { } public void changeStateNoQuantizableItem(InventoryItem item, ItemState state){ - if (!item.getCategory().getQuantizable()) { + if (Boolean.FALSE.equals(item.getCategory().getQuantizable())) { updateInventoryItemState(item.getItemId(), state); } } diff --git a/src/main/java/com/consola/lis/service/LoanService.java b/src/main/java/com/consola/lis/service/LoanService.java index 736b5d9..9a0e27c 100644 --- a/src/main/java/com/consola/lis/service/LoanService.java +++ b/src/main/java/com/consola/lis/service/LoanService.java @@ -27,46 +27,58 @@ public class LoanService { private final LoanRepository loanRepository; private final InventoryItemService inventoryItemService; + public Loan createLoan(LoanDTO loanRequest) { + validateLoanRequest(loanRequest); + + InventoryItem item = inventoryItemService.findInventoryItem(loanRequest.getItemId()); + updateItemStateAndTotal(item, loanRequest.getQuantity()); - if(!inventoryItemService.existItem(loanRequest.getItemId())) { - throw new AlreadyExistsException("409", HttpStatus.CONFLICT, "Item no exists into inventory"); + Loan loan = buildLoanFromRequest(loanRequest); + return loanRepository.save(loan); + } + + private void validateLoanRequest(LoanDTO loanRequest) { + if (!inventoryItemService.existItem(loanRequest.getItemId())) { + throw new AlreadyExistsException("409", HttpStatus.CONFLICT, "Item does not exist in inventory"); } InventoryItem item = inventoryItemService.findInventoryItem(loanRequest.getItemId()); - if(!item.getLendable()) { - throw new IllegalParameterInRequest("400", HttpStatus.BAD_REQUEST, "This item not is lendable"); + + if (Boolean.FALSE.equals(item.getLendable())) { + throw new IllegalParameterInRequest("400", HttpStatus.BAD_REQUEST, "This item is not lendable"); } - if(loanRequest.getQuantity()>item.getTotal()){ + if (loanRequest.getQuantity() > item.getTotal()) { throw new IllegalParameterInRequest("400", HttpStatus.BAD_REQUEST, "The quantity is greater than the stock"); } + } - inventoryItemService.changeStateNoQuantizableItem(item, ItemState.LENDED); - + private void updateItemStateAndTotal(InventoryItem item, int quantity) { + inventoryItemService.changeStateNoQuantizableItem(item, ItemState.LENDED); - if (item.getCategory().getQuantizable() && item.getTotal() - loanRequest.getQuantity() == 0) { - inventoryItemService.updateInventoryItemState(loanRequest.getItemId(), ItemState.OUT_OF_STOCK); + if (Boolean.TRUE.equals(item.getCategory().getQuantizable()) && item.getTotal() - quantity == 0) { + inventoryItemService.updateInventoryItemState(item.getItemId(), ItemState.OUT_OF_STOCK); } - inventoryItemService.updateInventoryItemTotal(loanRequest.getItemId(), -loanRequest.getQuantity()); + inventoryItemService.updateInventoryItemTotal(item.getItemId(), -quantity); + } - if(loanRequest.getLoanType()==null) loanRequest.setLoanType(LoanType.GENERAL); + private Loan buildLoanFromRequest(LoanDTO loanRequest) { + if (loanRequest.getLoanType() == null) { + loanRequest.setLoanType(LoanType.GENERAL); + } - Loan loan = Loan.builder() + return Loan.builder() .itemId(loanRequest.getItemId()) .loanType(LoanType.valueOf(loanRequest.getLoanType().name())) .borrowerUser(loanRequest.getBorrowerUser()) .lenderUser(loanRequest.getLenderUser()) .quantity(loanRequest.getQuantity()) - .loanState(LoanState.valueOf(LoanState.ACTIVE.name())) + .loanState(LoanState.ACTIVE) .observation(loanRequest.getObservation()) .returnDate(loanRequest.getReturnDate()) .build(); - - return loanRepository.save(loan); - - } public void deleteLoan(int loanId){ diff --git a/src/main/java/com/consola/lis/service/ReturnLoanService.java b/src/main/java/com/consola/lis/service/ReturnLoanService.java index a52dcec..4c0439e 100644 --- a/src/main/java/com/consola/lis/service/ReturnLoanService.java +++ b/src/main/java/com/consola/lis/service/ReturnLoanService.java @@ -38,7 +38,7 @@ public void createReturnLoan(ReturnLoanDTO returnLoanRequest) { Loan loan = loanService.getOneLoan(returnLoanRequest.getLoanId()); InventoryItem item = inventoryItemService.findInventoryItem(loan.getItemId()); - if (item.getCategory().getQuantizable() && item.getTotal() == 0) { + if (Boolean.TRUE.equals(item.getCategory().getQuantizable()) && item.getTotal() == 0) { inventoryItemService.updateInventoryItemState(loan.getItemId(), ItemState.AVAILABLE); } 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 f43e22e..10caf61 100644 --- a/src/main/java/com/consola/lis/util/constans/EndpointConstant.java +++ b/src/main/java/com/consola/lis/util/constans/EndpointConstant.java @@ -12,8 +12,8 @@ public class EndpointConstant { //Endpoints Category public static final String ENDPOINT_CATEGORY = "/api/console-lis/auth/category"; - public static final String ENDPOINT_DELETE_CATEGORY = "/{categoryName}"; - public static final String ENDPOINT_ONE_CATEGORY = "/{categoryName}"; + public static final String ENDPOINT_DELETE_CATEGORY = "/{categoryId}"; + public static final String ENDPOINT_ONE_CATEGORY = "/{categoryId}"; public static final String ENDPOINT_ALL_NAMES_CATEGORIES = "/categoriesNames"; diff --git a/src/main/java/com/consola/lis/util/mapper/LoanMapper.java b/src/main/java/com/consola/lis/util/mapper/LoanMapper.java index 3c344f4..fc0dd81 100644 --- a/src/main/java/com/consola/lis/util/mapper/LoanMapper.java +++ b/src/main/java/com/consola/lis/util/mapper/LoanMapper.java @@ -4,7 +4,6 @@ import com.consola.lis.model.entity.InventoryItem; import com.consola.lis.model.entity.Loan; -import java.util.Optional; public class LoanMapper { diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index df42857..6c6fa52 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -8,7 +8,7 @@ spring.ldap.urls=rls=https://sistemas.udea.edu.co/api/ldap/login/{username} #config data base spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root -spring.datasource.password= +spring.datasource.password=Tascon1234W% spring.datasource.url=jdbc:mysql://localhost:3306/console_lis?allowPublicKeyRetrieval=true&useSSL=false springdoc.default-produces-media-type=application/json spring.jpa.show-sql=true diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties index 46630ef..43bd2f6 100644 --- a/src/main/resources/application-prod.properties +++ b/src/main/resources/application-prod.properties @@ -11,7 +11,7 @@ spring.datasource.username=root spring.datasource.password=c0ns0l3l1s spring.datasource.url=jdbc:mysql://localhost:33060/console_lis?allowPublicKeyRetrieval=true&useSSL=false springdoc.default-produces-media-type=application/json -spring.jpa.show-sql=true + diff --git a/src/test/java/com/consola/lis/service/CategoryServiceTest.java b/src/test/java/com/consola/lis/service/CategoryServiceTest.java index 80de5a6..99a80ec 100644 --- a/src/test/java/com/consola/lis/service/CategoryServiceTest.java +++ b/src/test/java/com/consola/lis/service/CategoryServiceTest.java @@ -71,23 +71,23 @@ void testCreateCategory_CategoryAlreadyExists() { @Test void testDeleteCategory_CategoryExists() { - String categoryName = "Test"; + int categoryId = 1; - when(categoryRepository.existsByCategoryName(categoryName)).thenReturn(true); + when(categoryRepository.existsById(categoryId)).thenReturn(true); - assertDoesNotThrow(() -> categoryService.deleteCategory(categoryName)); - verify(categoryRepository, times(1)).deleteByCategoryName(categoryName); + assertDoesNotThrow(() -> categoryService.deleteCategory(categoryId)); + verify(categoryRepository, times(1)).deleteById(categoryId); } @Test void testDeleteCategory_CategoryNotExists() { - String categoryName = "Test"; + int categoryId = 1; - when(categoryRepository.existsByCategoryName(categoryName)).thenReturn(false); + when(categoryRepository.existsById(categoryId)).thenReturn(false); - assertThrows(NotExistingException.class, () -> categoryService.deleteCategory(categoryName)); - verify(categoryRepository, never()).deleteByCategoryName(categoryName); + assertThrows(NotExistingException.class, () -> categoryService.deleteCategory(categoryId)); + verify(categoryRepository, never()).deleteById(categoryId); } @Test