command/playlist: save only if at least one song was added

This commit is contained in:
Max Kellermann 2021-11-11 09:44:47 +01:00
parent b4e72aba6c
commit 651f57bced
3 changed files with 18 additions and 8 deletions

View File

@ -233,10 +233,12 @@ handle_playlistadd_position(Client &client, const char *playlist_name,
#ifdef ENABLE_DATABASE
const DatabaseSelection selection(uri, true, nullptr);
SearchInsertIntoPlaylist(client.GetDatabaseOrThrow(),
client.GetStorage(),
selection,
editor, position);
if (SearchInsertIntoPlaylist(client.GetDatabaseOrThrow(),
client.GetStorage(),
selection,
editor, position) == 0)
/* no song was found, don't need to save */
return CommandResult::OK;
#else
(void)client;
r.Error(ACK_ERROR_NO_EXIST, "No database");

View File

@ -42,7 +42,7 @@ search_add_to_playlist(const Database &db, const Storage *storage,
db.Visit(selection, f);
}
void
unsigned
SearchInsertIntoPlaylist(const Database &db, const Storage *storage,
const DatabaseSelection &selection,
PlaylistFileEditor &playlist,
@ -50,9 +50,14 @@ SearchInsertIntoPlaylist(const Database &db, const Storage *storage,
{
assert(position <= playlist.size());
db.Visit(selection, [&playlist, &position, storage](const auto &song){
playlist.Insert(position,
unsigned n = 0;
db.Visit(selection, [&playlist, &position, &n, storage](const auto &song){
playlist.Insert(position + n,
DatabaseDetachSong(storage, song));
++position;
++n;
});
return n;
}

View File

@ -33,7 +33,10 @@ search_add_to_playlist(const Database &db, const Storage *storage,
const char *playlist_path_utf8,
const DatabaseSelection &selection);
void
/**
* @return the number of songs added
*/
unsigned
SearchInsertIntoPlaylist(const Database &db, const Storage *storage,
const DatabaseSelection &selection,
PlaylistFileEditor &playlist,