Exemple #1
0
        private async Task <TodoApi.Models.Controller> CreateController(string controllerName)
        {
            if (!string.IsNullOrEmpty(controllerName))
            {
                TodoApi.Models.Controller controller = new TodoApi.Models.Controller()
                {
                    ControllerName = controllerName
                };
                _context.Controllers.Add(controller);
                await _context.SaveChangesAsync();

                return(controller);
            }
            else
            {
                return(null);
            }
        }
Exemple #2
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);
        }