Skip to content

Commit

Permalink
Add to playlist, remove from playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
geloczi committed Dec 19, 2021
1 parent 870d9cd commit 14bc2db
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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()
{
Expand Down
30 changes: 29 additions & 1 deletion SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<ApiListRessponse<PlaylistList>> ListPlaylistsAsync(int limit,
/// <param name="id">The identifier.</param>
/// <param name="additionalFields">Additional fields to load.</param>
/// <returns></returns>
public async Task<ApiDataResponse<Playlist>> GetPlaylistAsync(int limit, int offset, string id, SongQueryAdditional additionalFields)
public async Task<ApiDataResponse<Playlist>> GetPlaylistAsync(int limit, int offset, string id, SongQueryAdditional additionalFields = SongQueryAdditional.None)
{
var args = new List<(string, object)>();
args.Add(GetLibraryArg());
Expand Down Expand Up @@ -59,5 +59,33 @@ public async Task<ApiDataResponse<Playlist>> GetPlaylistAsync(int limit, int off
var playlists = await Client.QueryListAsync<ApiListRessponse<PlaylistList>>(SYNO_AudioStation_Playlist, "getinfo", limit, offset, args.ToArray());
return new ApiDataResponse<Playlist>(playlists, playlists.Data?.Playlists?.FirstOrDefault() ?? default);
}

public async Task<ApiResponse> AddSongsToPlaylist(string id, params string[] songIds)
{
return await Client.QueryObjectAsync<ApiResponse>(SYNO_AudioStation_Playlist, "updatesongs",
("id", id),
("offset", -1),
("limit", 0),
("songs", string.Join(",", songIds))
);
}

/// <summary>
/// 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.
/// </summary>
/// <param name="id">The identifier of the Playlist.</param>
/// <param name="startIndex">The index of the first song to be deleted.</param>
/// <param name="count">The count of songs to be removed from start index.</param>
public async Task<ApiResponse> 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<ApiResponse>(SYNO_AudioStation_Playlist, "updatesongs",
("id", id),
("offset", startIndex),
("limit", count),
("songs", string.Empty)
);
}
}
}

0 comments on commit 14bc2db

Please sign in to comment.