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

Feat/add user ldap #54

Merged
merged 5 commits into from
Jun 3, 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
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/consola/lis/configuration/EnumConfig.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.consola.lis.configuration;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EnumConfig{
public class EnumConfig {

@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
return objectMapper;
}



}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.consola.lis.controller;

import com.consola.lis.dto.TableRegistersItemDTO;
import com.consola.lis.dto.TableRegistersLoanDTO;
import com.consola.lis.model.enums.ItemState;
import com.consola.lis.util.constans.ApiDescription;
import com.consola.lis.util.constans.EndpointConstant;
Expand All @@ -10,6 +12,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -49,9 +52,10 @@ public void deleteItem(@PathVariable("itemId") String itemId){


@Operation(summary = ApiDescription.DESCRIPTION_INVENTORY_TABLE)
@GetMapping(EndpointConstant.ENDPOINT_INVENTORY_TABLE)
public Map<String, Object> inventoryItems(Pageable pageable) {
return inventoryItemService.getAllItemsMapped(pageable);
@PostMapping(EndpointConstant.ENDPOINT_INVENTORY_TABLE)
public Map<String, Object> inventoryItems(@RequestBody TableRegistersItemDTO tableRegistersDTO){
Pageable pageable = PageRequest.of(tableRegistersDTO.getPage(), tableRegistersDTO.getSize());
return inventoryItemService.getAllItemsMapped(tableRegistersDTO.getLendable(),pageable);
}

@Operation(summary = ApiDescription.DESCRIPTION_ONE_ITEM)
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/consola/lis/controller/LoanController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@


import com.consola.lis.dto.LoanDTO;
import com.consola.lis.dto.TableRegistersLoanDTO;
import com.consola.lis.model.entity.Loan;
import com.consola.lis.service.LoanService;
import com.consola.lis.util.constans.ApiDescription;
import com.consola.lis.util.constans.EndpointConstant;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,6 +20,8 @@
import java.util.List;
import java.util.Map;

import static java.lang.Integer.parseInt;

@Tag(name = "Manage Loans", description = ApiDescription.DESCRIPTION_CONTROLLER_LOAN)
@RestController
@RequestMapping(EndpointConstant.ENDPOINT_LOAN)
Expand Down Expand Up @@ -51,10 +55,12 @@ public List<Loan> gelAllLoans(){
return loanService.getAllLoans();
}


@Operation(summary = ApiDescription.DESCRIPTION_ALL_LOANS_TABLE)
@GetMapping(EndpointConstant.ENDPOINT_ALL_LOANS_TABLE)
public Map<String, Object> loans(Pageable pageable){
return loanService.getAllLoansMapper(pageable);
@PostMapping(EndpointConstant.ENDPOINT_ALL_LOANS_TABLE )
public Map<String, Object> loans(@RequestBody TableRegistersLoanDTO tableRegistersDTO){
Pageable pageable = PageRequest.of(tableRegistersDTO.getPage(), tableRegistersDTO.getSize());
return loanService.getAllLoansMapper(tableRegistersDTO.getLoanState(),pageable);
}

@Operation(summary = ApiDescription.DESCRIPTION_HEADERS_LOAN)
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/consola/lis/dto/LoanInfoDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class LoanInfoDTO {
private String loanName;
private String loanState;
private String borrowerUser;

private String loanItemId;
private String loanType;
@JsonFormat(pattern = "yyyy-MM-dd")
private Date loanDate;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/consola/lis/dto/TableRegistersItemDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.consola.lis.dto;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TableRegistersItemDTO {
private int page;
private int size;
private Boolean lendable;
}
21 changes: 21 additions & 0 deletions src/main/java/com/consola/lis/dto/TableRegistersLoanDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.consola.lis.dto;

import com.consola.lis.model.enums.LoanState;
import com.consola.lis.util.deserializer.LoanStateDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TableRegistersLoanDTO {
private int page;
private int size;
@JsonDeserialize(using = LoanStateDeserializer.class)
private LoanState loanState;
}
2 changes: 2 additions & 0 deletions src/main/java/com/consola/lis/model/entity/Loan.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class Loan {
@Column(name = "lender_user")
private String lenderUser;

private String item;
private String category;
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", timezone="GMT-5")
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/consola/lis/model/enums/ItemState.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public String toString() {
}

public static ItemState fromString(String text) {
if (text == null || text.trim().isEmpty()) {
return null;
}
for (ItemState state : ItemState.values()) {
if (state.lowerCase.equalsIgnoreCase(text)) {
return state;
Expand All @@ -35,6 +38,8 @@ public static ItemState fromString(String text) {
throw new IllegalArgumentException("No enum constant with text " + text);
}



public static List<String> getLowerCaseValuesList() {
List<String> lowerCaseValues = new ArrayList<>();
for (ItemState state : ItemState.values()) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/consola/lis/model/enums/LoanState.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public String toString() {
}

public static LoanState fromString(String text) {
if (text == null || text.trim().isEmpty()) {
return null;
}
for (LoanState state : LoanState.values()) {
if (state.lowerCase.equalsIgnoreCase(text)) {
return state;
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/consola/lis/model/enums/UserRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum UserRole {
AUXPROG ("Auxiliar de Programación"),
AUXADMI("Auxiliar Administrativo"),
STUDENT("Estudiante"),
GRADUATED("Egresado"),
PROFESSOR("Profesor");

final String lowerCase;
Expand All @@ -27,16 +28,17 @@ public String toString() {
}

public static UserRole fromString(String text) {
try {
for (UserRole role : UserRole.values()) {
if (role.lowerCase.equalsIgnoreCase(text)) {
return role;
}
if (text == null || text.isEmpty()) {
throw new IllegalArgumentException("Text cannot be null or empty");
}

for (UserRole role : UserRole.values()) {
if (role.lowerCase.equalsIgnoreCase(text)) {
return role;
}
} catch (IllegalArgumentException e) {
return fromNumericString(text);
}
throw new IllegalArgumentException("No enum constant with text " + text);

return fromNumericString(text);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.consola.lis.model.repository;

import com.consola.lis.model.entity.InventoryItem;

import com.consola.lis.model.enums.ItemState;
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.data.repository.query.Param;
import org.springframework.stereotype.Repository;


Expand All @@ -16,5 +15,6 @@ public interface InventoryItemRepository extends JpaRepository<InventoryItem, St
@Query("SELECT p FROM InventoryItem p ")
Page<InventoryItem> findAllItems(Pageable pageable);

Page<InventoryItem> findByState(ItemState state, Pageable pageable);
@Query("SELECT p FROM InventoryItem p WHERE (:lendable IS NULL OR p.lendable = :lendable)")
Page<InventoryItem> findAllLoansByLendable(@Param("lendable") Boolean lendable, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

Expand All @@ -14,4 +15,8 @@ public interface LoanRepository extends JpaRepository<Loan,Integer> {

@Query("SELECT p FROM Loan p ")
Page<Loan> findAllLoans(Pageable pageable);


@Query("SELECT p FROM Loan p WHERE (:loanState IS NULL OR p.loanState = :loanState) ORDER BY CASE WHEN p.loanState = 'ACTIVE' THEN 0 ELSE 1 END, p.loanId ASC")
Page<Loan> findAllLoansByState(@Param("loanState") LoanState loanState, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ public boolean existItem(String itemId) {
}


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

public Map<String, Object> getAllItemsMapped(Boolean lendable, Pageable pageable) {
Page<InventoryItem> inventoryItemsPage = getAllInventoryItems(lendable, pageable);
Map<String, Object> result = new HashMap<>();
result.put("totalElements", inventoryItemsPage.getTotalElements());
result.put("totalPages", inventoryItemsPage.getTotalPages());
Expand All @@ -158,8 +157,8 @@ private List<ItemInfoDTO> mapToItemInfoList(List<InventoryItem> items) {
.toList();
}

private Page<InventoryItem> getAllInventoryItems(Pageable pageable) {
return inventoryItemRepository.findAllItems(pageable);
private Page<InventoryItem> getAllInventoryItems(Boolean lendable,Pageable pageable) {
return inventoryItemRepository.findAllLoansByLendable(lendable,pageable);
}
public List<String> getHeaders() {
List<String> header = new ArrayList<>();
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/com/consola/lis/service/LoanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.consola.lis.model.enums.LoanState;
import com.consola.lis.model.enums.LoanType;
import com.consola.lis.model.enums.ItemState;
import com.consola.lis.model.repository.InventoryItemRepository;
import com.consola.lis.model.repository.LoanRepository;
import com.consola.lis.util.exception.AlreadyExistsException;
import com.consola.lis.util.exception.IllegalParameterInRequest;
Expand All @@ -27,6 +28,7 @@ public class LoanService {

private final LoanRepository loanRepository;
private final InventoryItemService inventoryItemService;
private final InventoryItemRepository inventoryItemRepository;

public Loan createLoan(LoanDTO loanRequest) {
validateLoanRequest(loanRequest);
Expand Down Expand Up @@ -68,12 +70,16 @@ private Loan buildLoanFromRequest(LoanDTO loanRequest) {
if (loanRequest.getLoanType() == null) {
loanRequest.setLoanType(LoanType.GENERAL);
}
Optional<InventoryItem> item = inventoryItemRepository.findById(loanRequest.getItemId());


return Loan.builder()
.itemId(loanRequest.getItemId())
.loanType(LoanType.valueOf(loanRequest.getLoanType().name()))
.borrowerUser(loanRequest.getBorrowerUser())
.lenderUser(loanRequest.getLenderUser())
.item(loanRequest.getItemId())
.category(item.get().getCategory().getCategoryName())
.quantity(loanRequest.getQuantity())
.loanState(LoanState.ACTIVE)
.observation(loanRequest.getObservation())
Expand Down Expand Up @@ -104,8 +110,10 @@ public List<Loan> getAllLoans() {



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

public Map<String, Object> getAllLoansMapper(LoanState loanState,Pageable pageable) {

Page<Loan> loanPage = getAllLoans(loanState, pageable);

Map<String, Object> result = new HashMap<>();
result.put("totalElements", loanPage.getTotalElements());
Expand All @@ -117,12 +125,15 @@ public Map<String, Object> getAllLoansMapper(Pageable pageable) {

private List<LoanInfoDTO> mapToLoanInfoList(List<Loan> loans) {
return loans.stream()
.map(loan -> LoanMapper.mapLoanToDTO(loan, inventoryItemService.findInventoryItem(loan.getItemId())))
.map(loan -> {
InventoryItem item = loan.getItemId() != null ? inventoryItemService.findInventoryItem(loan.getItemId()) : null;
return LoanMapper.mapLoanToDTO(loan, item);
})
.toList();
}

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

public List<String> getHeaders() {
Expand All @@ -139,7 +150,7 @@ public List<String> getHeaders() {

public void updateReturnLoanState (int loanId, LoanState state) {
Loan existingLoan = loanRepository.findById(loanId)
.orElseThrow(() -> new NotExistingException("409", HttpStatus.CONFLICT, "Item not exists into inventary"));
.orElseThrow(() -> new NotExistingException("409", HttpStatus.CONFLICT, "Item not exists into inventory"));

existingLoan.setLoanState(state);
loanRepository.save(existingLoan);
Expand Down
Loading
Loading