public IActionResult UploadToCloudinary([FromForm] PhotoUploadDto photoUploadDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var file = photoUploadDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams { File = new FileDescription(file.Name, stream) }; uploadResult = _cloudinary.Upload(uploadParams); } } if (uploadResult.Error != null) { return(BadRequest()); } return(Ok(new { Url = uploadResult.Uri.ToString(), SecureUrl = uploadResult.SecureUri.ToString() })); }
public async Task <PhotoUploadDto> UpdatePhotoAsync(PhotoUploadDto photoDto) { var userPrincipal = _httpContext.HttpContext.User; var user = await GetUserAsync(userPrincipal); user.ProfilePhotoPath = photoDto.PhotoOriginalPath; user.ProfilePhotoThumbPath = photoDto.PhotoThumbPath; _context.Users.Update(user); await _context.SaveChangesAsync(); return(photoDto); }
public async Task <IActionResult> AddDocumentForUse(int userId, [FromForm] PhotoUploadDto photoForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _publicTransportRepository.GetUser(userId); 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(600).Height(500).Crop("fill") }; uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; _mapper.Map(photoForCreationDto, userFromRepo); var result = await _userManager.UpdateAsync(userFromRepo); if (result.Succeeded) { return(Ok(photoForCreationDto)); } return(BadRequest("Could not add the photo!")); }
public async Task <ActionResult <PhotoUserDto> > AddPhotoUser([FromForm] PhotoUploadDto uploadDto) { var file = uploadDto.Avatar; var currentUser = await _userManager.Users .Include(p => p.PhotoUsers) .SingleOrDefaultAsync(u => u.Id == User.GetUserId()); var result = await _photoService.AddPhotoAsync(file); if (result.Error != null) { return(BadRequest(result.Error.Message)); } var photo = new PhotoUser { PhotoUserUrl = result.SecureUrl.AbsoluteUri, PublicId = result.PublicId }; if (currentUser.PhotoUsers.Count == 0) { photo.IsMain = true; } currentUser.PhotoUsers.Add(photo); var resultUser = await _userManager.UpdateAsync(currentUser); if (resultUser.Succeeded) { return(CreatedAtRoute("GetUser", new { id = currentUser.Id }, _mapper.Map <PhotoUserDto>(photo))); } return(BadRequest("Problem addding photo")); }
public async Task <ActionResult <OrderToReturnDto> > UpdateOrderMember(int id, string buyerEmail, [FromForm] PhotoUploadDto photoUpload) { var updatedOrder = await _orderRepo.UpdateOrderPaymentUrl(id, buyerEmail, photoUpload.Photo); if (updatedOrder == null) { return(BadRequest(new ApiResponse(400, "Failed updating order"))); } return(Ok(_mapper.Map <OrderToReturnDto>(updatedOrder))); }
public async Task <ActionResult <PhotoUserDto> > EditPhotoUser([FromForm] PhotoUploadDto uploadDto, int photoId) { //remove old photo var user = await _userManager.Users .Include(p => p.PhotoUsers) .SingleOrDefaultAsync(u => u.Id == User.GetUserId()); var photo = user.PhotoUsers.FirstOrDefault(x => x.Id == photoId); if (photo == null) { return(NotFound()); } if (photo.PublicId != null) { var result = await _photoService.DeletePhotoAsync(photo.PublicId); if (result.Error != null) { return(BadRequest(result.Error.Message)); } } user.PhotoUsers.Remove(photo); var resultDelete = await _userManager.UpdateAsync(user); if (resultDelete.Succeeded) { //add new photo var file = uploadDto.Avatar; var afterAdding = await _photoService.AddPhotoAsync(file); if (afterAdding.Error != null) { return(BadRequest(afterAdding.Error.Message)); } var newPhoto = new PhotoUser { PhotoUserUrl = afterAdding.SecureUrl.AbsoluteUri, PublicId = afterAdding.PublicId }; if (user.PhotoUsers.Count == 0) { newPhoto.IsMain = true; } user.PhotoUsers.Add(newPhoto); var resultUser = await _userManager.UpdateAsync(user); if (resultUser.Succeeded) { return(new PhotoUserDto { Id = newPhoto.Id, PhotoUserUrl = newPhoto.PhotoUserUrl, IsMain = newPhoto.IsMain }); } return(BadRequest("Problem editding photo")); } return(BadRequest("Failed to delete the photo")); }
public async Task <ActionResult <ProductToReturnDto> > AddProductPhoto(int id, [FromForm] PhotoUploadDto photoDto) { var spec = new ProductsWithTypesAndBrandsSpecification(id); var product = await _unitOfWork.Repository <Product>().GetByIdWithSpecAsync(spec); if (photoDto.Photo.Length > 0) { var photo = await _photoRepository.SaveToDiskAsync(photoDto.Photo); if (photo != null) { product.AddPhoto(photo.PictureUrl, photo.FileName); _unitOfWork.Repository <Product>().Update(product); var result = await _unitOfWork.Complete(); if (result <= 0) { return(BadRequest(new ApiResponse(400, "Problem uploading product photo"))); } } else { return(BadRequest(new ApiResponse(400, "Problem uploading product photo"))); } } return(_mapper.Map <ProductToReturnDto>(product)); }