Esempio n. 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var entity = await _dbContext.Authors.FindAsync(Id);

            if (entity == null)
            {
                return(BadRequest());
            }

            entity.FirstName = FirstName;
            entity.LastName  = LastName;
            entity.JobTitle  = JobTitle;
            entity.FullBio   = FullBio;
            entity.ShortBio  = ShortBio;
            entity.Permalink = Permalink;


            if (PhotoFile != null && PhotoFile.Length > 0)
            {
                entity.PhotoUrl = await _uploadManager.SaveAuthorImageAsync(PhotoFile);
            }

            await _dbContext.SaveChangesAsync();

            this.InformUser(FormResult.Updated, $"{entity.FirstName} {entity.LastName}", "author");
            return(RedirectToPage("Index"));
        }
Esempio n. 2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (Password != ConfirmPassword)
            {
                ModelState.AddModelError(nameof(ConfirmPassword), "Password must match confirm password");
                return(Page());
            }

            //save photo
            var photoUrl = await _uploadManager.SaveAuthorImageAsync(PhotoFile);

            var author = new Author()
            {
                FirstName = FirstName,
                LastName  = LastName,
                ShortBio  = ShortBio,
                FullBio   = FullBio,
                JobTitle  = JobTitle,
                Permalink = Permalink,
                PhotoUrl  = photoUrl
            };

            //Populate identity info
            //Try to save identity info first
            author.User = new ApplicationUser()
            {
                UserName  = Email,
                Email     = Email,
                FirstName = FirstName,
                LastName  = LastName,
                PhotoUrl  = photoUrl
            };

            await _dbContext.Authors.AddAsync(author);

            var userCreationResult = await _userManager.CreateAsync(author.User, Password);

            if (userCreationResult.Succeeded)
            {
                await _userManager.AddToRoleAsync(author.User, "Author");

                this.InformUser(FormResult.Added, $"{author.FirstName} {author.LastName}", "author");
                return(RedirectToPage("Index"));
            }
            else
            {
                foreach (var err in userCreationResult.Errors)
                {
                    ModelState.AddModelError("", err.Description);
                }

                return(Page());
            }
        }