public BookCheckoutDocument Put(int bookshelfId, int bookId, int borrowerId, BookCheckoutDocument document) { var bookShelf = bookShelfRepository.Get(bookshelfId); if (bookShelf == null) { throw new ResourceNotFoundException("Book shelf not Found"); } var book = bookRepository.Get(bookId); if (book == null) { throw new ResourceNotFoundException("Book not Found"); } var bookOnShelf = bookInShelfRepository.Query() .FirstOrDefault(x => x.BookShelfId == bookshelfId && x.BookId == bookId); if (bookOnShelf == null) { throw new ResourceNotFoundException("Book is not in the self specified"); } var borrower = borrowerRepository.Get(borrowerId); if (borrower == null) { throw new ResourceNotFoundException("Borrower not Found"); } var checkouts = checkoutRepository.Query().Where(x => x.BookOnShelfId == bookOnShelf.Id && (x.ReturnOn == null || x.ReturnOn > DateTime.Today)) .ToList(); if (checkouts.Any(x => x.BorrowerId != borrowerId)) { throw new ResourceNotFoundException("Book is already checked out to a diffrent borrower"); } checkouts.ForEach(c => checkoutRepository.Delete(c)); var checkout = new BookCheckout { BorrowerId = borrowerId, CheckedOutAt = document.CheckedOutAt, BookOnShelfId = bookOnShelf.Id, BookOnShelf = bookOnShelf, Comment = document.Comment, ReturnOn = document.ReturnOn, Borrower = borrower }; checkoutRepository.Save(checkout); return(Map(checkout)); }
public IHttpContentResult <BookCheckoutDocument> Put(int bookshelfId, int bookId, int borrowerId, BookCheckoutDocument document) { return(Request.CreateContentResponse(resource.Put(bookshelfId, bookId, borrowerId, document))); }