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 ActionResult UploadImage(UploadImageModel model)
        {
            var isUploadSuccessful = false;

            if (model.File != null)
            {
                Bitmap original   = null;
                var    name       = "newimagefile";
                var    errorField = string.Empty;

                if (model.File != null) // model.IsFile
                {
                    errorField = "File";
                    name       = Path.GetFileNameWithoutExtension(model.File.FileName);
                    original   = System.Drawing.Image.FromStream(model.File.InputStream) as Bitmap;
                }

                if (original != null)
                {
                    var img = CreateImage(original, model.X, model.Y, model.Width, model.Height);
                    var ms  = new MemoryStream();
                    img.Save(ms, ImageFormat.Jpeg);

                    using (MemoryStream memoryStream = new MemoryStream(ms.ToArray()))
                    {
                        isUploadSuccessful = _cloudinaryManager.UploadImage("streamed", memoryStream, model.ChangeHeight, model.ChangeWidth, model.ImageRadius, model.FileName,
                                                                            string.Equals(model.FolderName, "Root") ? null : model.FolderName);
                    }
                    if (isUploadSuccessful)
                    {
                        //Redirect to index
                        return(RedirectToAction("ImageBrowser"));
                    }
                }
                else //Otherwise we add an error and return to the (previous) view with the model data
                {
                    ModelState.AddModelError(errorField, "Your upload did not seem valid. Please try again using only correct images!");
                }
            }
            return(View(model));
        }
        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" }));
        }