public async Task <BestLapTime> Insert(BestLapTime bestLapTime) { using (ApplicationDbContext db = new ApplicationDbContext()) { bestLapTime.Id = string.IsNullOrEmpty(bestLapTime.Id) ? Guid.NewGuid().ToString() : bestLapTime.Id; bestLapTime = db.BestLapTimes.Add(bestLapTime); // register in context await db.SaveChangesAsync(); /* * LapTime lapTime = await db.LapTimes * .Where(l => l.Id == bestLapTime.LapTimeId) * .Include(l => l.DriverResult) * .Include(l => l.DriverResult.ApplicationUser) * .Include(l => l.DriverResult.Car) * .Include(l => l.DriverResult.RaceSession) * .Include(l => l.DriverResult.RaceSession.Track) * .FirstOrDefaultAsync(); * * TimeSpan time = bestLapTime.LapTime.Time; * DriverResult driverResult = bestLapTime.LapTime.DriverResult; * Car car = driverResult.Car; * ApplicationUser user = driverResult.ApplicationUser; * Track track = driverResult.RaceSession.Track; * * bool modified = false; * * // Check Track's overall record * if (string.IsNullOrEmpty(track.BestLapTimeId)|| track.BestLapTime.LapTime.Time > time) * { * track.BestLapTimeId = bestLapTime.Id; * modified = true; * } * * // Check this car's overall record * if (string.IsNullOrEmpty(car.BestLapTimeId) || car.BestLapTime.LapTime.Time > time) * { * car.BestLapTimeId = bestLapTime.Id; * modified = true; * } * * // Check this car's best time for this user * BestLapTime usersCarBestLap = car.BestLapTimes.Where(l => l.ApplicationUserId == user.Id).FirstOrDefault(); * if (usersCarBestLap == null) * { * modified = true; * user.BestLapTimes.Add(bestLapTime); * car.BestLapTimes.Add(bestLapTime); * } * else if (time < usersCarBestLap.LapTime.Time) * { * usersCarBestLap.LapTimeId = bestLapTime.Id; * modified = true; * } * * // If changes made then save best lap time * if (modified) * { * await db.SaveChangesAsync(); * } */ } return(bestLapTime); }
public async Task <HttpResponseMessage> PostLapTime(LapTimeDTO[] lapTimeDTOs) { if (!ModelState.IsValid) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } LapTimeDTO first = lapTimeDTOs.FirstOrDefault(); if (first != null) { DriverResultsRepository <DriverResult> driverResultsRepo = new DriverResultsRepository <DriverResult>(); CarsRepository <Car, CarDTO> carsRepo = new CarsRepository <Car, CarDTO>(); TracksRepository <Track> tracksRepo = new TracksRepository <Track>(); BestLapTimesRepository <BestLapTime> bestLapsRepo = new BestLapTimesRepository <BestLapTime>(); DriverResult driverResult = await driverResultsRepo.GetById(first.DriverResultId); BestLapTime usersBestLapInCar = bestLapsRepo.GetForUserId(driverResult.ApplicationUserId).Where(bl => bl.CarId == driverResult.CarId).FirstOrDefault(); TimeSpan fastestLap = driverResult.BestLapTime; BestLapTime bestLapTime = new BestLapTime(); foreach (LapTimeDTO lapTimeDTO in lapTimeDTOs) { LapTime lapTime = new LapTime() { Id = Guid.NewGuid().ToString(), DriverResultId = lapTimeDTO.DriverResultId, LapNumber = lapTimeDTO.LapNumber, Time = lapTimeDTO.Time }; lapTime = await repo.Insert(lapTime); if (lapTime.Time == fastestLap) { bestLapTime = new BestLapTime() { Id = Guid.NewGuid().ToString(), ApplicationUserId = driverResult.ApplicationUserId, CarId = driverResult.CarId, LapTimeId = lapTime.Id, }; if (usersBestLapInCar == null) { bestLapTime = await bestLapsRepo.Insert(bestLapTime); } else if (fastestLap < usersBestLapInCar.LapTime.Time) { EntityState response = await bestLapsRepo.Update(usersBestLapInCar.Id, bestLapTime); } Car car = await carsRepo.GetById(driverResult.CarId); var carsBestLapTime = car?.BestLapTime?.LapTime?.Time; if (car?.BestLapTimeId == null || fastestLap < carsBestLapTime) { car.BestLapTimeId = bestLapTime.Id; await carsRepo.Update(car.Id, car); } Track track = await tracksRepo.GetById(car?.TrackId); var trackBestLap = track?.BestLapTime?.LapTime?.Time; if (track?.BestLapTimeId == null || fastestLap < trackBestLap) { track.BestLapTimeId = bestLapTime.Id; await tracksRepo.Update(track.Id, track); } } } } return(new HttpResponseMessage(HttpStatusCode.OK)); }
public async Task Setup(ApplicationUser loggedInUser) { this.User = loggedInUser; BestLapTimesRepository <BestLapTime> bestLapTimesRepo = new BestLapTimesRepository <BestLapTime>(); // Set Track Values if (string.IsNullOrEmpty(SelectedTrackId)) { this.SelectedTrack = this.User.Tracks.FirstOrDefault(); this.SelectedTrackId = this.SelectedTrack.Id; } else { this.SelectedTrack = this.User.Tracks.Where(t => t.Id == this.SelectedTrackId).FirstOrDefault(); } string trackBestLapId = this.SelectedTrack.BestLapTimeId; if (this.SelectedTrack.BestLapTimeId == null) { this.TrackRecord = "No Record Set"; this.TrackRecordHolder = "No Record Set"; } else { BestLapTime trackBestLapTime = await bestLapTimesRepo.GetById(trackBestLapId); this.TrackRecord = trackBestLapTime.LapTime.Time.ToString(@"m\:ss\.fff"); this.TrackRecordHolder = trackBestLapTime.ApplicationUser.UserName; } foreach (Track track in this.User.Tracks.ToList()) { TrackDTO trackDTO = Mapper.Map <Track, TrackDTO>(track); this.MyTracks.Add(trackDTO); this.MyTracksListItems.Add(new SelectListItem() { Text = trackDTO.Name, Value = trackDTO.Id }); } // Set Cars in Selected Track values CarsRepository <Car, CarDTO> carsRepo = new CarsRepository <Car, CarDTO>(); // var cars = carsRepo.GetFor(SelectedTrackId); var cars = carsRepo.GetAllAsDTO(this.SelectedTrackId).OrderBy(c => c.TrackRecord); if (cars.Count() > 0) { // Populate dropdown foreach (CarDTO car in cars) { this.CarsInGarage.Add(car); this.CarsInGarageListItems.Add(new SelectListItem() { Text = car.Name, Value = car.Id }); } // Select a car CarDTO tempCar = string.IsNullOrEmpty(SelectedCarId) ? cars.FirstOrDefault() : cars.Where(c => c.Id == this.SelectedCarId).FirstOrDefault(); this.SelectedCarId = tempCar.Id; // this.SelectedCar = Mapper.Map<Car, CarDTO>(tempCar); this.SelectedCar = tempCar; this.BestLapTimesForCar = await bestLapTimesRepo.GetBestLapTimesForCar(this.SelectedCar.Id); // Set up child View Models this.ChangeTrackViewModel = new ChangeTrackViewModel { SelectedTrackId = this.SelectedTrackId, MyTracksListItems = this.MyTracksListItems }; this.ChangeCarViewModel = new ChangeCarViewModel { SelectedCarId = this.SelectedCarId, CarsInGarageListItems = this.CarsInGarageListItems }; this.EditCarViewModel = new EditCarViewModel { CarsInGarageListItems = this.CarsInGarageListItems }; this.DeleteCarViewModel = new DeleteCarViewModel { CarsInGarageListItems = CarsInGarageListItems }; // Set selected Car Values this.EditCarViewModel.SetCarToEdit(tempCar); this.DeleteCarViewModel.SetCarToDelete(tempCar); } // Required for adding first car. this.CreateCarViewModel = new CreateCarViewModel { MyTracksListItems = this.MyTracksListItems }; }