public async Task RemoveVehicle(Vehicle vehicle) { foreach (var p in vehicle.Pictures) { await _uploadService.DeleteObjectAsync(p.Id); } _dataContext.Remove(vehicle); await _dataContext.SaveChangesAsync(); }
public async Task <bool> RemoveVehicle(Vehicle vehicle) { foreach (var p in vehicle.Pictures) { await _uploadService.DeleteObjectAsync(p.Id); } _dataContext.Remove(vehicle); try { await _dataContext.SaveChangesAsync(); } catch { return(false); } return(true); }
public async Task <IActionResult> SetLicensePicture(List <IFormFile> files) { var driverId = User.Claims.FirstOrDefault(c => c.Type == Helpers.Constants.Strings.JwtClaimIdentifiers.DriverId)?.Value; var driver = _usersRepository.GetDriverById(Guid.Parse(driverId)); long size = files.Sum(f => f.Length); var formFile = files[0]; if (!formFile.IsImage()) { return(BadRequest()); } if (driver.DriverLicense == null) { ModelState.AddModelError(nameof(driver.DriverLicense), "No license to add image."); return(BadRequest(ModelState)); //remove picture from data context } if (driver.DriverLicense.ImageId != null) { await _uploadService.DeleteObjectAsync(driver.DriverLicense.ImageId); } if (formFile.Length > 0) { var filename = ContentDispositionHeaderValue .Parse(formFile.ContentDisposition) .FileName .TrimStart().ToString(); filename = _hostingEnvironment.WebRootPath + $@"\uploads" + $@"\{formFile.FileName}"; size += formFile.Length; using (var fs = System.IO.File.Create(filename)) { await formFile.CopyToAsync(fs); fs.Flush(); }//these code snippets saves the uploaded files to the project directory var imageId = Guid.NewGuid().ToString() + Path.GetExtension(filename); await _uploadService.PutObjectToStorage(imageId.ToString(), filename);//this is the method to upload saved file to S3 driver.DriverLicense.UpdateTime = DateTime.UtcNow; driver.DriverLicense.ImageId = imageId; driver.DriverLicense.IsApproved = false; var res = await _usersRepository.UpdateDriverLicense(driver.DriverLicense); if (!res) { return(Conflict()); } System.IO.File.Delete(filename); return(Ok()); } return(BadRequest()); }