public bool DeletePlaylist(Playlist playlist)
        {
            try
            {
                PlaylistTable.DeleteOnSubmit(playlist);
                PlaylistTable.Context.SubmitChanges();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public int SavePlaylist(Playlist playlist)
        {
            try
            {
                if (playlist.PlaylistId == 0)
                {
                    PlaylistTable.InsertOnSubmit(playlist);
                }
                else
                {
                    PlaylistTable.Context.Refresh(RefreshMode.KeepChanges, playlist);
                }

                PlaylistTable.Context.SubmitChanges();

                return playlist.PlaylistId;
            }
            catch (Exception)
            {
                return -1;
            }
        }
        public int SavePlaylist(int userId, Playlist playlist)
        {
            using (var tranScope = new TransactionScope())
            {
                try
                {
                    var playlistId = playlist.PlaylistId;
                    var newPlaylist = (playlistId == 0);

                    if (newPlaylist)
                    {
                        playlistId = SqlPlaylistRepository.SavePlaylist(playlist);
                    }

                    if (playlist.PlaylistSongCollection.Count > 0 && SavePlaylistSongs(UpdatePlaylistSongsPlaylistId(playlist.PlaylistId, playlist.PlaylistSongCollection)))
                    {
                        if (SaveUserPlaylist(userId, playlistId, newPlaylist))
                        {
                            tranScope.Complete();

                            return playlistId;
                        }
                    }
                    else
                    {
                        return -1;
                    }
                }
                catch
                {
                    return -1;
                }
            }

            return -1;
        }
        public Playlist GetPlaylistById(int playlistId)
        {
            var playlist = SqlPlaylistRepository.Playlist.Where(p => p.PlaylistId == playlistId).FirstOrDefault();
            if (playlist != null)
            {
                playlist.PlaylistSongCollection = GetPlaylistSongCollection(playlistId);
            }
            else
            {
                playlist = new Playlist();
                playlist.PlaylistSongCollection = new List<PlaylistSong>();
            }

            return playlist;
        }
        public string SavePlaylist(string form, bool newPlaylist)
        {
            var result = "The playlist could not be saved. Please try again. If the problem persists, please contact us at [email protected]";
            var formCollection = form.Split('&');

            if (!string.IsNullOrEmpty(MusicManagerBase.ReturnFormItemValue(formCollection, "playlistName")) && !(MusicManagerBase.ReturnFormItemValue(formCollection, "playlistName").Equals("New Playlist")))
            {
                // check if a playlist with this name already exists
                var playlist = WebService.GetPlaylistByName(MusicManagerBase.ReturnFormItemValue(formCollection, "playlistName"));

                var playlistId = 0;
                if (playlist.PlaylistId > 0)
                {
                    // then check if it belongs to this user
                    var userPlaylist = WebService.GetUserPlaylistsByUserId(WebSecurity.CurrentUserId).Where(x => x.PlaylistId == playlist.PlaylistId).FirstOrDefault();

                    if (userPlaylist != null)
                    {
                        playlistId = playlist.PlaylistId;
                    }
                }

                if (playlistId > 0 && newPlaylist)
                {
                    result = "Warning! This name is already in use. Please select a new name or load the playlist with this name and add this song to it.";
                    return result;
                }

                if (playlistId == 0 && !string.IsNullOrEmpty(MusicManagerBase.ReturnFormItemValue(formCollection, "playlistId")))
                {
                    if (int.TryParse(MusicManagerBase.ReturnFormItemValue(formCollection, "playlistId"), out playlistId))
                    {
                        playlistId = Convert.ToInt32(MusicManagerBase.ReturnFormItemValue(formCollection, "playlistId"));
                    }
                }

                var playlistSongCollection = PopulatePlaylistSongs(formCollection, playlistId);

                playlist = new Playlist()
                {
                    Active = true,
                    CreatedDate = DateTime.Now,
                    PlaylistId = playlistId,
                    PlaylistName = MusicManagerBase.ReturnFormItemValue(formCollection, "playlistName"),
                    PlaylistSongCollection = playlistSongCollection
                };

                try
                {
                    playlistId = WebService.SavePlaylist(WebSecurity.CurrentUserId, playlist);
                    if (playlistId > 0)
                    {
                        result = "The playlist has been saved successfully!&" + playlistId;
                    }
                }
                catch (Exception ex)
                {
                    return result;
                }
            }
            else
            {
                result = "Please enter a valid name for your playlist!";
            }

            return result;
        }