Пример #1
0
        public async Task <IActionResult> CheckoutBook(string Id)//vm
        {
            if (Id == null)
            {
                ViewBag.ErrorTitle   = $"You are tring to CheckOut a book with invalid model state!";
                ViewBag.ErrorMessage = "Book Id cannot be null!";
                return(View("Error"));
            }
            ClaimsPrincipal currUser = this.User;
            var             userId   = currUser.FindFirst(ClaimTypes.NameIdentifier).Value;
            //var user = await  _userManager.GetUserAsync(User);

            var book = await _bookService.FindByIdAsync(Id);

            if (book == null)
            {
                ViewBag.ErrorTitle = $"You are tring to CheckOut a book with invalid model state!";
                return(View("Error"));
            }
            if (book.Copies <= 0)
            {
                ViewBag.ErrorTitle = $"You are tring to CheckOut a book without copies available !";
                return(View("Error"));
            }
            var hr = await _historyService.CheckoutBookAsync(Id, userId);

            var checkoutBookVm = new CheckoutBookViewModel
            {
                Title               = book.Title,
                AuthorName          = book.Author.Name,
                Country             = book.Country,
                ReturnDate          = hr.ReturnDate,
                SubjectCategoryName = book.SubjectCategory.Name,
                Pages               = book.Pages,
                Year          = book.Year,
                Language      = book.Language,
                CoverImageUrl = book.CoverImageUrl
            };
            //TODO: try-catch!!!!! ( catch exeption (return BadRequest(ex msg)) !!!
            var user = await _userManager.GetUserAsync(User);

            var username = user.UserName;

            var notificationDescription = _notificationManager.CheckOutBookDescription(username, book.Title);
            var notification            = await _notificationService.CreateNotificationAsync(notificationDescription, username);

            _toast.AddSuccessToastMessage("You checked out book successfully!");
            return(View(checkoutBookVm));
        }