예제 #1
0
        public async Task <IActionResult> AddPhotoToProfile(int userId,
                                                            [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            User user = await _repo.GetUser(userId);

            if (user.ProfilePhoto != null)
            {
                Photo trashPhoto = await _repo.GetPhotoById(user.ProfilePhoto.Id);

                _repo.Delete(trashPhoto);
            }

            photoForCreationDto.Width  = 500;
            photoForCreationDto.Height = 500;
            photoForCreationDto.UserId = userId;
            photoForCreationDto.IsMain = true;

            Photo photo = UploadPhotoToCloudinary(photoForCreationDto);

            _repo.Add(photo);

            if (await _repo.SaveAllAsync())
            {
                PhotoForReturnDto photoForReturnDto = _mapper.Map <PhotoForReturnDto>(photo);

                return(Ok(photoForReturnDto));
            }

            return(BadRequest("Adding photo to user's profile could not be possible"));
        }