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 }; }