Added a way to visit users

This commit is contained in:
emeric
2024-04-27 16:36:51 +02:00
parent 1a9b392a71
commit bb70c6dbaf
4 changed files with 67 additions and 0 deletions
+12
View File
@@ -61,6 +61,18 @@ namespace lms::db
return utils::execRangeQuery<UserId>(query, params.range);
}
void User::find(Session& session, const FindParameters& params, const std::function<void(const User::pointer&)>& func)
{
auto query{ session.getDboSession()->find<User>() };
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();
@@ -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<UserId> find(Session& session, const FindParameters& params);
static void find(Session& session, const FindParameters& params, const std::function<void(const pointer&)>& func);
static pointer findDemoUser(Session& session);
// accessors
+1
View File
@@ -13,6 +13,7 @@ add_executable(test-database
TrackBookmark.cpp
TrackFeatures.cpp
TrackList.cpp
User.cpp
)
target_link_libraries(test-database PRIVATE
+53
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#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<UserId> 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());
}
}
}