Beispiel #1
0
        public IActionResult CreateLending(int clientId, int libraryId, LendingForCreateDto lending)
        {
            if (!_repo.ClientExists(clientId) || !_repo.BookExists(lending.BookId))
            {
                return(NotFound());
            }
            var bookId  = lending.BookId;
            var library = _repo.GetLibrary(libraryId);

            if (library != null && lending.ReturnDate == null)
            {
                var bookCopies = _repo.GetBookCopiesForLibrary(libraryId).SingleOrDefault(b => b.BookId == bookId);
                if (bookCopies == null || bookCopies.NumberOfCopies < 1)
                {
                    return(NotFound());
                }
                bookCopies.NumberOfCopies--;
                _repo.UpdateBookCopies(bookCopies);
            }
            if (lending.WitdrawDate == null)
            {
                lending.WitdrawDate = DateTime.Now;
            }
            var lendingEntity = _mapper.Map <Lending>(lending);

            if (lending.ReturnDate == null)
            {
                lendingEntity.ReturnDate = null;
            }
            _repo.CreateLending(clientId, lendingEntity);
            _repo.Save();
            var lendingForReturn = _mapper.Map <LendingForReturnDto>(lendingEntity);

            return(CreatedAtRoute("GetLending", new { clientId, id = lendingForReturn.Id }, lendingForReturn));
        }