Exemplo n.º 1
0
        public async Task <ActionResult <PhotoDto> > AddPhoto(IFormFile file)
        {
            var user = await _userRepository.GetUserByUserNameAsync(User.GetUserName());

            var result = await _photoService.AddPhotoAysnc(file);

            if (result.Error != null)
            {
                return(BadRequest(result.Error.Message));
            }
            var photo = new Photo
            {
                Url      = result.SecureUrl.AbsoluteUri,
                PublicId = result.PublicId
            };

            if (user.Photos.Count == 0)
            {
                photo.IsMain = true;
            }
            ;

            user.Photos.Add(photo);

            if (await _userRepository.SaveAllAsync())
            {
                //return _mapper.Map<PhotoDto>(photo);
                return(CreatedAtRoute("GetUser", new { username = user.UserName }, _mapper.Map <PhotoDto>(photo)));
            }
            return(BadRequest("Problem adding photo"));
        }