From 85745fd0d3ff6c5753f27e41f1a4b4c07e2bb767 Mon Sep 17 00:00:00 2001 From: Alexander Dudkin Date: Thu, 12 Sep 2024 23:59:47 +0300 Subject: [PATCH] feat: last one mapping completed p.s. need to implement api gateway --- .../loans/config/LibraryClient.java | 2 +- .../loans/repository/LoanRepository.java | 3 +++ .../earlspilner/loans/rest/api/LoanApi.java | 3 +-- .../rest/controller/LoanRestController.java | 11 +++++---- .../loans/service/LoanService.java | 2 +- .../loans/service/LoanServiceImpl.java | 24 +++++++++++++++---- 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/library-api-loan-service/src/main/java/dev/earlspilner/loans/config/LibraryClient.java b/library-api-loan-service/src/main/java/dev/earlspilner/loans/config/LibraryClient.java index 1f24235..fa8e4cd 100644 --- a/library-api-loan-service/src/main/java/dev/earlspilner/loans/config/LibraryClient.java +++ b/library-api-loan-service/src/main/java/dev/earlspilner/loans/config/LibraryClient.java @@ -18,6 +18,6 @@ public interface LibraryClient { BookRecordDto getBookRecord(@PathVariable Integer bookId); @PutMapping("/{bookId}") - void setBookOnLoan(@PathVariable Integer bookId, @RequestBody BookRecordDto bookRecordDto); + void setBookStatus(@PathVariable Integer bookId, @RequestBody BookRecordDto bookRecordDto); } diff --git a/library-api-loan-service/src/main/java/dev/earlspilner/loans/repository/LoanRepository.java b/library-api-loan-service/src/main/java/dev/earlspilner/loans/repository/LoanRepository.java index 6e76759..83c16ca 100644 --- a/library-api-loan-service/src/main/java/dev/earlspilner/loans/repository/LoanRepository.java +++ b/library-api-loan-service/src/main/java/dev/earlspilner/loans/repository/LoanRepository.java @@ -3,8 +3,11 @@ import dev.earlspilner.loans.entity.Loan; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + /** * @author Alexander Dudkin */ public interface LoanRepository extends JpaRepository { + Optional findByUserIdAndBookIdAndReturnedAtIsNull(Integer bookId, Integer userId); } diff --git a/library-api-loan-service/src/main/java/dev/earlspilner/loans/rest/api/LoanApi.java b/library-api-loan-service/src/main/java/dev/earlspilner/loans/rest/api/LoanApi.java index 913cfc0..12c5cdb 100644 --- a/library-api-loan-service/src/main/java/dev/earlspilner/loans/rest/api/LoanApi.java +++ b/library-api-loan-service/src/main/java/dev/earlspilner/loans/rest/api/LoanApi.java @@ -1,6 +1,5 @@ package dev.earlspilner.loans.rest.api; -import dev.earlspilner.loans.dto.BookRecordDto; import dev.earlspilner.loans.dto.LoanDto; import jakarta.servlet.http.HttpServletRequest; import org.springframework.http.ResponseEntity; @@ -11,5 +10,5 @@ public interface LoanApi { ResponseEntity addLoan(LoanDto dto, HttpServletRequest request); ResponseEntity getLoan(Integer loanId); - ResponseEntity returnBook(Integer bookId, BookRecordDto dto); + ResponseEntity returnBook(Integer bookId, HttpServletRequest request); } diff --git a/library-api-loan-service/src/main/java/dev/earlspilner/loans/rest/controller/LoanRestController.java b/library-api-loan-service/src/main/java/dev/earlspilner/loans/rest/controller/LoanRestController.java index 0df41ec..c01d2b6 100644 --- a/library-api-loan-service/src/main/java/dev/earlspilner/loans/rest/controller/LoanRestController.java +++ b/library-api-loan-service/src/main/java/dev/earlspilner/loans/rest/controller/LoanRestController.java @@ -31,12 +31,15 @@ public ResponseEntity addLoan(@RequestBody LoanDto dto, HttpServletRequ } @Override - public ResponseEntity getLoan(Integer loanId) { - return null; + @GetMapping("/{loanId}") + public ResponseEntity getLoan(@PathVariable Integer loanId) { + return new ResponseEntity<>(loanService.getLoan(loanId), HttpStatus.OK); } @Override - public ResponseEntity returnBook(Integer bookId, BookRecordDto dto) { - return null; + @PutMapping("/{bookId}") + public ResponseEntity returnBook(@PathVariable Integer bookId, HttpServletRequest request) { + return new ResponseEntity<>(loanService.returnBook(bookId, request), HttpStatus.OK); } + } diff --git a/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanService.java b/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanService.java index c98a68d..9a2e992 100644 --- a/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanService.java +++ b/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanService.java @@ -10,5 +10,5 @@ public interface LoanService { LoanDto addLoan(LoanDto dto, HttpServletRequest request); LoanDto getLoan(Integer loanId); - LoanDto returnBook(Integer bookId, BookRecordDto dto); + LoanDto returnBook(Integer bookId, HttpServletRequest request); } diff --git a/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanServiceImpl.java b/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanServiceImpl.java index fd2f2d9..85e6f77 100644 --- a/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanServiceImpl.java +++ b/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanServiceImpl.java @@ -8,12 +8,16 @@ import dev.earlspilner.loans.entity.Loan; import dev.earlspilner.loans.mapper.LoanMapper; import dev.earlspilner.loans.repository.LoanRepository; +import dev.earlspilner.loans.rest.advice.LoanNotFoundException; import dev.earlspilner.loans.security.JwtCore; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.Instant; + +import static dev.earlspilner.loans.dto.BookStatus.IN_LIBRARY; import static dev.earlspilner.loans.dto.BookStatus.ON_LOAN; /** @@ -46,18 +50,30 @@ public LoanDto addLoan(LoanDto dto, HttpServletRequest request) { } UserDto userDto = userClient.getUser(jwtCore.getUsernameFromToken(jwtCore.getTokenFromRequest(request))); - libraryClient.setBookOnLoan(dto.bookId(), new BookRecordDto(null, ON_LOAN)); + libraryClient.setBookStatus(dto.bookId(), new BookRecordDto(null, ON_LOAN)); Loan loan = new Loan(userDto.id(), dto.bookId()); return loanMapper.toLoanDto(loanRepository.save(loan)); } @Override public LoanDto getLoan(Integer loanId) { - return null; + return loanRepository.findById(loanId) + .map(loanMapper::toLoanDto) + .orElseThrow(() -> new LoanNotFoundException("Loan not found with ID: " + loanId)); } @Override - public LoanDto returnBook(Integer bookId, BookRecordDto dto) { - return null; + public LoanDto returnBook(Integer bookId, HttpServletRequest request) { + BookRecordDto bookRecord = libraryClient.getBookRecord(bookId); + if (bookRecord == null || bookRecord.status() == IN_LIBRARY) { + throw new IllegalArgumentException("You can't return this book right now"); + } + + UserDto userDto = userClient.getUser(jwtCore.getUsernameFromToken(jwtCore.getTokenFromRequest(request))); + Loan loan = loanRepository.findByUserIdAndBookIdAndReturnedAtIsNull(bookId, userDto.id()) + .orElseThrow(() -> new LoanNotFoundException("Loan not found with bookId '" + bookId + "' and userId '" + userDto.id() + "'")); + loan.setReturnedAt(Instant.now()); + libraryClient.setBookStatus(bookId, new BookRecordDto(null, IN_LIBRARY)); + return loanMapper.toLoanDto(loanRepository.save(loan)); } }