Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from UNIR-WG/feature/loans_api
Browse files Browse the repository at this point in the history
Adding new features to Loans
  • Loading branch information
devmariodiaz authored Jan 31, 2024
2 parents 87c8cba + 1a5cd4a commit 1d4695c
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public Loan convert(LoanRequest source) {
return Loan.builder()
.bookId(source.getBookId())
.clientId(source.getClientId())
.loanDate(source.getLoanDate())
.dueDate(source.getDueDate())
.returnDate(source.getReturnDate())
.isReturned(source.getIsReturned())
.renewalCount(source.getRenewalCount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.exceptions.BadParametersException;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.exceptions.EntityInvalidOperationException;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.exceptions.EntityNotFoundException;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.LoanRequest;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.LoanResponse;
Expand All @@ -14,7 +15,9 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.*;
import java.util.logging.Logger;

@RestController
@RequiredArgsConstructor
Expand All @@ -30,11 +33,11 @@ public ResponseEntity<List<LoanResponse>> getLoans(
@Parameter(name="clientId", example = "")
@RequestParam(required = false) Long clientId,
@Parameter(name="loanDate", example = "")
@RequestParam(required = false)Date loanDate,
@RequestParam(required = false) LocalDate loanDate,
@Parameter(name="returnDate", example = "")
@RequestParam(required = false)Date returnDate,
@RequestParam(required = false)LocalDate returnDate,
@Parameter(name="dueDate", example = "")
@RequestParam(required = false)Date dueDate,
@RequestParam(required = false)LocalDate dueDate,
@Parameter(name="isReturned", example = "")
@RequestParam(required = false)Boolean isReturned,
@Parameter(name="renewalCount", example = "")
Expand Down Expand Up @@ -98,10 +101,15 @@ public ResponseEntity<LoanResponse> addLoan(@RequestBody LoanRequest loanRequest
}
catch (BadParametersException e)
{
Logger.getGlobal().warning("Bad Parameters");
return ResponseEntity.badRequest().build();
}
catch(EntityInvalidOperationException e) {
return ResponseEntity.badRequest().build();
}
catch (Exception e)
{
Logger.getGlobal().warning("Error: " + e.getMessage());
return ResponseEntity.internalServerError().build();
}
}
Expand Down Expand Up @@ -147,7 +155,7 @@ public ResponseEntity<LoanResponse> patchLoan(@RequestBody LoanRequest loanReque
}
}

@GetMapping("/loans/client/{clientId}")
@GetMapping("/clients/{clientId}/loans")
public ResponseEntity<List<LoanResponse>> getLoanByClientId(@PathVariable String clientId) {
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.exceptions;

import jakarta.ws.rs.NotAllowedException;

public class EntityInvalidOperationException extends RuntimeException {
public EntityInvalidOperationException(String errorMessage, Throwable err) { super(errorMessage, err); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.*;

import java.time.LocalDate;
import java.util.Date;

@Getter
Expand All @@ -13,9 +14,9 @@
public class LoanDto {
private Long bookId;
private Long clientId;
private Date loanDate;
private Date returnDate;
private Date dueDate;
private LocalDate loanDate;
private LocalDate returnDate;
private LocalDate dueDate;
private Boolean isReturned;
private Integer renewalCount;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api;
import lombok.*;

import java.time.LocalDate;
import java.util.Date;

@Getter
Expand All @@ -12,9 +13,7 @@
public class LoanRequest {
private Long bookId;
private Long clientId;
private Date loanDate;
private Date returnDate;
private Date dueDate;
private LocalDate returnDate = null;
private Boolean isReturned;
private Integer renewalCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.*;

import java.time.LocalDate;
import java.util.Date;

@Getter
Expand All @@ -14,9 +15,9 @@ public class LoanResponse {
private Long id;
private Long bookId;
private Long clientId;
private Date loanDate;
private Date returnDate;
private Date dueDate;
private LocalDate loanDate;
private LocalDate returnDate;
private LocalDate dueDate;
private Boolean isReturned;
private Integer renewalCount;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.model.sql;

import java.time.LocalDate;
import java.util.Date;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -32,11 +33,11 @@ public class Loan {
@Column(name = "clientId")
private Long clientId;
@Column(name = "loanDate")
private Date loanDate;
private LocalDate loanDate;
@Column(name = "returnDate")
private Date returnDate;
private LocalDate returnDate;
@Column(name = "dueDate")
private Date dueDate;
private LocalDate dueDate;
@Column(name = "isReturned")
private Boolean isReturned;
@Column(name = "renewalCount")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
@Repository
public interface LoanJpaRepository extends JpaRepository<Loan, Long>, JpaSpecificationExecutor<Loan> {
List<Loan> findByClientId(Long clientId);
List<Loan> findByBookIdAndIsReturned(Long bookId, boolean isRturned);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Optional;
Expand All @@ -26,6 +27,7 @@ public LoanRepository(LoanJpaRepository loanJpaRepository) {
public List<Loan> findAll() { return loanJpaRepository.findAll(); }
public Loan findById(Long id) { return loanJpaRepository.findById(id).orElse(null); }
public List<Loan> findByClientId(Long clientId){ return loanJpaRepository.findByClientId(clientId); }
public List<Loan> findByBookIdAndIsReturned(Long bookId, boolean isRturned){ return loanJpaRepository.findByBookIdAndIsReturned(bookId, isRturned); }
public Loan save(Loan loan){ return loanJpaRepository.save(loan); }
public Loan delete(Loan loan)
{
Expand All @@ -34,7 +36,7 @@ public Loan delete(Loan loan)
return l.get();
}

public List<Loan> search(Long bookId, Long clientId, Date loanDate, Date returnDate, Date dueDate, Boolean isReturned, Integer renewalCount) {
public List<Loan> search(Long bookId, Long clientId, LocalDate loanDate, LocalDate returnDate, LocalDate dueDate, Boolean isReturned, Integer renewalCount) {
SearchCriteria<Loan> spec = new SearchCriteria<>();

if(bookId != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.LoanRequest;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.LoanResponse;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

public interface ILoanService {
// CRUD
List<LoanResponse> getAllLoans(Long bookId, Long clientId, Date loanDate, Date returnDate, Date dueDate, Boolean isReturned, Integer renewalCount);
List<LoanResponse> getAllLoans(Long bookId, Long clientId, LocalDate loanDate, LocalDate returnDate, LocalDate dueDate, Boolean isReturned, Integer renewalCount);
LoanResponse createLoan(LoanRequest request);
LoanResponse getLoanById(Long id);
LoanResponse modifyAllLoanData(LoanRequest loan, Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.unir.missi.desarrollowebfullstack.bookabook.operador.config.LoanRequestToLoanConverter;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.config.LoanToLoanResponseConverter;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.exceptions.BadParametersException;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.exceptions.EntityInvalidOperationException;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.exceptions.EntityNotFoundException;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.BookResponse;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.ClientResponse;
Expand All @@ -19,6 +20,7 @@
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -51,7 +53,7 @@ public class LoanService implements ILoanService {
// CRUD

@Override
public List<LoanResponse> getAllLoans(Long bookId, Long clientId, Date loanDate, Date returnDate, Date dueDate, Boolean isReturned, Integer renewalCount) {
public List<LoanResponse> getAllLoans(Long bookId, Long clientId, LocalDate loanDate, LocalDate returnDate, LocalDate dueDate, Boolean isReturned, Integer renewalCount) {
Loan loan = new Loan(null, bookId, clientId, loanDate, returnDate, dueDate, isReturned, renewalCount);
List<Loan> loanList;
if (this.isValidSyntaxLoanForNulls(loan)) {
Expand All @@ -75,25 +77,30 @@ public LoanResponse createLoan(LoanRequest request) {
throw new BadParametersException("One or more parameters of the request are wrong", null);
}

Logger.getGlobal().warning("hasta aki");
// If loan referencing non-existing books throw 404
if (! isExistingBook(loan.getBookId().toString()))
{
throw new EntityNotFoundException("Book id " + loan.getBookId() + " does not exist.", null);
}

Logger.getGlobal().warning("hasta aki existe book");
// If loan referencing non-existing clients throw 404
if(! isExistingClient(loan.getClientId().toString()))
{
throw new EntityNotFoundException("Client id " + loan.getClientId() + " does not exist.", null);
}

Logger.getGlobal().warning("hasta aki existe client");
List<Loan> foundLoanedBook = loanRepository.findByBookIdAndIsReturned(request.getBookId(), false);
Logger.getGlobal().warning("Book Count: " + foundLoanedBook.stream().count());

if((long) foundLoanedBook.size() != 0) {
Logger.getGlobal().warning("Error: Book " + request.getBookId() + " cannot be loaned because it's already loaned to other client");
throw new EntityInvalidOperationException("Error: Book " + request.getBookId() + " cannot be loaned because it's already loaned to other client", null);
}

loan.setLoanDate(LocalDate.now());
loan.setDueDate(loan.getLoanDate().plusDays(7));

// Implicit else: valid loan is saved in the DB
Loan createdLoan = loanRepository.save(loan);
Logger.getGlobal().warning("hasta aki ya se ha guardao");
return this.loanToLoanResponseConverter.convert(createdLoan);
}

Expand Down Expand Up @@ -155,13 +162,11 @@ public LoanResponse modifyLoan(LoanRequest loanRequest, Long id) {
Loan loan = this.loanRequestToLoanConverter.convert(loanRequest);

// If loan has null values or wrong values throw 400
if (
! this.isValidSyntaxLoanForNulls(Objects.requireNonNull(loan))
if (!this.isValidSyntaxLoanForNulls(Objects.requireNonNull(loan))
|| ! this.isValidSyntaxLoanForZeroes(loan))
{
throw new BadParametersException("One or more parameters of the request are wrong", null);
}

// If loan does not exist throw 404
Loan loanMatched = loanRepository.findById(id);
if (loanMatched == null)
Expand All @@ -181,6 +186,15 @@ public LoanResponse modifyLoan(LoanRequest loanRequest, Long id) {
throw new EntityNotFoundException("Client id " + loan.getClientId() + " does not exist.", null);
}

if(loan.getIsReturned()) {
loan.setReturnDate(LocalDate.now());
}
else
{
loan.setDueDate(LocalDate.now().plusDays(7));
loan.setRenewalCount(loan.getRenewalCount() + 1);
}

// Update values of matched loan with values of received request
Loan mergedLoan = this.loanMergerNonEmpty.merge(loanMatched, loan);

Expand Down Expand Up @@ -222,18 +236,27 @@ public List<LoanResponse> getLoansByClientId(Long clientId) {
private boolean isExistingBook(String id) {
try {
ResponseEntity<BookResponse> book = buscadorClient.getBook(id);
Logger.getGlobal().warning("MyText");
return book != null;
}
catch(Exception e) {
return false;
}
}

private BookResponse getBook(String id) {
try {
ResponseEntity<BookResponse> book = buscadorClient.getBook(id);
BookResponse response = book.getBody();
return response;
}
catch(Exception e) {
return null;
}
}

private boolean isExistingClient(String id) {
try {
ResponseEntity<ClientResponse> client = buscadorClient.getClient(id);
Logger.getGlobal().warning("MyText");
return client != null;
}
catch(Exception e) {
Expand All @@ -245,9 +268,7 @@ private boolean isValidSyntaxLoanForNulls(Loan loan)
{
return loan.getBookId() != null
&& loan.getClientId() != null
&& loan.getLoanDate() != null
&& loan.getReturnDate() != null
&& loan.getDueDate() != null
//&& loan.getReturnDate() != null
&& loan.getIsReturned() != null
&& loan.getRenewalCount() != null;
}
Expand Down

0 comments on commit 1d4695c

Please sign in to comment.