Fix SavePlaylistQuery

This commit is contained in:
casey langen 2020-10-06 23:04:46 -07:00
parent 37d7acb147
commit c88942569b
2 changed files with 37 additions and 21 deletions

View File

@ -344,9 +344,6 @@ bool SavePlaylistQuery::CreatePlaylist(musik::core::db::Connection &db) {
}
}
this->library->GetMessageQueue().Broadcast(
Message::Create(nullptr, message::PlaylistCreated, playlistId));
return true;
}
@ -354,14 +351,7 @@ bool SavePlaylistQuery::RenamePlaylist(musik::core::db::Connection &db) {
Statement renamePlaylist(RENAME_PLAYLIST_QUERY.c_str(), db);
renamePlaylist.BindText(0, this->playlistName);
renamePlaylist.BindInt64(1, this->playlistId);
bool result = (renamePlaylist.Step() != db::Error);
if (result) {
this->library->GetMessageQueue().Broadcast(
Message::Create(nullptr, message::PlaylistRenamed, playlistId));
}
return result;
return (renamePlaylist.Step() != db::Error);
}
bool SavePlaylistQuery::ReplacePlaylist(musik::core::db::Connection &db) {
@ -382,9 +372,6 @@ bool SavePlaylistQuery::ReplacePlaylist(musik::core::db::Connection &db) {
return false;
}
this->library->GetMessageQueue().Broadcast(
Message::Create(nullptr, message::PlaylistModified, playlistId));
return true;
}
@ -399,9 +386,6 @@ bool SavePlaylistQuery::AppendToPlaylist(musik::core::db::Connection& db) {
transaction.Cancel();
}
this->library->GetMessageQueue().Broadcast(
Message::Create(nullptr, message::PlaylistModified, playlistId));
return result;
}
@ -413,9 +397,33 @@ bool SavePlaylistQuery::OnRun(musik::core::db::Connection &db) {
case Operation::Create: this->result = this->CreatePlaylist(db); break;
case Operation::Append: this->result = this->AppendToPlaylist(db); break;
}
if (this->result) {
this->SendPlaylistMutationBroadcast();
}
return this->result;
}
void SavePlaylistQuery::SendPlaylistMutationBroadcast() {
switch (this->op) {
case Operation::Rename:
this->library->GetMessageQueue().Broadcast(
Message::Create(nullptr, message::PlaylistRenamed, playlistId));
break;
case Operation::Replace:
this->library->GetMessageQueue().Broadcast(
Message::Create(nullptr, message::PlaylistModified, playlistId));
break;
case Operation::Create:
this->library->GetMessageQueue().Broadcast(
Message::Create(nullptr, message::PlaylistCreated, playlistId));
break;
case Operation::Append:
this->library->GetMessageQueue().Broadcast(
Message::Create(nullptr, message::PlaylistModified, playlistId));
break;
}
}
/* SUPPORTING TYPES */
SavePlaylistQuery::TrackListWrapper::TrackListWrapper() {
@ -469,15 +477,18 @@ ITrackList* SavePlaylistQuery::TrackListWrapper::Get() {
/* ISerializableQuery */
std::string SavePlaylistQuery::SerializeQuery() {
nlohmann::json tracksJson = tracks.Get()
? ITrackListToJsonIdList(*tracks.Get())
: nlohmann::json();
nlohmann::json output = {
{ "name", kQueryName },
{ "name", kQueryName },
{ "options", {
{ "op", this->op },
{ "playlistName", this->playlistName },
{ "categoryType", this->categoryType },
{ "playlistId", this->playlistId },
{ "categoryId", this->categoryId },
{ "tracks", ITrackListToJsonIdList(*tracks.Get()) }
{ "tracks", tracksJson }
}}
};
return output.dump();
@ -490,8 +501,11 @@ std::string SavePlaylistQuery::SerializeResult() {
void SavePlaylistQuery::DeserializeResult(const std::string& data) {
auto input = nlohmann::json::parse(data);
this->SetStatus(input["result"].get<bool>() == true
? IQuery::Finished : IQuery::Failed);
this->result = input["result"].get<bool>();
this->SetStatus(result ? IQuery::Finished : IQuery::Failed);
if (result) {
SendPlaylistMutationBroadcast();
}
}
std::shared_ptr<SavePlaylistQuery> SavePlaylistQuery::DeserializeQuery(

View File

@ -144,6 +144,8 @@ namespace musik { namespace core { namespace library { namespace query {
SavePlaylistQuery(musik::core::ILibraryPtr library); /* for query (de)serialization */
void SendPlaylistMutationBroadcast();
struct TrackListWrapper {
TrackListWrapper();
TrackListWrapper(std::shared_ptr<musik::core::TrackList> shared);