Exemplo n.º 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));
        }
        public static IReadOnlyCollection <CheckoutBookViewModel> MapToCheckOutViewModel(this IDictionary <Book, DateTime> checkOuts)
        {
            var checkOutsOfUserVm = new List <CheckoutBookViewModel>();

            foreach (var checkoutPair in checkOuts)
            {
                //всеки път new , защото е референтен тип и се променят старите иначе.. :)
                var viewModel = new CheckoutBookViewModel();
                viewModel.ReturnDate          = checkoutPair.Value;
                viewModel.Id                  = checkoutPair.Key.Id;
                viewModel.Title               = checkoutPair.Key.Title;
                viewModel.AuthorName          = checkoutPair.Key.Author.Name;
                viewModel.Country             = checkoutPair.Key.Country;
                viewModel.Pages               = checkoutPair.Key.Pages;
                viewModel.Year                = checkoutPair.Key.Year;
                viewModel.Language            = checkoutPair.Key.Language;
                viewModel.Copies              = checkoutPair.Key.Copies;
                viewModel.SubjectCategoryName = checkoutPair.Key.SubjectCategory.Name;
                viewModel.CoverImageUrl       = checkoutPair.Key.CoverImageUrl;
                checkOutsOfUserVm.Add(viewModel);
            }
            return(checkOutsOfUserVm);
        }