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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AleixMT committed Feb 22, 2024
2 parents 5c355c2 + 0f485f7 commit 182659e
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 61 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
@@ -1,20 +1,28 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
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;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.sql.Loan;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.service.LoanService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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 @@ -24,20 +32,27 @@ public class LoansController {
private final LoanService service;

@GetMapping("/loans")
@Operation(
operationId = "Obtener préstamos",
description = "Operacion de lectura",
summary = "Se devuelve una lista de todos los préstamos almacenados en la base de datos.")
@ApiResponse(
responseCode = "200",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Loan.class)))
public ResponseEntity<List<LoanResponse>> getLoans(
@Parameter(name="bookId", example = "")
@Parameter(name="bookId", description = "Id del Libro préstado")
@RequestParam(required = false) Long bookId,
@Parameter(name="clientId", example = "")
@Parameter(name="clientId", description = "Id del Cliente al que se le realizó el préstamo")
@RequestParam(required = false) Long clientId,
@Parameter(name="loanDate", example = "")
@RequestParam(required = false)Date loanDate,
@Parameter(name="returnDate", example = "")
@RequestParam(required = false)Date returnDate,
@Parameter(name="dueDate", example = "")
@RequestParam(required = false)Date dueDate,
@Parameter(name="isReturned", example = "")
@Parameter(name="loanDate", description = "Fecha de registro del préstamo")
@RequestParam(required = false) LocalDate loanDate,
@Parameter(name="returnDate", description = "Fecha en que el libro fue devuelto")
@RequestParam(required = false)LocalDate returnDate,
@Parameter(name="dueDate", description = "Fecha de vencimiento del préstamo")
@RequestParam(required = false)LocalDate dueDate,
@Parameter(name="isReturned", description = "Indica si el libro ha sido devuelto")
@RequestParam(required = false)Boolean isReturned,
@Parameter(name="renewalCount", example = "")
@Parameter(name="renewalCount", description = "Cantidad de renovaciones del préstamo")
@RequestParam(required = false)Integer renewalCount
) {

Expand All @@ -61,6 +76,17 @@ public ResponseEntity<List<LoanResponse>> getLoans(
}

@GetMapping("/loans/{id}")
@Operation(
operationId = "Obtener un préstamo",
description = "Operacion de lectura",
summary = "Se devuelve un préstamo a partir de su identificador.")
@ApiResponse(
responseCode = "200",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Loan.class)))
@ApiResponse(
responseCode = "404",
content = @Content(mediaType = "application/json", schema = @Schema()),
description = "No se ha encontrado el préstamo con el identificador indicado.")
public ResponseEntity<LoanResponse> getLoan(@PathVariable String id) {
try
{
Expand All @@ -82,6 +108,25 @@ public ResponseEntity<LoanResponse> getLoan(@PathVariable String id) {
}

@PostMapping("/loans")
@Operation(
operationId = "Crear un préstamo",
description = "Operacion de escritura",
summary = "Se crea un préstamo a partir de sus datos.",
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "Datos del préstamo a crear.",
required = true,
content = @Content(mediaType = "application/json", schema = @Schema(implementation = LoanRequest.class))))
@ApiResponse(
responseCode = "201",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = LoanResponse.class)))
@ApiResponse(
responseCode = "400",
content = @Content(mediaType = "application/json", schema = @Schema()),
description = "Datos incorrectos introducidos.")
@ApiResponse(
responseCode = "404",
content = @Content(mediaType = "application/json", schema = @Schema()),
description = "Id del Libro O Id del Cliente no existen.")
public ResponseEntity<LoanResponse> addLoan(@RequestBody LoanRequest loanRequest) {
try
{
Expand All @@ -100,13 +145,28 @@ public ResponseEntity<LoanResponse> addLoan(@RequestBody LoanRequest loanRequest
{
return ResponseEntity.badRequest().build();
}
catch(EntityInvalidOperationException e) {
return ResponseEntity.badRequest().build();
}
catch (Exception e)
{
Logger.getGlobal().warning("Error: " + e.getMessage());
return ResponseEntity.internalServerError().build();
}
}

@DeleteMapping("/loans/{id}")
@Operation(
operationId = "Eliminar un préstamo",
description = "Operacion de escritura",
summary = "Se elimina un préstamo a partir de su identificador.")
@ApiResponse(
responseCode = "200",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = LoanResponse.class)))
@ApiResponse(
responseCode = "404",
content = @Content(mediaType = "application/json", schema = @Schema()),
description = "No se ha encontrado el préstamo con el identificador indicado.")
public ResponseEntity<LoanResponse> deleteLoan(@PathVariable String id) {
try
{
Expand All @@ -123,14 +183,30 @@ public ResponseEntity<LoanResponse> deleteLoan(@PathVariable String id) {
}

@PatchMapping("/loans/{id}")
@Operation(
operationId = "Modificar parcialmente un préstamo",
description = "RFC 7386. Operacion de escritura",
summary = "RFC 7386. Se modifica parcialmente un préstamo.",
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "Datos del préstamo a modificar.",
required = true,
content = @Content(mediaType = "application/merge-patch+json", schema = @Schema(implementation = LoanRequest.class))))
@ApiResponse(
responseCode = "200",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = LoanResponse.class)))
@ApiResponse(
responseCode = "400",
content = @Content(mediaType = "application/json", schema = @Schema()),
description = "Datos incorrectos introducidos.")
public ResponseEntity<LoanResponse> patchLoan(@RequestBody LoanRequest loanRequest, @PathVariable String id) {
try
{
Logger.getGlobal().warning("Entra al controller" + loanRequest);
if (loanRequest == null)
{
return ResponseEntity.badRequest().build();
}
LoanResponse newLoan = this.service.modifyLoan(loanRequest, Long.valueOf(id));
LoanResponse newLoan = this.service.modifyLoan(loanRequest.getIsReturned(), Long.valueOf(id));
return ResponseEntity.status(HttpStatus.CREATED).body(newLoan);
}
catch (EntityNotFoundException e)
Expand All @@ -147,7 +223,18 @@ public ResponseEntity<LoanResponse> patchLoan(@RequestBody LoanRequest loanReque
}
}

@GetMapping("/loans/client/{clientId}")
@GetMapping("/clients/{clientId}/loans")
@Operation(
operationId = "Obtener los préstamos de un cliente.",
description = "Operacion de lectura",
summary = "Se devuelve una lista de préstamos relacionados a un cliente.")
@ApiResponse(
responseCode = "200",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = LoanResponse.class)))
@ApiResponse(
responseCode = "404",
content = @Content(mediaType = "application/json", schema = @Schema()),
description = "No se han encontrado préstamos con el identificador de clienteindicado.")
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 Boolean isReturned;
private Integer renewalCount;
private LocalDate returnDate = null;
private Boolean isReturned = false;
private Integer renewalCount = 0;
}
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,16 +3,17 @@
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);
LoanResponse modifyLoan(LoanRequest loan, Long id);
LoanResponse modifyLoan(boolean isReturned, Long id);
LoanResponse deleteLoan(String id);

// SPECIALIZATIONS
Expand Down
Loading

0 comments on commit 182659e

Please sign in to comment.