public async Task <IActionResult> CreateAlbum(Album album)
        {
            CloudinaryManager cm = new CloudinaryManager();
            var userId           = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            album.UserId = userId;

            if (album.Image != null)
            {
                string urlImage = cm.UploadImage(album.Image);
                if (urlImage != string.Empty)
                {
                    album.Image = urlImage;
                }
                else
                {
                    return(BadRequest(new { message = "An error has ocur when uploading the image!" }));
                }
            }

            _playAppRepository.Add(album);
            if (await _playAppRepository.SaveAll())
            {
                return(Ok(new { message = "Album Created!", data = album }));
            }

            return(BadRequest(new { message = "Error when creating Album" }));
        }
        public async Task <IActionResult> GetUserProfile(UserProfile user)
        {
            CloudinaryManager cm = new CloudinaryManager();
            var userId           = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            //Get the to use to udpate
            User UserToUpdate = await _playAppRepository.GetUser(userId);

            //Check  username
            if (UserToUpdate.Username != user.Username)
            {
                var userFound = await _authRepository.GetUserByUserName(user.Username);

                if (userFound != null)
                {
                    return(BadRequest(new { message = "Username Already exists!" }));
                }
            }
            //Check email
            if (UserToUpdate.Email != user.Email)
            {
                var userFound = await _authRepository.GetUserByEmail(user.Email);

                if (userFound != null)
                {
                    return(BadRequest(new { message = "Email Already exists!" }));
                }
            }


            if (UserToUpdate == null)
            {
                return(BadRequest(new { message = "User not found!" }));
            }

            UserToUpdate.Username = user.Username;
            UserToUpdate.Email    = user.Email;

            //Update image
            if (user.ProfileImage != null)
            {
                string urlImage = cm.UploadImage(user.ProfileImage);
                if (urlImage != string.Empty)
                {
                    UserToUpdate.ProfileImage = urlImage;
                }
                else
                {
                    return(BadRequest(new { message = "An error has ocur when uploading the image!" }));
                }
            }

            await _playAppRepository.SaveAll();

            return(Ok(new { message = "User updated", user = _mapper.Map <UserProfile>(UserToUpdate) }));
        }
        public async Task <IActionResult> UpdateAlbum(AlbumForUpdate albumForUpdate)
        {
            CloudinaryManager cm = new CloudinaryManager();
            var currentAlbum     = await _playAppRepository.GetAlbum(albumForUpdate.Id);

            var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (userId != currentAlbum.UserId)
            {
                return(BadRequest(new { message = "Not authorized" }));
            }

            if (albumForUpdate.Image != null)
            {
                string urlImage = cm.UploadImage(albumForUpdate.Image);
                if (urlImage != string.Empty)
                {
                    albumForUpdate.Image = urlImage;
                }
                else
                {
                    return(BadRequest(new { message = "An error has ocur when uploading the image!" }));
                }
            }
            else
            {
                albumForUpdate.Image = currentAlbum.Image;
            }


            _mapper.Map(albumForUpdate, currentAlbum);

            if (await _playAppRepository.SaveAll())
            {
                return(Ok(new { message = "Album updated", data = _mapper.Map <AlbumToReturn>(currentAlbum) }));
            }

            return(BadRequest(new { message = "Album was not updated" }));
        }
 public ImageController()
 {
     _cloudinaryManager = new CloudinaryManager();
 }