public async Task ChangePhotoAsync(AddPhotoDto model) { var user = await _auth.FindByIdUserAsync(model.UserId); if (user == null) { throw new UserNotExistException("Given user not exist!!", 400); } var ext = model.UploadedFile.FileName.Substring(model.UploadedFile.FileName.LastIndexOf('.')); if (this._config[$"PhotoExtensions:{ext}"] != null) { var photo = await _unit.PhotoRepository.GetPhotoByUserAsync(user.Id); photo.Name = $"{user.Id}{model.UploadedFile.Name}"; photo.Path = $"{_env.WebRootPath}\\avatars\\{photo.Name}"; using (var fileStream = new FileStream(photo.Path, FileMode.Create)) { await model.UploadedFile.CopyToAsync(fileStream); } await _unit.Commit(); } else { throw new PhotoInCorrectException("Given extension is incorrect!!", 400); } }
public async Task AddPhoto(AddPhotoDto photoDto) { DateTime dateTime = DateTime.Now; byte[] bytesPhoto = Convert.FromBase64String(photoDto.ImageBytes); await _photoRepository.AddPhoto(new Photo { Title = photoDto.Title, ImageBytes = bytesPhoto, UpdateDateTime = dateTime }); }
public async Task AddUserPhoto(AddPhotoDto newPhoto) { await _addPhotoBusiness.AddPhoto(newPhoto); await _userPhotoRepository.UnitOfWork.Save(); var photo = await _getPhotoBusiness.GetLastPhoto(); await _userPhotoRepository.AddUserPhoto(new UserPhoto { PhotoId = photo.Id, UserId = newPhoto.UserId }); }
public async Task<ActionResult> AddPhoto(AddPhotoDto addPhotoDto) { Photo photo = new Photo { // UserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value), // UrlAdress = addPhotoDto.UrlAdress, }; _repo.Add(photo); await _repo.SaveAll(); return StatusCode(201); }
public async Task <IActionResult> AddPhoto(int userId, [FromForm] AddPhotoDto addPhotoDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await repository.GetUser(userId, true); var file = addPhotoDto.File; var imageUploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var imageUploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face") }; imageUploadResult = cloudinary.Upload(imageUploadParams); } } addPhotoDto.Url = imageUploadResult.Uri.ToString(); addPhotoDto.PublicId = imageUploadResult.PublicId; var photo = mapper.Map <Photo>(addPhotoDto); if (!user.Photos.Any(p => p.IsMain)) { photo.IsMain = true; } user.Photos.Add(photo); if (await repository.SaveAll()) { var result = mapper.Map <GetPhotoDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, result)); } return(BadRequest("Add photo failure")); }
public async Task AddPhoto([FromBody] AddPhotoDto photo) { await _addUserPhotoCommandHandler.Handler(photo); }
public async Task Handler(AddPhotoDto photo) { await _addUserPhotoBusiness.AddUserPhoto(photo); await _userPhotoRepository.UnitOfWork.Save(); }