Example #1
0
        public async Task <IActionResult> Create([FromBody] CreatePersonViewModel person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { Message = "Invalid input(s)" }));
            }

            var userIdGUID = await GetCurrentUserIdAsync();

            var groupOfCurrentUser = await _context.Groups
                                     .Where(a => a.AppUser.IdentityGUID == userIdGUID &&
                                            a.GroupId == person.GroupId)
                                     .FirstOrDefaultAsync();

            if (groupOfCurrentUser == null)
            {
                return(BadRequest(new { Message = "Invalid input(s)" }));
            }

            var newPerson = new Person
            {
                PersonGroup = groupOfCurrentUser,
                LastName    = person.LastName,
                FirstName   = person.FirstName,
                ContactNo   = person.ContactNo
            };

            _context.People.Add(newPerson);
            await _context.SaveChangesAsync();

            return(Ok(new { Message = "Successfully created new person", NewPerson = newPerson }));
        }
        public async Task <IActionResult> Create([FromBody] CreateGroupViewModel @group)
        {
            if (ModelState.IsValid)
            {
                var userId = await GetCurrentUserIdAsync();

                var currentAppUser = await _context.ApplicationUsers
                                     .Where(u => u.IdentityGUID == userId)
                                     .FirstOrDefaultAsync();

                var newGroup = new Group
                {
                    AppUser   = currentAppUser,
                    GroupName = @group.GroupName
                };

                _context.Add(newGroup);
                await _context.SaveChangesAsync();

                return(Ok(new { Message = "Successfully create new group" }));
            }

            return(BadRequest(ModelState));
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new IdentityUser
                {
                    UserName = Input.Email,
                    Email    = Input.Email,

                    //  CONFIRMED to true so no need to confirm email
                    EmailConfirmed = true
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    //  Create new app user
                    var appUser = new ApplicationUser
                    {
                        IdentityGUID = user.Id,
                        LastName     = Input.LastName,
                        FirstName    = Input.FirstName
                    };

                    _attendanceCheckDbContext.ApplicationUsers.Add(appUser);
                    await _attendanceCheckDbContext.SaveChangesAsync();

                    _logger.LogInformation("User created a new account with password.");

                    return(RedirectToAction("Index", "Home"));

                    /*
                     * var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                     * code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                     * var callbackUrl = Url.Page(
                     *  "/Account/ConfirmEmail",
                     *  pageHandler: null,
                     *  values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                     *  protocol: Request.Scheme);
                     *
                     * await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                     *  $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
                     *
                     * if (_userManager.Options.SignIn.RequireConfirmedAccount)
                     * {
                     *  return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl });
                     * }
                     * else
                     * {
                     *  await _signInManager.SignInAsync(user, isPersistent: false);
                     *  return LocalRedirect(returnUrl);
                     * }
                     */
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }