private async Task LoadAsync(ProjectCars2User user)
        {
            var userName = await _userManager.GetUserNameAsync(user);

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            Username = userName;

            Input = new InputModel
            {
                GamerTag    = user.GamerTag,
                PhoneNumber = phoneNumber
            };
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new ProjectCars2User {
                    UserName = Input.Email, Email = Input.Email, GamerTag = Input.GamerTag
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    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 },
                        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 }));
                    }
                    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());
        }
Esempio n. 3
0
        public async Task <ActionResult <LapTimes> > PostLapTime(LapTimeDTO lapTimeDTO)
        {
            if (lapTimeDTO != null)
            {
                if (IsAnyNullOrEmpty(lapTimeDTO) == false)
                {
                    ProjectCars2User user = await _context.Users.FirstOrDefaultAsync(c => c.GamerTag == lapTimeDTO.GamerTag);

                    if (user != null)
                    {
                        CarClass carClass = await _context.CarClasses.FirstOrDefaultAsync(c => c.VehicleClass == lapTimeDTO.VehicleClass);

                        if (carClass == null)
                        {
                            carClass = await CreateCarClass(lapTimeDTO.VehicleClass);
                        }
                        if (carClass == null)
                        {
                            return(null);
                        }

                        Car car = await _context.Cars.FirstOrDefaultAsync(c => c.VehicleName == lapTimeDTO.VehicleName);

                        if (car == null)
                        {
                            car = await CreateCar(lapTimeDTO.VehicleName, carClass);
                        }
                        if (car == null)
                        {
                            return(null);
                        }

                        Track track = await _context.Tracks.FirstOrDefaultAsync(c => c.TrackLength == lapTimeDTO.TrackLength);

                        if (track == null)
                        {
                            track = await CreateTrack(lapTimeDTO.TrackLength, lapTimeDTO.TrackName);
                        }
                        if (track == null)
                        {
                            return(null);
                        }

                        SessionMode sessionMode = await _context.SessionModes.FirstOrDefaultAsync(c => c.SessionModeName == lapTimeDTO.SessionMode);

                        if (sessionMode == null)
                        {
                            sessionMode = await CreateSessionMode(lapTimeDTO.SessionMode);
                        }
                        if (sessionMode == null)
                        {
                            return(null);
                        }

                        Platform platform = await _context.Platforms.FirstOrDefaultAsync(c => c.PlatformName == lapTimeDTO.Platform);

                        if (platform == null)
                        {
                            platform = await CreatePlatform(lapTimeDTO.Platform);
                        }
                        if (platform == null)
                        {
                            return(null);
                        }

                        Setup setup = await _context.Setups.FirstOrDefaultAsync(c => c.SetupName == lapTimeDTO.Setup);

                        if (setup == null)
                        {
                            setup = await CreateSetup(lapTimeDTO.Setup);
                        }
                        if (setup == null)
                        {
                            return(null);
                        }

                        TodoApi.Models.Controller controller = await _context.Controllers.FirstOrDefaultAsync(c => c.ControllerName == lapTimeDTO.Controller);

                        if (controller == null)
                        {
                            controller = await CreateController(lapTimeDTO.Controller);
                        }
                        if (controller == null)
                        {
                            return(null);
                        }

                        Camera camera = await _context.Cameras.FirstOrDefaultAsync(c => c.CameraName == lapTimeDTO.Camera);

                        if (camera == null)
                        {
                            camera = await CreateCamera(lapTimeDTO.Camera);
                        }
                        if (camera == null)
                        {
                            return(null);
                        }

                        bool     isModified = false;
                        LapTimes lapTime    = await _context.LapTimes
                                              .Include(l => l.Track)
                                              .Include(l => l.Car)
                                              .Include(l => l.ProjectCars2User)
                                              .FirstOrDefaultAsync(l => l.ProjectCars2User.Email == user.Email && l.Track.TrackName == track.TrackName && l.Car.VehicleName == car.VehicleName);

                        if (lapTime != null)
                        {
                            isModified = true;
                        }
                        else
                        {
                            lapTime         = new LapTimes();
                            lapTime.LapTime = double.MaxValue;
                        }

                        if (lapTime.LapTime > lapTimeDTO.LapTime)
                        {
                            lapTime.ProjectCars2User = user;
                            lapTime.Camera           = camera;
                            lapTime.Car         = car;
                            lapTime.Controller  = controller;
                            lapTime.Platform    = platform;
                            lapTime.SessionMode = sessionMode;
                            lapTime.Setup       = setup;
                            lapTime.Track       = track;
                            lapTime.LapDate     = lapTimeDTO.LapDate;
                            lapTime.AmbTemp     = lapTimeDTO.AmbTemp;
                            lapTime.LapTime     = lapTimeDTO.LapTime;
                            lapTime.RainDensity = lapTimeDTO.RainDensity;
                            lapTime.Sector1     = lapTimeDTO.Sector1;
                            lapTime.Sector2     = lapTimeDTO.Sector2;
                            lapTime.Sector3     = lapTimeDTO.Sector3;
                            lapTime.TrackTemp   = lapTimeDTO.TrackTemp;
                        }

                        if (isModified == true)
                        {
                            _context.Entry(lapTime).State = EntityState.Modified;
                        }
                        else
                        {
                            _context.LapTimes.Add(lapTime);
                        }

                        await _context.SaveChangesAsync();

                        return(CreatedAtAction(nameof(GetLapTime), new { id = lapTime.Id }, lapTime));
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            return(null);
        }