DistroqueryAPI: getRepositoryPackages: added support for query string

This commit is contained in:
Silvan Calarco 2024-07-06 18:28:15 +02:00
parent f1fd8ef275
commit e7e06284da
2 changed files with 15 additions and 4 deletions

View File

@ -104,7 +104,7 @@ json DistroqueryAPI::getPackageSourceDetailsById(configTag* ct, long id) {
return j; return j;
} }
json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int page) { json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int page, string query) {
json j; json j;
string sql; string sql;
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
@ -113,6 +113,7 @@ json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int
j["query"]["repository"] = repository; j["query"]["repository"] = repository;
j["query"]["per_page"] = per_page; j["query"]["per_page"] = per_page;
j["query"]["page"] = page; j["query"]["page"] = page;
j["query"]["q"] = query;
if (per_page < 1) { if (per_page < 1) {
j["error"] = "'per_page' query var must be >= 1"; j["error"] = "'per_page' query var must be >= 1";
@ -136,7 +137,13 @@ json DistroqueryAPI::getRepositoryPackages(string repository, int per_page, int
return j; return j;
} }
sql = "SELECT COUNT(*) OVER() AS total_count,* FROM sources ORDER BY name COLLATE NOCASE ASC" string query_append;
if (query != "")
query_append = "WHERE name LIKE '%" + query + "%' OR summary LIKE '%" + query +
"%' OR description LIKE '%" + query + "%'";
sql = "SELECT COUNT(*) OVER() AS total_count,* FROM sources " + query_append +
" ORDER BY name COLLATE NOCASE ASC"
" LIMIT " + to_string(per_page) + " LIMIT " + to_string(per_page) +
" OFFSET " + to_string(per_page * (page -1)); " OFFSET " + to_string(per_page * (page -1));
if (sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) { if (sqlite3_prepare_v2(db, sql.c_str(), sql.length(), &stmt, NULL) == SQLITE_OK) {
@ -442,6 +449,7 @@ void DistroqueryAPI::getApiResponse(string path_info) {
// Pagination // Pagination
unsigned int per_page = 100; unsigned int per_page = 100;
unsigned int page = 1; unsigned int page = 1;
string query;
if (query_vars.find("per_page") != query_vars.end()) { if (query_vars.find("per_page") != query_vars.end()) {
try { try {
per_page = atoi(query_vars["per_page"].c_str()); per_page = atoi(query_vars["per_page"].c_str());
@ -456,7 +464,10 @@ void DistroqueryAPI::getApiResponse(string path_info) {
cerr << "Error converting '" << query_vars["page"] << " to int: " << e.what() << endl; cerr << "Error converting '" << query_vars["page"] << " to int: " << e.what() << endl;
} }
} }
auto packages = getRepositoryPackages(path_split[1], per_page, page); if (query_vars.find("q") != query_vars.end()) {
query = urlDecode(query_vars["q"]);
}
auto packages = getRepositoryPackages(path_split[1], per_page, page, query);
cout << packages.dump(); cout << packages.dump();
} else if (path_split[0] == "package") { } else if (path_split[0] == "package") {
// API service: package // API service: package

View File

@ -41,7 +41,7 @@ class DistroqueryAPI {
configTag *config; configTag *config;
json configToJsonRepositories(); json configToJsonRepositories();
void sendErrorResponse(string message); void sendErrorResponse(string message);
json getRepositoryPackages(string repository, int per_page, int page); json getRepositoryPackages(string repository, int per_page, int page, string query);
json getPackageSourceDetailsById(configTag* ct, long id); json getPackageSourceDetailsById(configTag* ct, long id);
json getPackageSourceDetails(string repository, string package); json getPackageSourceDetails(string repository, string package);
json getBuiltPackagesFromSourceID(configTag* ct, long id); json getBuiltPackagesFromSourceID(configTag* ct, long id);