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
188 lines
No EOL
5.5 KiB
PHP
188 lines
No EOL
5.5 KiB
PHP
<?php
|
|
|
|
use Reflect\{Response, Path};
|
|
use ReflectRules\{Ruleset, Rules, Type};
|
|
|
|
use VLW\Database\Database;
|
|
use VLW\Database\Tables\Battlestation\{
|
|
MbTable,
|
|
MbFormfactorEnum
|
|
};
|
|
use VLW\Database\Tables\Battlestation\Config\{
|
|
MbGpuTable,
|
|
MbPsuTable,
|
|
MbDramTable,
|
|
MbStorageTable,
|
|
ChassisMbTable,
|
|
MbCpuCoolerModel
|
|
};
|
|
|
|
require_once Path::root("src/Database/Database.php");
|
|
require_once Path::root("src/Database/Tables/Battlestation/Mb.php");
|
|
require_once Path::root("src/Database/Tables/Battlestation/Config/MbPsu.php");
|
|
require_once Path::root("src/Database/Tables/Battlestation/Config/MbGpu.php");
|
|
require_once Path::root("src/Database/Tables/Battlestation/Config/MbDram.php");
|
|
require_once Path::root("src/Database/Tables/Battlestation/Config/MbStorage.php");
|
|
require_once Path::root("src/Database/Tables/Battlestation/Config/ChassisMb.php");
|
|
require_once Path::root("src/Database/Tables/Battlestation/Config/MbCpuCooler.php");
|
|
|
|
class GET_BattlestationMb extends Database {
|
|
private const REL_CPU = "cpus";
|
|
private const REL_PSU = "psus";
|
|
private const REL_GPU = "gpus";
|
|
private const REL_DRAM = "dram";
|
|
private const REL_STORAGE = "storage";
|
|
private const REL_CHASSIS = "chassis";
|
|
|
|
protected Ruleset $ruleset;
|
|
|
|
private array $query;
|
|
private array $results = [];
|
|
|
|
public function __construct() {
|
|
$this->ruleset = new Ruleset(strict: true);
|
|
|
|
$this->ruleset->GET([
|
|
(new Rules(MbTable::ID->value))
|
|
->type(Type::STRING)
|
|
->min(parent::UUID_LENGTH)
|
|
->max(parent::UUID_LENGTH),
|
|
|
|
(new Rules(MbTable::FORMFACTOR->value))
|
|
->type(Type::ENUM, MbFormfactorEnum::names()),
|
|
|
|
(new Rules(MbTable::VENDOR_NAME->value))
|
|
->type(Type::STRING)
|
|
->min(1)
|
|
->max(parent::SIZE_VARCHAR),
|
|
|
|
(new Rules(MbTable::VENDOR_MODEL->value))
|
|
->type(Type::STRING)
|
|
->min(1)
|
|
->max(parent::SIZE_VARCHAR),
|
|
|
|
(new Rules(MbTable::NETWORK_ETHERNET->value))
|
|
->type(Type::NULL)
|
|
->type(Type::STRING)
|
|
->min(1)
|
|
->max(parent::SIZE_VARCHAR),
|
|
|
|
(new Rules(MbTable::NETWORK_WLAN->value))
|
|
->type(Type::NULL)
|
|
->type(Type::STRING)
|
|
->min(1)
|
|
->max(parent::SIZE_VARCHAR),
|
|
|
|
(new Rules(MbTable::NETWORK_BLUETOOTH->value))
|
|
->type(Type::NULL)
|
|
->type(Type::STRING)
|
|
->min(1)
|
|
->max(parent::SIZE_VARCHAR),
|
|
|
|
(new Rules(MbTable::IS_RETIRED->value))
|
|
->type(Type::BOOLEAN)
|
|
]);
|
|
|
|
parent::__construct(Databases::BATTLESTATION, $this->ruleset);
|
|
|
|
// Use a copy of search parameters
|
|
$this->query = $_GET;
|
|
}
|
|
|
|
private function get_chassis(): void {
|
|
foreach ($this->results as &$result) {
|
|
// Get motherboard id from relationship by chassis id
|
|
$result[self::REL_CHASSIS] = $this->db
|
|
->for(ChassisMbTable::NAME)
|
|
->where([ChassisMbTable::REF_MB_ID->value => $result[MbTable::ID->value]])
|
|
->select(ChassisMbTable::values())
|
|
->fetch_all(MYSQLI_ASSOC);
|
|
}
|
|
}
|
|
|
|
private function get_psu(): void {
|
|
foreach ($this->results as &$result) {
|
|
// Get motherboard id from relationship by chassis id
|
|
$result[self::REL_PSU] = $this->db
|
|
->for(MbPsuTable::NAME)
|
|
->where([MbPsuTable::REF_MB_ID->value => $result[MbTable::ID->value]])
|
|
->select(MbPsuTable::values())
|
|
->fetch_all(MYSQLI_ASSOC);
|
|
}
|
|
}
|
|
|
|
private function get_cpu(): void {
|
|
foreach ($this->results as &$result) {
|
|
// Get motherboard id from relationship by chassis id
|
|
$result[self::REL_CPU] = $this->db
|
|
->for(MbCpuCoolerModel::NAME)
|
|
->where([MbCpuCoolerModel::REF_MB_ID->value => $result[MbTable::ID->value]])
|
|
->select(MbCpuCoolerModel::values())
|
|
->fetch_all(MYSQLI_ASSOC);
|
|
}
|
|
}
|
|
|
|
private function get_gpu(): void {
|
|
foreach ($this->results as &$result) {
|
|
// Get motherboard id from relationship by chassis id
|
|
$result[self::REL_GPU] = $this->db
|
|
->for(MbGpuTable::NAME)
|
|
->where([MbGpuTable::REF_MB_ID->value => $result[MbTable::ID->value]])
|
|
->select(MbGpuTable::values())
|
|
->fetch_all(MYSQLI_ASSOC);
|
|
}
|
|
}
|
|
|
|
private function get_dram(): void {
|
|
foreach ($this->results as &$result) {
|
|
// Get motherboard id from relationship by chassis id
|
|
$result[self::REL_DRAM] = $this->db
|
|
->for(MbDramTable::NAME)
|
|
->where([MbDramTable::REF_MB_ID->value => $result[MbTable::ID->value]])
|
|
->select(MbDramTable::values())
|
|
->fetch_all(MYSQLI_ASSOC);
|
|
}
|
|
}
|
|
|
|
private function get_storage(): void {
|
|
foreach ($this->results as &$result) {
|
|
// Get motherboard id from relationship by chassis id
|
|
$result[self::REL_STORAGE] = $this->db
|
|
->for(MbStorageTable::NAME)
|
|
->where([MbStorageTable::REF_MB_ID->value => $result[MbTable::ID->value]])
|
|
->select(MbStorageTable::values())
|
|
->fetch_all(MYSQLI_ASSOC);
|
|
}
|
|
}
|
|
|
|
// ----
|
|
|
|
private function get_motherboards(): array {
|
|
return $this->results = $this->db
|
|
->for(MbTable::NAME)
|
|
->where($this->query)
|
|
->order([MbTable::DATE_AQUIRED->value => "DESC"])
|
|
->select(MbTable::values())
|
|
->fetch_all(MYSQLI_ASSOC);
|
|
}
|
|
|
|
public function main(): Response {
|
|
// Set properties as "searchable"
|
|
parent::make_wildcard_search(MbTable::VENDOR_NAME->value, $this->query);
|
|
parent::make_wildcard_search(MbTable::VENDOR_MODEL->value, $this->query);
|
|
|
|
// Get hardware
|
|
$this->get_motherboards();
|
|
|
|
// Resolve hardware relationships
|
|
$this->get_chassis();
|
|
$this->get_cpu();
|
|
$this->get_psu();
|
|
$this->get_gpu();
|
|
$this->get_dram();
|
|
$this->get_storage();
|
|
|
|
// Return 404 Not Found if response array is empty
|
|
return new Response($this->results, $this->results ? 200 : 404);
|
|
}
|
|
} |