From 14bc2db98d44f351308bb670bc992d332238a585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Gel=C3=B3czi?= Date: Sun, 19 Dec 2021 21:42:27 +0100 Subject: [PATCH] Add to playlist, remove from playlist --- .../AudioStationConnector_Tests.cs | 30 ++++++++++++++++--- .../AudioStationClient.Playlist.cs | 30 ++++++++++++++++++- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/SynologyDotNet.AudioStation.IntegrationTest/AudioStationConnector_Tests.cs b/SynologyDotNet.AudioStation.IntegrationTest/AudioStationConnector_Tests.cs index 13572aa..f8fae52 100644 --- a/SynologyDotNet.AudioStation.IntegrationTest/AudioStationConnector_Tests.cs +++ b/SynologyDotNet.AudioStation.IntegrationTest/AudioStationConnector_Tests.cs @@ -363,7 +363,7 @@ public async Task GetSongById() } [TestMethod] - public async Task ListPlaylists() + public async Task Playlist_List() { var response = await AudioStation.ListPlaylistsAsync(TestPageSize, 0); Assert.IsTrue(response.Success); @@ -372,9 +372,9 @@ public async Task ListPlaylists() } [TestMethod] - public async Task GetPlaylist() + public async Task Playlist_Get() { - var playlist = await AudioStation.GetPlaylistAsync(TestPageSize, 0, TestPlaylist.ID, SongQueryAdditional.None); + var playlist = await AudioStation.GetPlaylistAsync(TestPageSize, 0, TestPlaylist.ID); Assert.IsTrue(playlist.Success); Assert.IsFalse(string.IsNullOrEmpty(playlist.Data.ID)); Assert.IsTrue(playlist.Data.Additional.Songs.Length > 0); @@ -385,7 +385,7 @@ public async Task GetPlaylist() } [TestMethod] - public async Task GetPlaylist_SongDetails() + public async Task Playlist_GetWithSongDetails() { var playlist = await AudioStation.GetPlaylistAsync(TestPageSize, 0, TestPlaylist.ID, SongQueryAdditional.All); Assert.IsTrue(playlist.Success); @@ -397,6 +397,28 @@ public async Task GetPlaylist_SongDetails() } } + [TestMethod] + public async Task Playlist_AddAndRemoveSong() + { + // Add test song to playlist + var response = await AudioStation.AddSongsToPlaylist(TestPlaylist.ID, TestSong.ID); + Assert.IsTrue(response.Success); + + // Get all songs on playlist + var playlist = await AudioStation.GetPlaylistAsync(TestPageSize, 0, TestPlaylist.ID); + Assert.IsTrue(playlist.Success); + Assert.IsTrue(playlist.Data.Additional.Songs.Any(x => x.ID == TestSong.ID)); + + // Remove the test song from the playlist + var testSongPlaylistIndex = playlist.Data.Additional.Songs.Select((song, index) => (song, index)).First(x => x.song.ID == TestSong.ID).index; + response = await AudioStation.RemoveSongsFromPlaylist(TestPlaylist.ID, testSongPlaylistIndex, 1); + + // Check + playlist = await AudioStation.GetPlaylistAsync(TestPageSize, 0, TestPlaylist.ID); + Assert.IsTrue(playlist.Success); + Assert.IsFalse(playlist.Data.Additional.Songs.Any(x => x.ID == TestSong.ID)); + } + [TestMethod] public async Task GetFileTags() { diff --git a/SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs b/SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs index d9aa31a..5a35a39 100644 --- a/SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs +++ b/SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs @@ -30,7 +30,7 @@ public async Task> ListPlaylistsAsync(int limit, /// The identifier. /// Additional fields to load. /// - public async Task> GetPlaylistAsync(int limit, int offset, string id, SongQueryAdditional additionalFields) + public async Task> GetPlaylistAsync(int limit, int offset, string id, SongQueryAdditional additionalFields = SongQueryAdditional.None) { var args = new List<(string, object)>(); args.Add(GetLibraryArg()); @@ -59,5 +59,33 @@ public async Task> GetPlaylistAsync(int limit, int off var playlists = await Client.QueryListAsync>(SYNO_AudioStation_Playlist, "getinfo", limit, offset, args.ToArray()); return new ApiDataResponse(playlists, playlists.Data?.Playlists?.FirstOrDefault() ?? default); } + + public async Task AddSongsToPlaylist(string id, params string[] songIds) + { + return await Client.QueryObjectAsync(SYNO_AudioStation_Playlist, "updatesongs", + ("id", id), + ("offset", -1), + ("limit", 0), + ("songs", string.Join(",", songIds)) + ); + } + + /// + /// Removes the selected song range from the specified playlist. + /// The Playlist API does NOT support removing songs by ID directly, you must query the playlist first. + /// + /// The identifier of the Playlist. + /// The index of the first song to be deleted. + /// The count of songs to be removed from start index. + public async Task RemoveSongsFromPlaylist(string id, int startIndex, int count) + { + // The Playlist API does NOT support removing songs by ID, it is using the "offset" and "limit" parameters to define the range to remove. + return await Client.QueryObjectAsync(SYNO_AudioStation_Playlist, "updatesongs", + ("id", id), + ("offset", startIndex), + ("limit", count), + ("songs", string.Empty) + ); + } } }