Esempio n. 1
0
        public GetPlaylistsResult GetPlaylists(GetPlaylistsRequest request)
        {
            try
            {
                List <Domain.Objects.Playlist> playlists;
                int        forItemSize;
                List <int> playlistIds;

                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    playlists = mgr.GetAll <Domain.Objects.Playlist>();


                    playlists.Insert(0, new Domain.Objects.Playlist()
                    {
                        Id = Domain.Objects.Playlist.LIKED_ID, Name = "[Liked]", NrOfTracks = mgr.GetLikedTrackCount()
                    });

                    if (!request.AsSelector)  // only show [all] when it's not listed for adding a track to the playlist
                    {
                        playlists.Insert(0, new Domain.Objects.Playlist()
                        {
                            Id = Domain.Objects.Playlist.ALL_ID, Name = "[All]", NrOfTracks = mgr.GetTrackCount()
                        });
                    }

                    if (request.ForItemIsTrack)
                    {
                        playlistIds = new List <int>(mgr.GetPlaylistIdsForTrack(request.ForItemId));
                        forItemSize = 1;
                    }
                    else
                    {
                        playlistIds = new List <int>(mgr.GetPlaylistIdsForAlbum(request.ForItemId));
                        forItemSize = mgr.FindTracks("", Domain.Objects.Playlist.ALL_ID, request.ForItemId, 0, int.MaxValue).Count;
                    }
                }

                if (request.AsSelector)
                {
                    return(new GetPlaylistsResult()
                    {
                        Success = true,
                        Playlists = playlists.Select(p =>
                        {
                            var itm = new PlaylistForAddingItem()
                            {
                                Id = p.Id + "",
                                IsCurrent = Player.Instance.CurrentPlaylist.Id == p.Id,
                                Name = p.Name,
                                NrOfTracks = p.NrOfTracks,

                                AlreadyOnPlaylistCount = playlistIds.Count(id => p.Id == id),
                                ForItemSize = forItemSize
                            };

                            return itm;
                        }).ToArray()
                    });
                }
                else
                {
                    return(new GetPlaylistsResult()
                    {
                        Success = true,
                        Playlists = playlists.Select(p => new Playlist()
                        {
                            Id = p.Id + "",
                            IsCurrent = Player.Instance.CurrentPlaylist.Id == p.Id,
                            Name = p.Name,
                            NrOfTracks = p.NrOfTracks
                        }).ToArray()
                    });
                }
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <GetPlaylistsResult>(ex));
            }
        }