mirror of
https://codeberg.org/vlw/vlw.se.git
synced 2025-09-13 21:13:40 +02:00
wip: 2025-02-05T04:38:16+0100 (1738726696)
This commit is contained in:
parent
ecd9c4e3cc
commit
f961579c79
7 changed files with 0 additions and 476 deletions
|
@ -1,60 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Reflect\{Response, Path};
|
||||
use ReflectRules\{Ruleset, Rules, Type};
|
||||
|
||||
use const VLW\API\RESP_DELETE_OK;
|
||||
use VLW\Database\Database;
|
||||
use VLW\Database\Tables\Work\WorkTable;
|
||||
|
||||
require_once Path::root("src/Endpoints.php");
|
||||
require_once Path::root("src/Database/Database.php");
|
||||
require_once Path::root("src/Database/Tables/Work.php");
|
||||
|
||||
class DELETE_Work extends Database {
|
||||
protected Ruleset $ruleset;
|
||||
|
||||
public function __construct() {
|
||||
$this->ruleset = new Ruleset(strict: true);
|
||||
|
||||
$this->ruleset->POST([
|
||||
(new Rules(WorkTable::ID->value))
|
||||
->type(Type::STRING)
|
||||
->min(1)
|
||||
->max(parent::SIZE_VARCHAR),
|
||||
|
||||
(new Rules(WorkTable::TITLE->value))
|
||||
->type(Type::STRING)
|
||||
->min(3)
|
||||
->max(parent::SIZE_VARCHAR),
|
||||
|
||||
(new Rules(WorkTable::SUMMARY->value))
|
||||
->type(Type::STRING)
|
||||
->min(1)
|
||||
->max(parent::SIZE_TEXT),
|
||||
|
||||
(new Rules(WorkTable::IS_LISTED->value))
|
||||
->type(Type::BOOLEAN),
|
||||
|
||||
(new Rules(WorkTable::DATE_MODIFIED->value))
|
||||
->type(Type::NUMBER)
|
||||
->min(1)
|
||||
->max(parent::SIZE_UINT8),
|
||||
|
||||
(new Rules(WorkTable::DATE_CREATED->value))
|
||||
->type(Type::NUMBER)
|
||||
->min(1)
|
||||
->max(parent::SIZE_UINT8)
|
||||
]);
|
||||
|
||||
$this->ruleset->validate_or_exit();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function main(): Response {
|
||||
return $this->db->for(FieldsEnumsModel::NAME)->delete($_POST) === true
|
||||
? new Response(RESP_DELETE_OK)
|
||||
: new Response("Failed to delete work entity", 500);
|
||||
}
|
||||
}
|
|
@ -1,109 +0,0 @@
|
|||
<?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,
|
||||
PermalinksTable
|
||||
};
|
||||
|
||||
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/WorkPermalinks.php");
|
||||
|
||||
class PATCH_Work extends Database {
|
||||
protected Ruleset $ruleset;
|
||||
|
||||
public function __construct() {
|
||||
$this->ruleset = new Ruleset(strict: true);
|
||||
|
||||
$this->ruleset->GET([
|
||||
(new Rules(WorkTable::ID->value))
|
||||
->required()
|
||||
->type(Type::STRING)
|
||||
->min(1)
|
||||
->max(parent::SIZE_VARCHAR)
|
||||
]);
|
||||
|
||||
$this->ruleset->POST([
|
||||
(new Rules(WorkTable::TITLE->value))
|
||||
->type(Type::STRING)
|
||||
->min(3)
|
||||
->max(parent::SIZE_VARCHAR),
|
||||
|
||||
(new Rules(WorkTable::SUMMARY->value))
|
||||
->type(Type::STRING)
|
||||
->min(1)
|
||||
->max(parent::SIZE_TEXT),
|
||||
|
||||
(new Rules(WorkTable::IS_LISTED->value))
|
||||
->type(Type::BOOLEAN),
|
||||
|
||||
(new Rules(WorkTable::DATE_MODIFIED->value))
|
||||
->type(Type::NUMBER)
|
||||
->min(1)
|
||||
->max(parent::SIZE_UINT8)
|
||||
->default(time()),
|
||||
|
||||
(new Rules(WorkTable::DATE_CREATED->value))
|
||||
->type(Type::NUMBER)
|
||||
->min(1)
|
||||
->max(parent::SIZE_UINT8)
|
||||
]);
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
// Generate a slug URL from string
|
||||
private static function gen_slug(string $input): string {
|
||||
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $input)));
|
||||
}
|
||||
|
||||
// Compute and return modeled year, month, and day from Unix timestamp in request body
|
||||
private static function gen_date_created(): array {
|
||||
return [
|
||||
WorkTable::DATE_YEAR->value => date("Y", $_POST[WorkTable::DATE_CREATED->value]),
|
||||
WorkTable::DATE_MONTH ->value => date("n", $_POST[WorkTable::DATE_CREATED->value]),
|
||||
WorkTable::DATE_DAY->value => date("j", $_POST[WorkTable::DATE_CREATED->value])
|
||||
];
|
||||
}
|
||||
|
||||
private function get_entity_by_id(string $id): Response {
|
||||
return (new Call(Endpoints::WORK->value))->params([
|
||||
WorkTable::ID->value => $id
|
||||
])->get();
|
||||
}
|
||||
|
||||
public function main(): Response {
|
||||
// Use copy of request body as entity
|
||||
$entity = $_POST;
|
||||
|
||||
// Generate a new slug id from title if changed
|
||||
if ($_POST[WorkTable::TITLE->value]) {
|
||||
$slug = $_POST[WorkTable::TITLE->value];
|
||||
|
||||
// Bail out if the slug generated from the new tite already exist
|
||||
if ($this->get_entity_by_id($slug)) {
|
||||
return new Response("An entity with this title already exist", 409);
|
||||
}
|
||||
|
||||
// Add the new slug to update entity
|
||||
$entity[WorkTable::ID] = $slug;
|
||||
}
|
||||
|
||||
// Generate new work date fields from timestamp
|
||||
if ($_POST[WorkTable::DATE_CREATED->value]) {
|
||||
array_merge($entity, self::gen_date_created());
|
||||
}
|
||||
|
||||
// Update entity by existing id
|
||||
return $this->db->for(WorkTable::NAME)->where([WorkTable::ID->value => $_GET[WorkTable::ID->value]])->update($entity) === true
|
||||
? new Response($_GET[WorkTable::ID->value])
|
||||
: new Response("Failed to update entity", 500);
|
||||
}
|
||||
}
|
|
@ -1,106 +0,0 @@
|
|||
<?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,
|
||||
PermalinksTable
|
||||
};
|
||||
|
||||
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/WorkPermalinks.php");
|
||||
|
||||
class POST_Work extends Database {
|
||||
protected Ruleset $ruleset;
|
||||
|
||||
public function __construct() {
|
||||
$this->ruleset = new Ruleset(strict: true);
|
||||
|
||||
$this->ruleset->POST([
|
||||
(new Rules(WorkTable::TITLE->value))
|
||||
->type(Type::STRING)
|
||||
->min(3)
|
||||
->max(parent::SIZE_VARCHAR)
|
||||
->default(null),
|
||||
|
||||
(new Rules(WorkTable::SUMMARY->value))
|
||||
->type(Type::STRING)
|
||||
->min(1)
|
||||
->max(parent::SIZE_TEXT)
|
||||
->default(null),
|
||||
|
||||
(new Rules(WorkTable::IS_LISTED->value))
|
||||
->type(Type::BOOLEAN)
|
||||
->default(false),
|
||||
|
||||
(new Rules(WorkTable::DATE_CREATED->value))
|
||||
->type(Type::NUMBER)
|
||||
->min(1)
|
||||
->max(parent::SIZE_UINT8)
|
||||
->default(time())
|
||||
]);
|
||||
|
||||
$this->ruleset->validate_or_exit();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
// Generate a slug URL from string
|
||||
private static function gen_slug(string $input): string {
|
||||
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $input)));
|
||||
}
|
||||
|
||||
// Compute and return modeled year, month, and day from a Unix timestamp
|
||||
private static function gen_date_created(): array {
|
||||
// Use provided timestamp in request
|
||||
$date_created = $_POST[WorkTable::DATE_CREATED->value];
|
||||
|
||||
return [
|
||||
WorkTable::DATE_YEAR->value => date("Y", $date_created),
|
||||
WorkTable::DATE_MONTH ->value => date("n", $date_created),
|
||||
WorkTable::DATE_DAY->value => date("j", $date_created)
|
||||
];
|
||||
}
|
||||
|
||||
private function get_entity_by_id(string $id): Response {
|
||||
return (new Call(Endpoints::WORK->value))->params([
|
||||
WorkTable::ID->value => $id
|
||||
])->get();
|
||||
}
|
||||
|
||||
public function main(): Response {
|
||||
// Use copy of request body as entity
|
||||
$entity = $_POST;
|
||||
|
||||
// Generate URL slug from title text or UUID if undefined
|
||||
$entity[WorkTable::ID->value] = $_POST[WorkTable::TITLE->value]
|
||||
? self::gen_slug($_POST[WorkTable::TITLE->value])
|
||||
: parent::gen_uuid4();
|
||||
|
||||
// Bail out here if a work entry with id had been created already
|
||||
if ($this->get_entity_by_id($entity[WorkTable::ID->value])->ok) {
|
||||
return new Response("An entity with id '{$slug}' already exist", 409);
|
||||
}
|
||||
|
||||
// Generate the necessary date fields
|
||||
array_merge($entity, self::gen_date_created());
|
||||
|
||||
// Let's try to insert the new entity
|
||||
if (!$this->db->for(WorkTable::NAME)->insert($entity)) {
|
||||
return new Response("Failed to insert work entry", 500);
|
||||
}
|
||||
|
||||
// Generate permalink for new entity
|
||||
return (new Call(Endpoints::WORK_PERMALINKS->value))->post([
|
||||
PermalinksTable::ID => $entity[WorkTable::ID->value],
|
||||
PermalinksTable::REF_WORK_ID => $entity[WorkTable::ID->value],
|
||||
PermalinksTable::DATE_CREATED => time()
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Reflect\{Response, Path};
|
||||
use ReflectRules\{Ruleset, Rules, Type};
|
||||
|
||||
use const VLW\API\RESP_DELETE_OK;
|
||||
use VLW\Database\Database;
|
||||
use VLW\Database\Tables\Work\ActionsTable;
|
||||
|
||||
require_once Path::root("src/Database/Database.php");
|
||||
require_once Path::root("src/Database/Tables/WorkActions.php");
|
||||
|
||||
class DELETE_WorkActions extends Database {
|
||||
protected Ruleset $ruleset;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$this->ruleset = new Ruleset(strict: true);
|
||||
|
||||
$this->ruleset->POST([
|
||||
(new Rules(ActionsTable::REF_WORK_ID->value))
|
||||
->min(1)
|
||||
->max(parent::SIZE_VARCHAR)
|
||||
]);
|
||||
}
|
||||
|
||||
public function main(): Response {
|
||||
return $this->db->for(ActionsTable::NAME)->delete($_POST) === true
|
||||
? new Response(RESP_DELETE_OK)
|
||||
: new Response("Failed to delete action for work entity", 500);
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
|
||||
use Reflect\{Response, Path};
|
||||
use ReflectRules\{Ruleset, Rules, Type};
|
||||
|
||||
use const VLW\API\RESP_DELETE_OK;
|
||||
use VLW\Database\Database;
|
||||
use VLW\Database\Tables\Work\TagsTable;
|
||||
|
||||
require_once Path::root("src/Database/Database.php");
|
||||
require_once Path::root("src/Database/Tables/Work/WorkTags.php");
|
||||
|
||||
class DELETE_WorkTags extends Database {
|
||||
private Ruleset $ruleset;
|
||||
|
||||
public function __construct() {
|
||||
$this->ruleset = new Ruleset(strict: true);
|
||||
|
||||
$this->ruleset->GET([
|
||||
(new Rules(TagsTable::REF_WORK_ID->value))
|
||||
->min(1)
|
||||
->max(parent::SIZE_VARCHAR),
|
||||
|
||||
(new Rules(TagsTable::NAME->value))
|
||||
->type(Type::ENUM, TagsNameEnum::names())
|
||||
]);
|
||||
|
||||
$this->ruleset->validate_or_exit();
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function main(): Response {
|
||||
return $this->db->for(TagsTable::NAME)->delete($_POST) === true
|
||||
? new Response(RESP_DELETE_OK)
|
||||
: new Response("Failed to delete value from document", 500);
|
||||
}
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
<?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,
|
||||
TagsTable,
|
||||
TagsNameEnum
|
||||
};
|
||||
|
||||
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/WorkTags.php");
|
||||
|
||||
class POST_WorkTags extends Database {
|
||||
protected Ruleset $ruleset;
|
||||
|
||||
public function __construct() {
|
||||
$this->ruleset = new Ruleset(strict: true);
|
||||
|
||||
$this->ruleset->POST([
|
||||
(new Rules(TagsTable::REF_WORK_ID->value))
|
||||
->required()
|
||||
->min(1)
|
||||
->max(parent::SIZE_VARCHAR),
|
||||
|
||||
(new Rules(TagsTable::NAME->value))
|
||||
->required()
|
||||
->type(Type::ENUM, TagsNameEnum::names())
|
||||
]);
|
||||
|
||||
$this->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(TagsTable::NAME)->insert($_POST) === true
|
||||
? new Response($_POST[TagsTable::REF_WORK_ID->value], 201)
|
||||
: new Response("Failed to add tag to work entity", 500);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue