mirror of
https://codeberg.org/vlw/vlw.se.git
synced 2025-09-13 21:13:40 +02:00
72 lines
No EOL
1.8 KiB
PHP
72 lines
No EOL
1.8 KiB
PHP
<?php
|
|
|
|
use Reflect\Call;
|
|
use Reflect\{Response, Path};
|
|
use ReflectRules\{Ruleset, Rules, Type};
|
|
|
|
use VLW\API\Endpoints;
|
|
use VLW\Database\Database;
|
|
use VLW\Database\Tables\Work\{
|
|
WorkTable,
|
|
ActionsTable
|
|
};
|
|
|
|
require_once Path::root("src/Endpoints.php");
|
|
require_once Path::root("src/Database/Database.php");
|
|
require_once Path::root("src/Database/Tables/Work/Work.php");
|
|
require_once Path::root("src/Database/Tables/Work/WorkActions.php");
|
|
|
|
class POST_WorkActions extends Database {
|
|
protected Ruleset $ruleset;
|
|
|
|
public function __construct() {
|
|
$this->ruleset = new Ruleset(strict: true);
|
|
|
|
$this->ruleset->POST([
|
|
(new Rules(ActionsTable::REF_WORK_ID->value))
|
|
->required()
|
|
->min(1)
|
|
->max(parent::SIZE_VARCHAR),
|
|
|
|
(new Rules(ActionsTable::DISPLAY_TEXT->value))
|
|
->required()
|
|
->type(Type::STRING)
|
|
->min(1)
|
|
->max(parent::SIZE_VARCHAR),
|
|
|
|
(new Rules(ActionsTable::HREF->value))
|
|
->required()
|
|
->type(Type::STRING)
|
|
->type(Type::NULL)
|
|
->min(1)
|
|
->max(parent::SIZE_VARCHAR),
|
|
|
|
(new Rules(ActionsTable::CLASS_LIST->value))
|
|
->type(Type::ARRAY)
|
|
->min(1)
|
|
->default([])
|
|
]);
|
|
|
|
$this->ruleset->validate_or_exit();
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
private static function get_entity(): Response {
|
|
return (new Call(Endpoints::WORK->value))->params([
|
|
WorkTable::ID->value => $_POST[ActionsTable::REF_WORK_ID->value]
|
|
])->get();
|
|
}
|
|
|
|
public function main(): Response {
|
|
// Bail out if work entity could not be fetched
|
|
$entity = self::get_entity();
|
|
if (!$entity->ok) {
|
|
return $entity;
|
|
}
|
|
|
|
return $this->db->for(ActionsTable::NAME)->insert($_POST) === true
|
|
? new Response($_POST[ActionsTable::REF_WORK_ID->value], 201)
|
|
: new Response("Failed to add action to work entity", 500);
|
|
}
|
|
} |