Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pageable implementation #46

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/main/java/com/consola/lis/controller/InventoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,7 +25,7 @@ public class InventoryController {
private final InventoryItemService inventoryItemService;

@ResponseStatus(HttpStatus.CREATED)
@PostMapping(EndpointConstant.ENDPOINT_INVENTORY_ITEM )
@PostMapping()
public ResponseEntity<InventoryItem> createInventoryItem(@RequestBody InventoryItemDTO inventoryItemDTO) throws JsonProcessingException {
InventoryItem createdGeneralItem = inventoryItemService.createInventoryItem(inventoryItemDTO);
return ResponseEntity.ok(createdGeneralItem);
Expand All @@ -42,8 +44,8 @@ public void deleteItem(@PathVariable("itemId") String itemId){


@GetMapping(EndpointConstant.ENDPOINT_INVENTORY_TABLE)
public Map<String, Object> inventoryItems() {
return inventoryItemService.getAllItemsMapped();
public Map<String, Object> inventoryItems(Pageable pageable) {
return inventoryItemService.getAllItemsMapped(pageable);
}

@GetMapping(EndpointConstant.ENDPOINT_ONE_ITEM)
Expand All @@ -64,6 +66,11 @@ public ResponseEntity<InventoryItem> updateInventoryItemQuantity(@PathVariable("
}


@GetMapping(EndpointConstant.ENDPOINT_HEADERS_ITEM)
public List<String> getHeaders(){
return inventoryItemService.getHeaders();
}



}
10 changes: 8 additions & 2 deletions src/main/java/com/consola/lis/controller/LoanController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -45,7 +46,12 @@ public List<Loan> gelAllLoans(){
}

@GetMapping(EndpointConstant.ENDPOINT_ALL_LOANS_TABLE)
public Map<String, Object> loans(){
return loanService.getAllLoansMapper();
public Map<String, Object> loans(Pageable pageable){
return loanService.getAllLoansMapper(pageable);
}

@GetMapping(EndpointConstant.ENDPOINT_HEADERS_LOAN)
public List<String> getHeaders(){
return loanService.getHeaders();
}
}
1 change: 0 additions & 1 deletion src/main/java/com/consola/lis/dto/ReturnLoanDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@NoArgsConstructor
public class ReturnLoanDTO {
private int loanId;
private String borrowerUser;
private String lenderUser;
private String observation;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<InventoryItem, String> {
}

@Query("SELECT p FROM InventoryItem p ")
Page<InventoryItem> findAllItems(Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -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<Loan,Integer> {
List<Loan> findByLoanState(LoanState loanState);

@Query("SELECT p FROM Loan p ")
Page<Loan> findAllLoans(Pageable pageable);
}
63 changes: 36 additions & 27 deletions src/main/java/com/consola/lis/service/InventoryItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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())) {
Expand All @@ -74,15 +75,15 @@ 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);
}
}

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()))
Expand Down Expand Up @@ -140,15 +141,28 @@ public boolean existItem(String itemId) {
}


public Map<String, Object> getAllItemsMapped() {
List<ItemInfoDTO> inventoryItems = getAllItems();
public Map<String, Object> getAllItemsMapped(Pageable pageable) {
Page<InventoryItem> inventoryItemsPage = getAllInventoryItems(pageable);

Map<String, Object> 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<String> createHeader() {
private List<ItemInfoDTO> mapToItemInfoList(List<InventoryItem> items) {
return items.stream()
.map(item -> InventoryItemMapper.mapToItemInfo(item, findCategory(item)))
.toList();
}

private Page<InventoryItem> getAllInventoryItems(Pageable pageable) {
return inventoryItemRepository.findAllItems(pageable);
}
public List<String> getHeaders() {
List<String> header = new ArrayList<>();
header.add("Id");
header.add("Estado");
Expand All @@ -159,25 +173,16 @@ private List<String> createHeader() {
return header;
}

public List<ItemInfoDTO> getAllItems() {
List<InventoryItem> allItems = getAllInventoryItems();
List<ItemInfoDTO> 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);
Expand All @@ -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);
}
Expand All @@ -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);
}
}




}
40 changes: 22 additions & 18 deletions src/main/java/com/consola/lis/service/LoanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,15 +111,28 @@ public List<Loan> getAllLoans() {



public Map<String, Object> getAllLoansMapper() {
List<LoanInfoDTO> loans = getAllLoansMap();
public Map<String, Object> getAllLoansMapper(Pageable pageable) {
Page<Loan> loanPage = getAllLoans(pageable);

Map<String, Object> 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<String> createHeader() {
private List<LoanInfoDTO> mapToLoanInfoList(List<Loan> loans) {
return loans.stream()
.map(loan -> LoanMapper.mapLoanToDTO(loan, inventoryItemService.findInventoryItem(loan.getItemId())))
.toList();
}

private Page<Loan> getAllLoans(Pageable pageable) {
return loanRepository.findAllLoans(pageable);
}

public List<String> getHeaders() {
List<String> header = new ArrayList<>();
header.add("Id");
header.add("Elemento");
Expand All @@ -128,19 +144,6 @@ private List<String> createHeader() {
return header;
}

public List<LoanInfoDTO> getAllLoansMap() {
List<Loan> allLoans = getAllLoans();
List<LoanInfoDTO> 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"));
Expand All @@ -150,4 +153,5 @@ public void updateReturnLoanState (int loanId, LoanState state) {
}



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