private async Task <AdCreateViewModel> InitializeCreationModel() { var model = new AdCreateViewModel { AllManufacturers = await this.cache.GetAllManufacturersAsync(), AllFuelTypes = await this.cache.GetAllFuelTypesAsync(), AllTransmissionTypes = await this.cache.GetAllTransmissionTypesAsync(), AvailableYears = GetAvailableYears(), AllFeatures = await this.vehicleElements.GetFeaturesAsync() }; return(model); }
public async Task <IActionResult> Create(AdCreateViewModel model, List <IFormFile> pictures) { if (!this.ModelState.IsValid) { return(View(model)); } model.AllFeatures = model .AllFeatures .Where(f => f.IsChecked) .ToList(); model.UserId = this.userManager.GetUserId(this.User); var serviceModel = this.mapper.Map <VehicleCreateServiceModel>(model); serviceModel.FeatureIds = new List <int>(model.AllFeatures.Select(f => f.Id)); var newAd = await this.ads.CreateAsync(serviceModel); try { foreach (var picture in pictures) { // Process the file to the file system: var extension = GetValidExtension(picture.FileName); var vehiclePictureFolder = string.Format(VehiclePictureFolder, newAd.VehicleId); var dbPath = string.Format(DbPath, vehiclePictureFolder, GetUniqueFileName(newAd.VehicleId, extension)); var fileProcessingSuccess = ProcessFile(picture, dbPath); if (fileProcessingSuccess) { // After successfull processing the image to file system => save it to database: var success = await this.pictures.CreateAsync(dbPath, newAd.VehicleId); if (!success) { return(View(model)); } } } } catch { return(View(model)); } return(RedirectToAction(nameof(Details), new { id = newAd.AdId })); }