vlw.se/endpoints/work/permalinks/POST.php

61 lines
No EOL
1.6 KiB
PHP

<?php
use Reflect\Call;
use Reflect\{Response, Path};
use ReflectRules\{Ruleset, Rules, Type};
use VLW\Database\Database;
use VLW\Database\Tables\Work\PermalinksTable;
require_once Path::root("src/Database/Database.php");
require_once Path::root("src/Database/Models/Work/WorkPermalinks.php");
class POST_WorkPermalinks extends Database {
protected Ruleset $ruleset;
public function __construct() {
$this->ruleset = new Ruleset(strict: true);
$this->ruleset->POST([
(new Rules(PermalinksTable::ID->value))
->required()
->type(Type::STRING)
->min(1)
->max(parent::MYSQL_VARCHAR_MAX_LENGTH),
(new Rules(PermalinksTable::REF_WORK_ID->value))
->required()
->type(Type::STRING)
->min(1)
->max(parent::MYSQL_VARCHAR_MAX_LENGTH),
(new Rules(PermalinksTable::DATE_CREATED->value))
->type(Type::NUMBER)
->min(1)
->max(parent::MYSQL_INT_MAX_LENGTH)
->default(time())
]);
$ruleset->validate_or_exit();
parent::__construct();
}
private static function get_entity(): Response {
return (new Call(Endpoints::WORK->value))->params([
WorkTable::ID->value => $_POST[TagsTable::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(PermalinksTable::NAME)->insert($_POST) === true
? new Response($_POST[PermalinksTable::ID->value], 201)
: new Response("Failed to add permalink to work entity", 500);
}
}