This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from UNIR-WG/feature/loans_api
ADDED: Adding Loan Controller, create and getall methods
- Loading branch information
Showing
3 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
.../net/unir/missi/desarrollowebfullstack/bookabook/operador/controller/LoansController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package net.unir.missi.desarrollowebfullstack.bookabook.operador.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Parameter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
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; | ||
|
||
import org.apache.http.protocol.ResponseServer; | ||
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.util.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class LoansController { | ||
@Autowired | ||
private final LoanService service; | ||
|
||
@GetMapping("/loans") | ||
public ResponseEntity<List<LoanResponse>> getAllLoans( | ||
@Parameter(name="bookId", example = "") | ||
@RequestParam(required = false) Long bookId, | ||
@Parameter(name="clientId", example = "") | ||
@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 = "") | ||
@RequestParam(required = false)Boolean isReturned, | ||
@Parameter(name="renewalCount", example = "") | ||
@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(); | ||
} | ||
} | ||
|
||
@PostMapping("/loans") | ||
public ResponseEntity<LoanResponse> addLoan(@RequestBody LoanRequest loanRequest) { | ||
try { | ||
if(loanRequest != null) { | ||
LoanResponse newLoan = service.createLoan(loanRequest); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(newLoan); | ||
} | ||
else | ||
{ | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
} catch(Exception e) { | ||
return ResponseEntity.internalServerError().build(); | ||
} | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
src/main/java/net/unir/missi/desarrollowebfullstack/bookabook/operador/model/sql/Loan.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters