Skip to content

Commit

Permalink
feat: last one mapping completed
Browse files Browse the repository at this point in the history
p.s. need to implement api gateway
  • Loading branch information
thisdudkin committed Sep 12, 2024
1 parent f48b4f8 commit 85745fd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Loan, Integer> {
Optional<Loan> findByUserIdAndBookIdAndReturnedAtIsNull(Integer bookId, Integer userId);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,5 +10,5 @@
public interface LoanApi {
ResponseEntity<LoanDto> addLoan(LoanDto dto, HttpServletRequest request);
ResponseEntity<LoanDto> getLoan(Integer loanId);
ResponseEntity<LoanDto> returnBook(Integer bookId, BookRecordDto dto);
ResponseEntity<LoanDto> returnBook(Integer bookId, HttpServletRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ public ResponseEntity<LoanDto> addLoan(@RequestBody LoanDto dto, HttpServletRequ
}

@Override
public ResponseEntity<LoanDto> getLoan(Integer loanId) {
return null;
@GetMapping("/{loanId}")
public ResponseEntity<LoanDto> getLoan(@PathVariable Integer loanId) {
return new ResponseEntity<>(loanService.getLoan(loanId), HttpStatus.OK);
}

@Override
public ResponseEntity<LoanDto> returnBook(Integer bookId, BookRecordDto dto) {
return null;
@PutMapping("/{bookId}")
public ResponseEntity<LoanDto> returnBook(@PathVariable Integer bookId, HttpServletRequest request) {
return new ResponseEntity<>(loanService.returnBook(bookId, request), HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 85745fd

Please sign in to comment.