public ActionResult Create(PlaylistViewModel viewModel, HttpPostedFileBase avatarImage)
        {
            Playlist playlist;
            string   path = null;

            if (String.IsNullOrEmpty(viewModel.Name) || viewModel.Name == " ")
            {
                ModelState.AddModelError("Name", "You need to fill in a name for your playlist");
                return(View(viewModel));
            }

            var user = userManager.ReadUser(User != null ? User.Identity.Name : testName);

            if (avatarImage != null && avatarImage.ContentLength > 0)
            {
                var bannerFileName = Path.GetFileName(avatarImage.FileName);
                path = FileHelper.NextAvailableFilename(Path.Combine(Server.MapPath(ConfigurationManager.AppSettings["PlaylistImgPath"]), bannerFileName));
                avatarImage.SaveAs(path);
                path = Path.GetFileName(path);
            }

            var keyAlreadyInUser = playlistManager.ReadPlaylistByKey(viewModel.Key);

            if (keyAlreadyInUser != null || viewModel.Key == null)
            {
                ModelState.AddModelError("Key", "The key value is already in use");
                return(View(viewModel));
            }


            try {
                if (viewModel.OrganisationId != 0)
                {
                    playlist = playlistManager.CreatePlaylistForOrganisation(viewModel.Name, viewModel.Description, viewModel.Key, viewModel.MaximumVotesPerUser, true, path, user, viewModel.OrganisationId);
                }
                else
                {
                    playlist = playlistManager.CreatePlaylistForUser(viewModel.Name, viewModel.Description, viewModel.Key, viewModel.MaximumVotesPerUser, true, path, user);
                }
                return(RedirectToAction("View", new { key = playlist.Key }));
            }
            catch (System.Exception e)
            {
                ModelState.AddModelError("", "Something went wrong");
                return(View(viewModel));
            }
        }
        public IHttpActionResult createPlaylist(FormDataCollection formData)
        {
            var userIdentity = RequestContext.Principal.Identity as ClaimsIdentity;

            if (userIdentity == null)
            {
                return(NotFound());
            }

            var email = userIdentity.Claims.First(c => c.Type == "sub").Value;

            if (email == null)
            {
                return(NotFound());
            }

            var user = userManager.ReadUser(email);

            if (user == null)
            {
                return(NotFound());
            }

            string imagePath = "";

            if (formData["image"] != null && formData["image"].Length > 0)
            {
                try
                {
                    var bitmap = ImageDecoder.DecodeBase64String(formData["image"]);

                    string extension = string.Empty;
                    if (bitmap.RawFormat.Equals(ImageFormat.Jpeg))
                    {
                        extension = ".jpg";
                    }
                    if (bitmap.RawFormat.Equals(ImageFormat.Png))
                    {
                        extension = ".png";
                    }
                    if (bitmap.RawFormat.Equals(ImageFormat.Bmp))
                    {
                        extension = ".bmp";
                    }
                    if (bitmap.RawFormat.Equals(ImageFormat.Gif))
                    {
                        extension = ".gif";
                    }

                    if (string.IsNullOrEmpty(extension))
                    {
                        return(BadRequest("The supplied image is not a valid image"));
                    }

                    imagePath = FileHelper.NextAvailableFilename(Path.Combine(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PlaylistImgPath"]), "playlist" + extension));
                    bitmap.Save(imagePath);

                    imagePath = Path.GetFileName(imagePath);
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }

            if (formData["organisationId"] != null)
            {
                var playlist = playlistManager.CreatePlaylistForOrganisation(formData["name"], formData["description"], formData["key"], 1, false, imagePath, user, long.Parse(formData["organisationId"]));
                if (playlist != null)
                {
                    return(Ok(playlist));
                }
            }
            else
            {
                var playlist = playlistManager.CreatePlaylistForUser(formData["name"], formData["description"], formData["key"], 1, false, imagePath, user);
                if (playlist != null)
                {
                    return(Ok(playlist));
                }
            }

            return(BadRequest());
        }