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 #4 from UNIR-WG/feature/loans_api
Browse files Browse the repository at this point in the history
Feature/loans api
  • Loading branch information
devmariodiaz authored Jan 24, 2024
2 parents dc3ee71 + 940703d commit 4ae388c
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.LoanResponse;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.service.LoanService;

import org.apache.http.protocol.ResponseServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -53,6 +52,9 @@ public ResponseEntity<List<LoanResponse>> getAllLoans(
public ResponseEntity<LoanResponse> addLoan(@RequestBody LoanRequest loanRequest) {
try {
if(loanRequest != null) {
if(!service.validateLoan(loanRequest)) {
return ResponseEntity.notFound().build();
}
LoanResponse newLoan = service.createLoan(loanRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(newLoan);
}
Expand All @@ -64,4 +66,26 @@ public ResponseEntity<LoanResponse> addLoan(@RequestBody LoanRequest loanRequest
return ResponseEntity.internalServerError().build();
}
}

@GetMapping("/loans/{id}")
public ResponseEntity<LoanResponse> getLoanById(Long id) {
try {
LoanResponse response = service.getLoanById(id);
return ResponseEntity.ok(response);
}
catch(Exception e) {
return ResponseEntity.internalServerError().build();
}
}

@GetMapping("/loans/client/{clientId}")
public ResponseEntity<List<LoanResponse>> getLoanByClientId(Long clientId) {
try {
List<LoanResponse> response = service.getLoansByClientId(clientId);
return ResponseEntity.ok(Objects.requireNonNullElse(response, Collections.emptyList()));
}
catch(Exception e) {
return ResponseEntity.internalServerError().build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.service;

import lombok.extern.slf4j.Slf4j;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.clients.BuscadorClient;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.BookResponse;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.ClientResponse;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.LoanRequest;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.LoanResponse;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.sql.Loan;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.repository.LoanRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

@Service
@Slf4j
Expand All @@ -26,6 +32,9 @@ public class LoanService implements ILoanService {

@Autowired
private LoanRepository loanRepository;

@Autowired
private BuscadorClient buscadorClient;
@Override
public List<LoanResponse> getAllLoans(Long bookId, Long clientId, Date loanDate, Date returnDate, Date dueDate, Boolean isReturned, Integer renewalCount) throws RuntimeException {

Expand All @@ -42,26 +51,7 @@ public List<LoanResponse> getAllLoans(Long bookId, Long clientId, Date loanDate,
else
loanList = loanRepository.findAll();

List<LoanResponse> loansResponse = new ArrayList<>();

if(loanList != null) {
for(Loan loan : loanList) {
LoanResponse loanResponse = LoanResponse.builder()

.bookId(loan.getBookId())
.clientId(loan.getClientId())
.loanDate(loan.getLoanDate())
.dueDate(loan.getDueDate())
.returnDate(loan.getReturnDate())
.isReturned(loan.getIsReturned())
.renewalCount(loan.getRenewalCount())
.build();

loansResponse.add(loanResponse);
}
}

return loansResponse.isEmpty() ? null : loansResponse;
return getLoanResponses(loanList);
}
catch(Exception e) {
throw new RuntimeException("Database failed: " + e.getMessage());
Expand All @@ -77,44 +67,118 @@ public LoanResponse createLoan(LoanRequest request) throws RuntimeException {
&& request.getDueDate() != null
&& request.getReturnDate() != null
&& request.getIsReturned() != null
&& request.getRenewalCount() != null

) {
Loan newLoan = Loan.builder()
.bookId(request.getBookId())
.clientId(request.getClientId())
.loanDate(request.getLoanDate())
.dueDate(request.getDueDate())
.returnDate(request.getReturnDate())
.isReturned(request.getIsReturned())
.renewalCount(request.getRenewalCount())
.build();

Loan createdLoan = loanRepository.save(newLoan);
&& request.getRenewalCount() != null) {

boolean bookExists = validateBookExists(request.getBookId().toString());

if(!bookExists) {
return null;
}

Loan newLoan = Loan.builder()
.bookId(request.getBookId())
.clientId(request.getClientId())
.loanDate(request.getLoanDate())
.dueDate(request.getDueDate())
.returnDate(request.getReturnDate())
.isReturned(request.getIsReturned())
.renewalCount(request.getRenewalCount())
.build();

return LoanResponse.builder()
.id(createdLoan.getId())
.bookId(createdLoan.getBookId())
.clientId(createdLoan.getClientId())
.loanDate(createdLoan.getLoanDate())
.returnDate(createdLoan.getReturnDate())
.dueDate(createdLoan.getDueDate())
.isReturned(createdLoan.getIsReturned())
.renewalCount(createdLoan.getRenewalCount()).build();
Loan createdLoan = loanRepository.save(newLoan);

}
return LoanResponse.builder()
.id(createdLoan.getId())
.bookId(createdLoan.getBookId())
.clientId(createdLoan.getClientId())
.loanDate(createdLoan.getLoanDate())
.returnDate(createdLoan.getReturnDate())
.dueDate(createdLoan.getDueDate())
.isReturned(createdLoan.getIsReturned())
.renewalCount(createdLoan.getRenewalCount()).build();

return null;
}
return null;
}

@Override
public LoanResponse getLoanById(Long id) throws RuntimeException {
Loan loan = loanRepository.findById(id);
if(loan != null) {
return LoanResponse.builder()
.id(loan.getId())
.bookId(loan.getBookId())
.clientId(loan.getClientId())
.loanDate(loan.getLoanDate())
.dueDate(loan.getDueDate())
.returnDate(loan.getReturnDate())
.isReturned(loan.getIsReturned())
.renewalCount(loan.getRenewalCount())
.build();
}
return null;
}

@Override
public List<LoanResponse> getLoansByClientId(Long clientId) throws RuntimeException {
return null;
List<Loan> loansList = loanRepository.findByClientId(clientId);
return getLoanResponses(loansList);
}

private List<LoanResponse> getLoanResponses(List<Loan> loansList) {
List<LoanResponse> loansResponse = new ArrayList<>();

if(loansList != null) {
for(Loan loan : loansList) {
LoanResponse loanResponse = LoanResponse.builder()
.id(loan.getId())
.bookId(loan.getBookId())
.clientId(loan.getClientId())
.loanDate(loan.getLoanDate())
.dueDate(loan.getDueDate())
.returnDate(loan.getReturnDate())
.isReturned(loan.getIsReturned())
.renewalCount(loan.getRenewalCount())
.build();

loansResponse.add(loanResponse);
}
}

return loansResponse.isEmpty() ? null : loansResponse;
}

private boolean validateBookExists(String id) {
try {
ResponseEntity<BookResponse> book = buscadorClient.getBook(id);
return true;
}
catch(Exception e) {
Logger.getGlobal().warning(e.getStackTrace().toString());
return false;
}
}

private boolean validateClientExists(String id) {
try {
ResponseEntity<ClientResponse> client = buscadorClient.getClient(id);
return true;
}
catch(Exception e) {
Logger.getGlobal().warning(e.getStackTrace().toString());
return false;
}
}
public boolean validateLoan(LoanRequest request) {
try {
validateClientExists(request.getClientId().toString());
validateBookExists(request.getBookId().toString());
return true;
}
catch(Exception e) {
Logger.getGlobal().warning(e.getMessage().toString());
return false;
}
}

@Override
Expand Down

0 comments on commit 4ae388c

Please sign in to comment.