public async Task <UserPhotoForReturnDto> UploadProfilePhoto(FileUploadDto uploadDto) { var claimId = int.Parse(httpContextAccessor.HttpContext.User?.Claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value); if (claimId != uploadDto.AnnounceId) { throw new RestException(HttpStatusCode.BadRequest, new { AlreadyExist = Messages.OperationDenied }); } var checkAnnounceById = await userDal.GetAsync(x => x.Id == uploadDto.AnnounceId); if (checkAnnounceById == null) { throw new RestException(HttpStatusCode.BadRequest, new { NotFound = Messages.UserNotFound }); } var uploadFile = await upload.Upload(uploadDto.File, "userprofile"); var mapForCreate = new UserPhotoForCreationDto(); mapForCreate.Name = uploadFile.Name; mapForCreate.FullPath = uploadFile.FullPath; mapForCreate.UserId = uploadDto.AnnounceId; mapForCreate.IsConfirm = false; mapForCreate.IsMain = false; var mapForDb = mapper.Map <UserPhoto>(mapForCreate); var createPhoto = await userPhotoDal.Add(mapForDb); return(mapper.Map <UserPhoto, UserPhotoForReturnDto>(createPhoto)); }
public IResult Add(IFormFile file, UserPhoto photo) { IResult result = BusinessRules.Run(CheckIfImageLimitExceedded(photo.UserId)); if (result != null) { return(result); } photo.ImagePath = FileHelpers.Add(file); _userPhotoDal.Add(photo); return(new SuccessResult(Messages.PhotoAdded)); }
public IActionResult AddPhoto([FromBody] UserPhoto userPhoto) { try { _userPhotoDal.Add(userPhoto); return(new StatusCodeResult(201)); } catch { } return(BadRequest()); }
public ActionResult AddUserPhoto(int userId, [FromForm] UserPhotoForCreationDto userPhotoForCreationDto) { UsersContext _context = new UsersContext(); var user = _userDal.GetUserById(userId); if (user == null) { return(BadRequest("Cloud not find the user.")); } var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId != user.Id) { return(Unauthorized()); } var file = userPhotoForCreationDto.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); } } userPhotoForCreationDto.Url = uploadResult.Uri.ToString(); userPhotoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <UserPhoto>(userPhotoForCreationDto); photo.user = user; if (!user.UserPhotos.Any(p => p.IsMain)) { photo.IsMain = true; } user.UserPhotos.Add(photo); photo.UserId = user.Id; _userPhotoDal.Add(photo); _context.SaveChanges(); /*if (_context.SaveChanges()>0) * { * var photoToReturn = _mapper.Map<UserPhotoForReturnDto>(photo); * return CreatedAtRoute("GetUserPhoto", new { Id = photo.Id }, photoToReturn); * }*/ return(Ok(201)); }
public async Task <UserPhotoForReturnDto> Create(FileUploadDto uploadDto) { var checkAnnounceById = await userDal.GetAsync(x => x.Id == uploadDto.AnnounceId); if (checkAnnounceById == null) { throw new RestException(HttpStatusCode.BadRequest, new { NotFound = Messages.UserNotFound }); } var uploadFile = await upload.Upload(uploadDto.File, "userprofile"); var mapForCreate = new UserPhotoForCreationDto(); mapForCreate.Name = uploadFile.Name; mapForCreate.FullPath = uploadFile.FullPath; mapForCreate.UserId = uploadDto.AnnounceId; mapForCreate.IsConfirm = false; mapForCreate.UnConfirm = false; var mapForDb = mapper.Map <UserPhoto>(mapForCreate); var createPhoto = await userPotoDal.Add(mapForDb); return(mapper.Map <UserPhoto, UserPhotoForReturnDto>(createPhoto)); }