diff options
| author | Carl Hetherington <cth@carlh.net> | 2026-01-24 13:07:04 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2026-02-03 21:37:04 +0100 |
| commit | 79cc9b66c779aa4e0c8a2485653da316bafd8e97 (patch) | |
| tree | aeea4caf098ad7a1c939957ff90ed08e5891cbeb | |
| parent | 76a37b29981d77c9bd7f20dc4ecca4a73b10b8f4 (diff) | |
Add move_entry().
| -rw-r--r-- | src/lib/show_playlist_list.cc | 40 | ||||
| -rw-r--r-- | src/lib/show_playlist_list.h | 2 | ||||
| -rw-r--r-- | test/show_playlist_test.cc | 31 |
3 files changed, 73 insertions, 0 deletions
diff --git a/src/lib/show_playlist_list.cc b/src/lib/show_playlist_list.cc index 980a10361..6dfbbad29 100644 --- a/src/lib/show_playlist_list.cc +++ b/src/lib/show_playlist_list.cc @@ -277,6 +277,46 @@ ShowPlaylistList::insert_entry(ShowPlaylistID playlist_id, ShowPlaylistEntry con void +ShowPlaylistList::move_entry(ShowPlaylistID playlist_id, int old_index, int new_index) +{ + SQLiteTransaction transaction(_db); + + if (old_index == new_index) { + return; + } + + SQLiteStatement find_id(_db, "SELECT id FROM entries WHERE show_playlist=? AND sort_index=?"); + find_id.bind_int64(1, playlist_id.get()); + find_id.bind_int64(2, old_index); + + optional<int> moving_id; + find_id.execute([&moving_id](SQLiteStatement& statement) { + DCPOMATIC_ASSERT(statement.data_count() == 1); + moving_id = statement.column_int64(0); + }); + DCPOMATIC_ASSERT(moving_id); + + auto const lower = old_index < new_index ? (old_index + 1) : new_index; + auto const upper = old_index < new_index ? new_index : (old_index - 1); + auto const direction = old_index < new_index ? "-1" : "+1"; + + SQLiteStatement update_others(_db, fmt::format("UPDATE entries SET sort_index=sort_index{} WHERE show_playlist=? AND sort_index>=? AND sort_index<=?", direction)); + update_others.bind_int64(1, playlist_id.get()); + update_others.bind_int64(2, lower); + update_others.bind_int64(3, upper); + update_others.execute(); + + SQLiteStatement update(_db, "UPDATE entries SET sort_index=? WHERE show_playlist=? AND id=?"); + update.bind_int64(1, new_index); + update.bind_int64(2, playlist_id.get()); + update.bind_int64(3, *moving_id); + update.execute(); + + transaction.commit(); +} + + +void ShowPlaylistList::update_entry(ShowPlaylistID playlist_id, int index, ShowPlaylistEntry const& entry) { SQLiteStatement update_entry(_db, _entries.update("WHERE show_playlist=? AND sort_index=?")); diff --git a/src/lib/show_playlist_list.h b/src/lib/show_playlist_list.h index 6ebadfc37..0da94ba99 100644 --- a/src/lib/show_playlist_list.h +++ b/src/lib/show_playlist_list.h @@ -80,6 +80,8 @@ public: void move_entry_up(ShowPlaylistID, int index); /** Move the given playlist entry one place lower (later) */ void move_entry_down(ShowPlaylistID, int index); + /** Move the given playlist entry to the given index */ + void move_entry(ShowPlaylistID playlist_id, int old_index, int new_index); void read_legacy(boost::filesystem::path dir); diff --git a/test/show_playlist_test.cc b/test/show_playlist_test.cc index 74d3e6cee..958e993b4 100644 --- a/test/show_playlist_test.cc +++ b/test/show_playlist_test.cc @@ -166,6 +166,37 @@ BOOST_AUTO_TEST_CASE(test_show_playlist_list) BOOST_CHECK_EQUAL(list.show_playlists().size(), 1U); BOOST_CHECK(list.show_playlists()[0].first == id2); BOOST_CHECK(list.show_playlists()[0].second == spl3); + + auto spl4 = ShowPlaylist("The Meaning of Life"); + auto id4 = list.add_show_playlist(spl4); + + list.add_entry(id4, ShowPlaylistEntry(store->get("e781b9d108a555b0fa12bfbaf308f0202058"), {})); + list.add_entry(id4, ShowPlaylistEntry(store->get("70eb015a-6328-468e-b53d-0211faaca64f"), {})); + list.add_entry(id4, ShowPlaylistEntry(store->get("22a6a978-4f66-4eae-96d9-91d638016616"), {})); + list.add_entry(id4, ShowPlaylistEntry(store->get("b54730b85a5f8ebefcb003dff602a3bb2085"), {})); + list.add_entry(id4, ShowPlaylistEntry(store->get("95caf5d5-2d33-45f3-b79e-a82c6932830d"), {})); + list.add_entry(id4, ShowPlaylistEntry(store->get("f166bb61-9312-4afa-bf54-6a1bdfb75a12"), {})); + list.add_entry(id4, ShowPlaylistEntry(store->get("b1f91cd7-830c-43ca-97fe-bac388d33061"), {})); + + list.move_entry(id4, 2, 5); + + BOOST_CHECK_EQUAL(list.entries(id4).size(), 7U); + BOOST_CHECK(list.entries(id4)[0] == ShowPlaylistEntry(store->get("e781b9d108a555b0fa12bfbaf308f0202058"), {})); + BOOST_CHECK(list.entries(id4)[1] == ShowPlaylistEntry(store->get("70eb015a-6328-468e-b53d-0211faaca64f"), {})); + BOOST_CHECK(list.entries(id4)[2] == ShowPlaylistEntry(store->get("b54730b85a5f8ebefcb003dff602a3bb2085"), {})); + BOOST_CHECK(list.entries(id4)[3] == ShowPlaylistEntry(store->get("95caf5d5-2d33-45f3-b79e-a82c6932830d"), {})); + BOOST_CHECK(list.entries(id4)[4] == ShowPlaylistEntry(store->get("f166bb61-9312-4afa-bf54-6a1bdfb75a12"), {})); + BOOST_CHECK(list.entries(id4)[5] == ShowPlaylistEntry(store->get("22a6a978-4f66-4eae-96d9-91d638016616"), {})); + BOOST_CHECK(list.entries(id4)[6] == ShowPlaylistEntry(store->get("b1f91cd7-830c-43ca-97fe-bac388d33061"), {})); + + list.move_entry(id4, 6, 0); + BOOST_CHECK(list.entries(id4)[0] == ShowPlaylistEntry(store->get("b1f91cd7-830c-43ca-97fe-bac388d33061"), {})); + BOOST_CHECK(list.entries(id4)[1] == ShowPlaylistEntry(store->get("e781b9d108a555b0fa12bfbaf308f0202058"), {})); + BOOST_CHECK(list.entries(id4)[2] == ShowPlaylistEntry(store->get("70eb015a-6328-468e-b53d-0211faaca64f"), {})); + BOOST_CHECK(list.entries(id4)[3] == ShowPlaylistEntry(store->get("b54730b85a5f8ebefcb003dff602a3bb2085"), {})); + BOOST_CHECK(list.entries(id4)[4] == ShowPlaylistEntry(store->get("95caf5d5-2d33-45f3-b79e-a82c6932830d"), {})); + BOOST_CHECK(list.entries(id4)[5] == ShowPlaylistEntry(store->get("f166bb61-9312-4afa-bf54-6a1bdfb75a12"), {})); + BOOST_CHECK(list.entries(id4)[6] == ShowPlaylistEntry(store->get("22a6a978-4f66-4eae-96d9-91d638016616"), {})); } |
