vlw.se/endpoints/battlestation/psu/GET.php

96 lines
No EOL
2.6 KiB
PHP

<?php
use Reflect\{Response, Path};
use ReflectRules\{Ruleset, Rules, Type};
use VLW\Database\Database;
use VLW\Database\Tables\Battlestation\{
PsuTable,
EightyplusRatingEnum
};
use VLW\Database\Tables\Battlestation\Config\MbPsuTable;
require_once Path::root("src/Database/Database.php");
require_once Path::root("src/Database/Models/Battlestation/Psu.php");
require_once Path::root("src/Database/Models/Battlestation/Config/MbPsu.php");
class GET_BattlestationPsu extends Database {
private const REL_MOTHERBOARDS = "motherboards";
protected Ruleset $ruleset;
private array $query;
private array $results = [];
public function __construct() {
$this->ruleset = new Ruleset(strict: true);
$this->ruleset->GET([
(new Rules(PsuTable::ID->value))
->type(Type::STRING)
->min(parent::UUID_LENGTH)
->max(parent::UUID_LENGTH),
(new Rules(PsuTable::POWER->value))
->type(Type::NUMBER)
->min(1)
->max(parent::MYSQL_INT_MAX_LENGTH),
(new Rules(PsuTable::EIGHTYPLUS_RATING->value))
->type(Type::ENUM, EightyplusRatingEnum::names()),
(new Rules(PsuTable::VENDOR_NAME->value))
->type(Type::STRING)
->min(1)
->max(parent::MYSQL_VARCHAR_MAX_LENGTH),
(new Rules(PsuTable::VENDOR_MODEL->value))
->type(Type::STRING)
->min(1)
->max(parent::MYSQL_VARCHAR_MAX_LENGTH),
(new Rules(PsuTable::IS_RETIRED->value))
->type(Type::BOOLEAN)
]);
parent::__construct(Databases::BATTLESTATION, $this->ruleset);
// Use a copy of search parameters
$this->query = $_GET;
}
private function get_motherboards(): void {
foreach ($this->results as &$result) {
// Get motherboard id from relationship by chassis id
$result[self::REL_MOTHERBOARDS] = $this->db
->for(MbPsuTable::NAME)
->where([MbPsuTable::REF_PSU_ID->value => $result[PsuTable::ID->value]])
->select(MbPsuTable::values())
->fetch_all(MYSQLI_ASSOC);
}
}
private function get_psu(): array {
return $this->results = $this->db
->for(PsuTable::NAME)
->where($this->query)
->order([PsuTable::DATE_AQUIRED->value => "DESC"])
->select(PsuTable::values())
->fetch_all(MYSQLI_ASSOC);
}
public function main(): Response {
// Set properties as "searchable"
parent::make_wildcard_search(PsuTable::VENDOR_NAME->value, $this->query);
parent::make_wildcard_search(PsuTable::VENDOR_MODEL->value, $this->query);
// Get hardware
$this->get_psu();
// Resolve hardware relationships
$this->get_motherboards();
// Return 404 Not Found if response array is empty
return new Response($this->results, $this->results ? 200 : 404);
}
}