Skip to content

Commit

Permalink
Refactoring: Decrease in cognitive complexity in Loan Service a Inven…
Browse files Browse the repository at this point in the history
…tory Item
  • Loading branch information
superpollo2 committed Apr 26, 2024
1 parent 0e26f46 commit 97c48a7
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category> findCategory(@PathVariable("categoryName") String categoryName){
return ResponseEntity.ok(categoryService.findCategory(categoryName));
public ResponseEntity<Category> findCategory(@PathVariable("categoryId") String categoryId){
return ResponseEntity.ok(categoryService.findCategory(categoryId));
}

@GetMapping(EndpointConstant.ENDPOINT_ALL_NAMES_CATEGORIES)
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/consola/lis/service/CategoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand All @@ -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<String> getCategoryNames() {
List<Category> categories = categoryRepository.findAll();
if (!categories.isEmpty()) {
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/com/consola/lis/service/InventoryItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
46 changes: 29 additions & 17 deletions src/main/java/com/consola/lis/service/LoanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";


Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/consola/lis/util/mapper/LoanMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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




16 changes: 8 additions & 8 deletions src/test/java/com/consola/lis/service/CategoryServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 97c48a7

Please sign in to comment.