public async Task <IActionResult> AddPhoto(int productId, [FromForm] PhotoForCreationDto photoForCreationDto) { var productFromRepo = await _repo.GetItemAsync(productId); var file = photoForCreationDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(400).Crop("pad").Gravity("center") //.Corp("fill") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); var checkIsMainPhoto = await _repository.AllItems.FirstOrDefaultAsync(p => p.IsMain == true && p.ProductId == productFromRepo.Id); if (checkIsMainPhoto != null) { photo.IsMain = false; } else { photo.IsMain = true; } photo.ProductId = productFromRepo.Id; if (await _repository.AddItemAsync(photo)) { var photoForReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoForReturn)); } return(BadRequest()); }