示例#1
0
        public Result RemoveFromPlaylist([FromBody] RemoveFromPlaylistRequest request)
        {
            try
            {
                if (request.PlaylistId == (Domain.Objects.Playlist.ALL_ID + ""))
                {
                    return new Result()
                           {
                               Success = false, Message = "Can't remove tracks from the [All] or [Liked] playlist"
                           }
                }
                ;
                else if (request.PlaylistId == (Domain.Objects.Playlist.LIKED_ID + ""))
                {
                    return(RemoveFromLikedPlaylist(request));
                }

                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    var playlist = mgr.Get <Domain.Objects.Playlist>(int.Parse(request.PlaylistId));

                    if (playlist == null)
                    {
                        return new Result()
                               {
                                   Success = false, Message = "Playlist not found"
                               }
                    }
                    ;

                    int nrOfItemsRemoved = 0;
                    if (request.Item.IsTrack)
                    {
                        var track = mgr.Get <Domain.Objects.Track>(request.Item.Id);

                        if (track == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Track not found"
                                   }
                        }
                        ;

                        var playlistTrack = mgr.GetPlaylistTrack(playlist.Id, track.Id);
                        if (playlistTrack == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Track is was not in the playlist"
                                   }
                        }
                        ;

                        mgr.RemoveFromPlaylist(playlist.Id, track.Id);
                        nrOfItemsRemoved++;
                    }
                    else
                    {
                        var album = mgr.Get <Domain.Objects.Album>(request.Item.Id);
                        if (album == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Album not found"
                                   }
                        }
                        ;

                        var tracks = mgr.FindTracks(null, Domain.Objects.Playlist.ALL_ID, album.Id, 0, int.MaxValue);
                        foreach (var track in tracks)
                        {
                            var playlistTrack = mgr.GetPlaylistTrack(playlist.Id, track.Id);

                            if (playlistTrack != null)
                            {
                                mgr.RemoveFromPlaylist(playlist.Id, track.Id);
                                nrOfItemsRemoved++;
                            }
                        }
                    }
                    playlist.NrOfTracks -= nrOfItemsRemoved;
                    mgr.Set <Domain.Objects.Playlist>(playlist);

                    return(new Result()
                    {
                        Success = true
                    });
                }
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <Result>(ex));
            }
        }