24 #include <nlohmann/json.hpp>
60 template <
typename DatabaseEntity,
typename AliasOrName>
64 using Query = odb::query<DatabaseEntity>;
68 bool operator()(
const DatabaseEntity &e1,
const DatabaseEntity &e2)
70 static_assert(std::is_integral<decltype(e1.id())>::value,
71 "Id is not an integral type.");
74 return e1.id() < e2.id();
78 using EntitySet = std::set<DatabaseEntity, DatabaseEntityComparator>;
82 std::enable_if_t<std::is_same<T, use_alias_tag>::value,
Query>
85 Query q(Query::alias.like(
"%" + partial +
"%"));
90 std::enable_if_t<std::is_same<T, use_name_tag>::value,
Query>
93 Query q(Query::name.like(
"%" + partial +
"%"));
98 std::enable_if_t<std::is_same<T, use_username_tag>::value,
Query>
101 Query q(Query::username.like(
"%" + partial +
"%"));
107 Query q(build_query<AliasOrName>(partial));
108 auto results = db->query(q);
109 for (
const auto &ap : results)
117 const std::string &partial_name_or_alias)
119 odb::transaction t(db->begin());
127 for (
auto i = 0u; i < partial_name_or_alias.length(); ++i)
129 auto partial_name_copy = partial_name_or_alias;
130 partial_name_copy[i] = std::toupper(partial_name_or_alias[i]);
134 return std::vector<DatabaseEntity>(entities.begin(), entities.end());
138 template <
typename T>
139 std::enable_if_t<std::is_same<T, use_alias_tag>::value,
json>
142 json result_json = {{
"id", entity.id()}, {
"alias", entity.alias()}};
146 template <
typename T>
147 std::enable_if_t<std::is_same<T, use_name_tag>::value,
json>
150 json result_json = {{
"id", entity.id()}, {
"name", entity.name()}};
154 template <
typename T>
155 std::enable_if_t<std::is_same<T, use_username_tag>::value,
json>
158 json result_json = {{
"id", entity.id()}, {
"username", entity.username()}};
173 auto entities =
search(db, partial);
175 for (
const auto &entity : entities)
177 rep.push_back(build_json_entry<AliasOrName>(entity));