public IEnumerable <CarDTO> GetCars(string trackId) { IEnumerable <CarDTO> cars = repo.GetAllAsDTO(trackId); string carImagesFolderPath = HttpContext.Current.Server.MapPath("~/Content/Images/Cars"); foreach (CarDTO car in cars) { string carImagePath = Path.Combine(carImagesFolderPath, car.ImageName); if (File.Exists(carImagePath)) { if (File.Exists(carImagePath)) { Image image = new Bitmap(carImagePath); int height = 200; int width = 200; if (image.Height > image.Width) { float scale = image.Height / height; width = Convert.ToInt32(image.Width / scale); } else if (image.Width > image.Height) { float scale = image.Width / width; height = Convert.ToInt32(image.Height / scale); } image = new Bitmap(image, width, height); MemoryStream ms = new MemoryStream(); switch (Path.GetExtension(carImagePath)) { case ".jpeg": image.Save(ms, ImageFormat.Jpeg); break; case ".jpg": image.Save(ms, ImageFormat.Jpeg); break; case ".png": image.Save(ms, ImageFormat.Png); break; case ".bmp": image.Save(ms, ImageFormat.Bmp); break; case ".gif": image.Save(ms, ImageFormat.Gif); break; default: break; } car.CarImageBytes = ms.ToArray(); } } } return(cars); }
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 }; }