예제 #1
0
        public async Task <IActionResult> Borrow(int id)
        {
            var copy = _context.Copies.FirstOrDefault(c => c.Book.Id == id && c.IsAvailable);

            if (copy != null)
            {
                var borrowing = new Borrowing
                {
                    Copy            = copy,
                    DateOfBorrowing = DateTime.Today,
                    ApplicationUser = await _userManager.GetUserAsync(User)
                };

                await _context.Borrowings.AddAsync(borrowing);

                copy.IsAvailable = false;
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(Index)));
        }