@POST
@Consumes("application/vnd.demo.library.loan+json")
@Produces("application/vnd.demo.library.loan+json")
@Linkable(value = LinkableIds.LOAN_NEW_ID, templateClass = LoanDto.class)
public Response newLoan(LoanDto loan) {
Book book = bookRepository.getBookById(loan.getBookId());
if (book.isBorrowed()) {
return Response.status(Status.CONFLICT).build();
}
Customer customer = customerRepository.getById(loan.getCustomerId());
book.setBorrowedBy(customer);
return HateoasResponse
.created(LinkableIds.LOAN_DETAILS_ID, book.getId())
.entity(loan)
.selfLink(LinkableIds.LOAN_DETAILS_ID, book.getId())
.link(LinkableIds.BOOK_DETAILS_ID, Rels.BOOK, book.getId())
.link(LinkableIds.CUSTOMER_DETAILS_ID, Rels.CUSTOMER, customer.getId())
.selfLink(LinkableIds.LOAN_RETURN_ID, book.getId()).build();
}