public IActionResult UpdateLending(int id, int clientId, int libraryId, LendingForUpdateDto lending) { if (!_repo.ClientExists(clientId)) { return(NotFound()); } var lendingFromRepo = _repo.GetLending(clientId, id); if (lendingFromRepo == null) { return(NotFound()); } var library = _repo.GetLibrary(libraryId); if (library != null && lendingFromRepo.ReturnDate == null && lending.ReturnDate != null) { var bookCopies = _repo.GetBookCopiesForLibrary(libraryId) .SingleOrDefault(b => b.BookId == lendingFromRepo.BookId); if (bookCopies == null) { var newBookCopies = new BookCopies() { LibraryId = libraryId, BookId = lendingFromRepo.BookId, NumberOfCopies = 1 }; _repo.CreateBookCopies(libraryId, newBookCopies); } else { bookCopies.NumberOfCopies++; _repo.UpdateBookCopies(bookCopies); } } if (lending.WitdrawDate == null) { lending.WitdrawDate = lendingFromRepo.WitdrawDate; } _mapper.Map(lending, lendingFromRepo); _repo.UpdateLending(lendingFromRepo); _repo.Save(); return(NoContent()); }
public IActionResult CreateBookCopies(int libraryId, BookCopiesForCreationDto bookCopiesDto) { if (!_repo.LibraryExists(libraryId)) { return(NotFound()); } if (!_repo.BookExists(bookCopiesDto.BookId)) { return(NotFound()); } //check copy exists var bookCopiesEntity = _mapper.Map <BookCopies>(bookCopiesDto); _repo.CreateBookCopies(libraryId, bookCopiesEntity); _repo.Save(); var bookCopiesForReturn = _mapper.Map <BookCopiesForReturnDto>(bookCopiesEntity); return(CreatedAtRoute("GetBookCopyForLibrary", new { libraryId, id = bookCopiesEntity.BookId }, bookCopiesForReturn)); }