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

Commit

Permalink
New Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
devmariodiaz committed Jan 29, 2024
1 parent be8d013 commit c326204
Show file tree
Hide file tree
Showing 16 changed files with 565 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
book-a-book-eureka:
image: aleixmt/book-a-book-eureka:latest
ports:
- "0.0.0.0:8761:8761"
- "0.0.0.0:8762:8761"
restart: unless-stopped
pull_policy: always
healthcheck:
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>13.2</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.clients;

import net.unir.missi.desarrollowebfullstack.bookabook.operador.config.FeignConfig;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.*;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
Expand All @@ -9,9 +10,10 @@
import java.util.List;
import java.util.Map;

@FeignClient(name = "buscador")
@FeignClient(name = "buscador", url = "http://localhost:8082/",configuration = FeignConfig.class)
public interface BuscadorClient {


// Authors

@GetMapping("/authors")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.config;

import org.springframework.cloud.openfeign.EnableFeignClients;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {

@Bean
Logger.Level feingLoggerLevel(){ return Logger.Level.FULL; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.config;

import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.sql.Loan;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LoanMerger {
public Loan merge(Loan destiny, Loan source)
{
destiny.setBookId(source.getBookId());
destiny.setClientId(source.getClientId());
destiny.setDueDate(source.getDueDate());
destiny.setLoanDate(source.getLoanDate());
destiny.setIsReturned(source.getIsReturned());
destiny.setRenewalCount(source.getRenewalCount());
destiny.setReturnDate(source.getReturnDate());
return destiny;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.config;

import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.sql.Loan;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LoanMergerNonEmpty {
public Loan merge(Loan destiny, Loan source)
{
if (source.getBookId() != null && source.getBookId() != 0)
{
destiny.setBookId(source.getBookId());
}

if (source.getClientId() != null && source.getClientId() != 0)
{
destiny.setClientId(source.getClientId());
}

if (source.getDueDate() != null)
{
destiny.setDueDate(source.getDueDate());
}

if (source.getLoanDate() != null)
{
destiny.setLoanDate(source.getLoanDate());
}

if (source.getIsReturned() != null)
{
destiny.setIsReturned(source.getIsReturned());
}

if (source.getRenewalCount() != null)
{
destiny.setRenewalCount(source.getRenewalCount());
}

if (source.getReturnDate() != null)
{
destiny.setReturnDate(source.getReturnDate());
}
return destiny;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.config;

import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.LoanRequest;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.sql.Loan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

@Configuration
public class LoanRequestToLoanConverter implements Converter<LoanRequest, Loan> {
@Override
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())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.config;

import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.api.LoanResponse;
import net.unir.missi.desarrollowebfullstack.bookabook.operador.model.sql.Loan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

@Configuration
public class LoanToLoanResponseConverter implements Converter<Loan, LoanResponse> {
@Override
public LoanResponse convert(Loan source) {
return LoanResponse.builder()
.id(source.getId())
.bookId(source.getBookId())
.clientId(source.getClientId())
.loanDate(source.getLoanDate())
.dueDate(source.getDueDate())
.returnDate(source.getReturnDate())
.isReturned(source.getIsReturned())
.renewalCount(source.getRenewalCount())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.swagger.v3.oas.annotations.Parameter;
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.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.service.LoanService;
Expand All @@ -22,7 +24,7 @@ public class LoansController {
private final LoanService service;

@GetMapping("/loans")
public ResponseEntity<List<LoanResponse>> getAllLoans(
public ResponseEntity<List<LoanResponse>> getLoans(
@Parameter(name="bookId", example = "")
@RequestParam(required = false) Long bookId,
@Parameter(name="clientId", example = "")
Expand All @@ -39,52 +41,129 @@ public ResponseEntity<List<LoanResponse>> getAllLoans(
@RequestParam(required = false)Integer renewalCount
) {

try {
List<LoanResponse> response = service.getAllLoans(bookId, clientId, loanDate, returnDate, dueDate, isReturned, renewalCount);
return ResponseEntity.ok(Objects.requireNonNullElse(response, Collections.emptyList()));
}
catch(Exception e) {
return ResponseEntity.internalServerError().build();
}
try
{
List<LoanResponse> response = this.service.getAllLoans(bookId, clientId, loanDate, returnDate, dueDate, isReturned, renewalCount);
return ResponseEntity.ok(Objects.requireNonNullElse(response, Collections.emptyList()));
}
catch (EntityNotFoundException e)
{
return ResponseEntity.notFound().build();
}
catch (BadParametersException e)
{
return ResponseEntity.badRequest().build();
}
catch (Exception e)
{
return ResponseEntity.internalServerError().build();
}
}

@GetMapping("/loans/{id}")
public ResponseEntity<LoanResponse> getLoan(@PathVariable String id) {
try
{
LoanResponse response = this.service.getLoanById(Long.valueOf(id));
return ResponseEntity.ok(response);
}
catch (EntityNotFoundException e)
{
return ResponseEntity.notFound().build();
}
catch (BadParametersException e)
{
return ResponseEntity.badRequest().build();
}
catch (Exception e)
{
return ResponseEntity.internalServerError().build();
}
}

@PostMapping("/loans")
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);
}
else
try
{
if (loanRequest == null)
{
return ResponseEntity.badRequest().build();
}
} catch(Exception e) {
LoanResponse newLoan = this.service.createLoan(loanRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(newLoan);
}
catch (EntityNotFoundException e)
{
return ResponseEntity.notFound().build();
}
catch (BadParametersException e)
{
return ResponseEntity.badRequest().build();
}
catch (Exception e)
{
return ResponseEntity.internalServerError().build();
}
}

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

@PatchMapping("/loans/{id}")
public ResponseEntity<LoanResponse> patchLoan(@RequestBody LoanRequest loanRequest, @PathVariable String id) {
try
{
if (loanRequest == null)
{
return ResponseEntity.badRequest().build();
}
LoanResponse newLoan = this.service.modifyLoan(loanRequest, Long.valueOf(id));
return ResponseEntity.status(HttpStatus.CREATED).body(newLoan);
}
catch(Exception e) {
catch (EntityNotFoundException e)
{
return ResponseEntity.notFound().build();
}
catch (BadParametersException e)
{
return ResponseEntity.badRequest().build();
}
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);
public ResponseEntity<List<LoanResponse>> getLoanByClientId(@PathVariable String clientId) {
try
{
List<LoanResponse> response = service.getLoansByClientId(Long.valueOf(clientId));
return ResponseEntity.ok(Objects.requireNonNullElse(response, Collections.emptyList()));
}
catch(Exception e) {
catch (EntityNotFoundException e)
{
return ResponseEntity.notFound().build();
}
catch (BadParametersException e)
{
return ResponseEntity.badRequest().build();
}
catch(Exception e)
{
return ResponseEntity.internalServerError().build();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.exceptions;

public class BadParametersException extends RuntimeException{
public BadParametersException(String errorMessage, Throwable err)
{
super(errorMessage, err);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.unir.missi.desarrollowebfullstack.bookabook.operador.exceptions;

public class EntityNotFoundException extends RuntimeException{
public EntityNotFoundException(String errorMessage, Throwable err)
{
super(errorMessage, err);
}
}
Loading

0 comments on commit c326204

Please sign in to comment.