mirror of
https://codeberg.org/vlw/vlw.se.git
synced 2025-09-13 21:13:40 +02:00
The PR is a huge refactor of all Reflect and Vegvisir code. I've merged the API and "Front-end" codebases together into the same root, this will allow for both Reflect and Vegvisir to use the same resources. Not only that, but I've also added proper database modeling with actual OOP inheritance for database tables. Reviewed-on: https://codeberg.org/vlw/vlw.se/pulls/23
96 lines
No EOL
2.4 KiB
PHP
96 lines
No EOL
2.4 KiB
PHP
<?php
|
|
|
|
use vlw\MySQL\Operators;
|
|
use Reflect\{Response, Path, Call};
|
|
use ReflectRules\{Ruleset, Rules, Type};
|
|
|
|
use VLW\API\Endpoints;
|
|
use VLW\Database\Database;
|
|
use const VLW\SEARCH_UPDATE_CACHE_PARM;
|
|
use VLW\Database\Tables\Search\{SearchTable, SearchCategoryEnum};
|
|
|
|
require_once Path::root("src/Consts.php");
|
|
require_once Path::root("src/API/Endpoints.php");
|
|
require_once Path::root("src/Database/Database.php");
|
|
require_once Path::root("src/Database/Tables/Search/Search.php");
|
|
|
|
class GET_Search extends Database {
|
|
protected Ruleset $ruleset;
|
|
|
|
public function __construct() {
|
|
$this->ruleset = new Ruleset(strict: true);
|
|
|
|
$this->ruleset->GET([
|
|
(new Rules(SearchTable::QUERY->value))
|
|
->type(Type::STRING)
|
|
->min(2)
|
|
->max(parent::SIZE_VARCHAR)
|
|
->default(null),
|
|
|
|
(new Rules(SearchTable::ID->value))
|
|
->type(Type::STRING)
|
|
->min(1)
|
|
->max(10)
|
|
->default(null),
|
|
|
|
(new Rules(SearchTable::CATEGORY->value))
|
|
->type(Type::ENUM, SearchCategoryEnum::names())
|
|
->default(null),
|
|
|
|
(new Rules(SEARCH_UPDATE_CACHE_PARM))
|
|
->type(Type::BOOLEAN)
|
|
->default(false)
|
|
]);
|
|
|
|
$this->ruleset->validate_or_exit();
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
private static function get_query(): string {
|
|
preg_match_all("/[a-zA-Z0-9]+/", $_GET[SearchTable::QUERY->value], $matches);
|
|
|
|
return strtolower(implode("", $matches[0]));
|
|
}
|
|
|
|
public function main(): Response {
|
|
// Freshen cache if update flag is set
|
|
if ($_GET[SEARCH_UPDATE_CACHE_PARM]) {
|
|
(new Call(Endpoints::SEARCH->value))->post();
|
|
}
|
|
|
|
$result = $this->db->for(SearchTable::NAME);
|
|
|
|
if ($_GET[SearchTable::ID->value]) {
|
|
$result = $result->where([SearchTable::ID->value => $_GET[SearchTable::ID->value]]);
|
|
} else if ($_GET[SearchTable::QUERY->value]) {
|
|
$query = self::get_query();
|
|
|
|
$filter = [
|
|
SearchTable::QUERY->value => [
|
|
Operators::LIKE->value => "%{$query}%"
|
|
]
|
|
];
|
|
|
|
if ($_GET[SearchTable::CATEGORY->value]) {
|
|
$filter[SearchTable::CATEGORY->value] = $_GET[SearchTable::CATEGORY->value];
|
|
}
|
|
|
|
$result = $result->where($filter);
|
|
} else {
|
|
new Response([], 400);
|
|
}
|
|
|
|
$result = $result->select([
|
|
SearchTable::ID->value,
|
|
SearchTable::TITLE->value,
|
|
SearchTable::SUMMARY->value,
|
|
SearchTable::CATEGORY->value,
|
|
SearchTable::HREF->value
|
|
]);
|
|
|
|
return $result->num_rows > 0
|
|
? new Response($result->fetch_all(MYSQLI_ASSOC))
|
|
: new Response([], 404);
|
|
}
|
|
} |