public async Task <IActionResult> CreateVehicle([FromBody] SaveVehicleResource vehicleResourve) { //chek request fields if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //save vehicle to the DB var vehicle = mapper.Map <SaveVehicleResource, Vehicle>(vehicleResourve); vehicle.LastUpdate = DateTime.Now; repository.Add(vehicle); await unitOfwork.CompleteAsync(); //return saved vehicle vehicle = await repository.GetVehicle(vehicle.ID); var resoult = mapper.Map <Vehicle, VehicleResource>(vehicle); return(Ok(resoult)); }
public async Task <IActionResult> Upload(int vehicleId, IFormFile file) { var vehicle = await repository.GetVehicle(vehicleId, includeRelated : false); if (vehicle == null) { return(NotFound()); } if (file == null) { return(BadRequest("Null file")); } if (file.Length == 0) { return(BadRequest("Empty file")); } if (file.Length > photoSettings.MaxLength) { return(BadRequest("Max file length exceeded")); } if (!photoSettings.IsSupported(file.FileName)) { return(BadRequest("Invalid file type")); } var uploadsFolderPath = Path.Combine(host.WebRootPath, "uploads"); if (!Directory.Exists(uploadsFolderPath)) { Directory.CreateDirectory(uploadsFolderPath); } var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var filePath = Path.Combine(uploadsFolderPath, fileName); using (var strem = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(strem); } var photo = new Photo { FileName = fileName }; vehicle.Photos.Add(photo); await unitOfwork.CompleteAsync(); var photoResourse = mapper.Map <Photo, PhotoResourse>(photo); return(Ok(photoResourse)); }