Пример #1
0
        public async Task <IActionResult> CreateBook(AssetCreateBookViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = ProcessUploadedAssetFile(model);

                var book = _mapper.Map <Book>(model);

                book.Status   = _context.Statuses.FirstOrDefault(x => x.Name == "Available");
                book.ImageUrl = "/images/" + uniqueFileName;
                book.Location = _branch.GetBranchByName(model.LibraryBranchName);

                //Prevent exceptions while searching when the author of the book is unknown
                if (book.Author == null)
                {
                    book.Author = "-";
                }

                await _assetsService.AddAsync(book);

                return(RedirectToAction("Create", "Catalog"));
            }

            return(View(model));
        }
Пример #2
0
        public IActionResult CreateBook(AssetCreateBookViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = ProcessUploadedBookFile(model);

                var book = new Book
                {
                    Title          = model.Title,
                    Author         = model.Author,
                    ISBN           = model.ISBN,
                    Year           = model.Year,
                    Status         = _context.Statuses.FirstOrDefault(x => x.Name == "Available"),
                    Cost           = model.Cost,
                    ImageUrl       = "/images/" + uniqueFileName,
                    NumberOfCopies = model.NumberOfCopies,
                    Location       = _branch.GetBranchByName(model.LibraryBranchName)
                };

                //Prevent exceptions while searching when the author of the book is unknown
                if (book.Author == null)
                {
                    book.Author = "-";
                }

                _assetsService.Add(book);

                return(RedirectToAction("Create", "Catalog"));
            }

            return(View(model));
        }
        public async Task <IActionResult> Create(PatronCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = _mapper.Map <User>(model);

                user.LibraryCard = new LibraryCard
                {
                    Fees    = 0,
                    Created = DateTime.Now
                };

                user.HomeLibraryBranch = _branch.GetBranchByName(model.HomeLibraryBranchName);

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token = token }, Request.Scheme);

                    _logger.Log(LogLevel.Warning, confirmationLink);

                    var addToRoleResult = await _userManager.AddToRoleAsync(user, "Patron");

                    if (!addToRoleResult.Succeeded)
                    {
                        ModelState.AddModelError("", "Cannot add user to the Patron role.");
                        return(View(model));
                    }

                    BackgroundJob.Enqueue <IEmailService>(x => x.SendEmailAsync(user.FirstName, user.Email, "Email confirmation",
                                                                                $"Congratulations! You are registered. </br> This is your libraryCardId: {user.LibraryCard.Id} </br>" +
                                                                                $"<a href= \"{confirmationLink}\">Please confirm your email address " +
                                                                                $"by clicking this text</a>"));

                    if (_signInManager.IsSignedIn(User) && User.IsInRole("Admin"))
                    {
                        return(RedirectToAction("UsersList", "Administration"));
                    }

                    ViewBag.ErrorTitle   = "Registration successful";
                    ViewBag.ErrorMessage = $"This is your libraryCardId: {user.LibraryCard.Id}. Before you can log in," +
                                           $" please confirm your email, by clicking on the confirmation link " +
                                           $"we have emailed you.";

                    return(View("~/Views/Account/RegistrationSuccessful.cshtml"));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }

            return(View(model));
        }