public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } _logger.LogInformation(LoggingEvents.GetItem, "Get VehicleModel {ID}", id); var vehicle = await _vehicleModelService.GetVehicle(id); VehicleModel = new VehicleModelEditDto { Id = vehicle.Id, VehicleType = vehicle.VehicleType, PricePerDay = vehicle.PricePerDay, NumberOfDoors = vehicle.NumberOfDoors, NumberOfSeats = vehicle.NumberOfSeats, Active = vehicle.Active, AirConditioning = vehicle.AirConditioning, Automatic = vehicle.Automatic }; if (VehicleModel == null) { _logger.LogWarning(LoggingEvents.GetItemNotFound, "VehicleModel {ID} NOT FOUND", id); return(NotFound()); } return(Page()); }
public async Task EditVehicleModel(VehicleModelEditDto vehicleModelDto) { var vehicleModel = await _dbContext.VehicleModels .Where(vm => vm.Id == vehicleModelDto.Id) .SingleOrDefaultAsync(); vehicleModel.VehicleType = vehicleModelDto.VehicleType; vehicleModel.PricePerDay = vehicleModelDto.PricePerDay.Value; vehicleModel.NumberOfDoors = vehicleModelDto.NumberOfDoors.Value; vehicleModel.NumberOfSeats = vehicleModelDto.NumberOfSeats.Value; vehicleModel.Active = vehicleModelDto.Active; vehicleModel.AirConditioning = vehicleModelDto.AirConditioning; vehicleModel.Automatic = vehicleModelDto.Automatic; if (vehicleModelDto.Picture != null || vehicleModelDto.Picture.Length > 0) { var file = vehicleModelDto.Picture; var fileName = Path.GetFileName(file.FileName); var extension = Path.GetExtension(file.FileName); string name = Path.GetFileNameWithoutExtension(fileName); string myfileName = name + '_' + vehicleModel.Id + extension; var storageConnectionString = _configuration.GetValue <String>("StorageConnectionString"); if (!String.IsNullOrEmpty(storageConnectionString)) { try { CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString); CloudBlobClient client = account.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference("images"); if (vehicleModel.VehicleUrl != null && vehicleModel.VehicleUrl.Length > 0) { Uri uri = new Uri(vehicleModel.VehicleUrl); string currentName = Path.GetFileName(uri.LocalPath); CloudBlockBlob oldBlob = container.GetBlockBlobReference(currentName); await oldBlob.DeleteIfExistsAsync(); } CloudBlockBlob blob = container.GetBlockBlobReference(myfileName); using (var stream = file.OpenReadStream()) { await blob.UploadFromStreamAsync(stream); vehicleModel.VehicleUrl = blob.Uri.ToString(); await _dbContext.SaveChangesAsync(); } } catch (Exception e) { } } } _dbContext.Attach(vehicleModel).State = EntityState.Modified; await _dbContext.SaveChangesAsync(); }