From bb70c6dbafe35c9978d6842222100c1511fb534e Mon Sep 17 00:00:00 2001 From: emeric Date: Sat, 27 Apr 2024 16:36:51 +0200 Subject: [PATCH] Added a way to visit users --- src/libs/database/impl/User.cpp | 12 +++++ src/libs/database/include/database/User.hpp | 1 + src/libs/database/test/CMakeLists.txt | 1 + src/libs/database/test/User.cpp | 53 +++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 src/libs/database/test/User.cpp diff --git a/src/libs/database/impl/User.cpp b/src/libs/database/impl/User.cpp index cf5b356b..97efe9d8 100644 --- a/src/libs/database/impl/User.cpp +++ b/src/libs/database/impl/User.cpp @@ -61,6 +61,18 @@ namespace lms::db return utils::execRangeQuery(query, params.range); } + void User::find(Session& session, const FindParameters& params, const std::function& func) + { + auto query{ session.getDboSession()->find() }; + + if (params.scrobblingBackend) + query.where("scrobbling_backend = ?").bind(*params.scrobblingBackend); + if (params.feedbackBackend) + query.where("feedback_backend = ?").bind(*params.feedbackBackend); + + return utils::forEachQueryRangeResult(query, params.range, func); + } + User::pointer User::findDemoUser(Session& session) { session.checkReadTransaction(); diff --git a/src/libs/database/include/database/User.hpp b/src/libs/database/include/database/User.hpp index 9965c277..aa5e00df 100644 --- a/src/libs/database/include/database/User.hpp +++ b/src/libs/database/include/database/User.hpp @@ -72,6 +72,7 @@ namespace lms::db static pointer find(Session& session, UserId id); static pointer find(Session& session, std::string_view loginName); static RangeResults find(Session& session, const FindParameters& params); + static void find(Session& session, const FindParameters& params, const std::function& func); static pointer findDemoUser(Session& session); // accessors diff --git a/src/libs/database/test/CMakeLists.txt b/src/libs/database/test/CMakeLists.txt index 99bbf7b7..4ce77ebe 100644 --- a/src/libs/database/test/CMakeLists.txt +++ b/src/libs/database/test/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable(test-database TrackBookmark.cpp TrackFeatures.cpp TrackList.cpp + User.cpp ) target_link_libraries(test-database PRIVATE diff --git a/src/libs/database/test/User.cpp b/src/libs/database/test/User.cpp new file mode 100644 index 00000000..039e485c --- /dev/null +++ b/src/libs/database/test/User.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2024 Emeric Poupon + * + * This file is part of LMS. + * + * LMS is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LMS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LMS. If not, see . + */ + +#include "Common.hpp" + +namespace lms::db::tests +{ + TEST_F(DatabaseFixture, User) + { + { + auto transaction{ session.createReadTransaction() }; + + bool visited{}; + User::find(session, User::FindParameters{}, [&](const User::pointer&) + { + visited = true; + }); + EXPECT_FALSE(visited); + } + + ScopedUser user1{ session, "MyUser1" }; + ScopedUser user2{ session, "MyUser2" }; + + { + auto transaction{ session.createReadTransaction() }; + + std::vector visitedUsers; + User::find(session, User::FindParameters{}, [&](const User::pointer& user) + { + visitedUsers.push_back(user->getId()); + }); + EXPECT_EQ(visitedUsers.size(), 2); + EXPECT_EQ(visitedUsers[0], user1->getId()); + EXPECT_EQ(visitedUsers[1], user2->getId()); + } + } +} \ No newline at end of file