public async Task <IHttpActionResult> PutTrack(string id, TrackDTO trackDTO) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != trackDTO.Id) { return(BadRequest()); } Track track = await repo.GetById(id); track.Name = trackDTO.Name; track.Length = trackDTO.Length; if (await repo.Update(id, track) != EntityState.Modified) { return(NotFound()); } return(StatusCode(HttpStatusCode.NoContent)); }
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)); }