diff --git a/endpoints/work/DELETE.php b/endpoints/work/DELETE.php deleted file mode 100644 index 39ba963..0000000 --- a/endpoints/work/DELETE.php +++ /dev/null @@ -1,60 +0,0 @@ -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); - } - } \ No newline at end of file diff --git a/endpoints/work/PATCH.php b/endpoints/work/PATCH.php deleted file mode 100644 index 1ccaaf0..0000000 --- a/endpoints/work/PATCH.php +++ /dev/null @@ -1,109 +0,0 @@ -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); - } - } \ No newline at end of file diff --git a/endpoints/work/POST.php b/endpoints/work/POST.php deleted file mode 100644 index 1890b48..0000000 --- a/endpoints/work/POST.php +++ /dev/null @@ -1,106 +0,0 @@ -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() - ]); - } - } \ No newline at end of file diff --git a/endpoints/work/actions/DELETE.php b/endpoints/work/actions/DELETE.php deleted file mode 100644 index ea93a30..0000000 --- a/endpoints/work/actions/DELETE.php +++ /dev/null @@ -1,32 +0,0 @@ -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); - } - } \ No newline at end of file diff --git a/endpoints/work/actions/POST.php b/endpoints/work/actions/POST.php deleted file mode 100644 index 1e023fd..0000000 --- a/endpoints/work/actions/POST.php +++ /dev/null @@ -1,72 +0,0 @@ -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); - } - } \ No newline at end of file diff --git a/endpoints/work/tags/DELETE.php b/endpoints/work/tags/DELETE.php deleted file mode 100644 index b5fbed0..0000000 --- a/endpoints/work/tags/DELETE.php +++ /dev/null @@ -1,38 +0,0 @@ -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); - } - } \ No newline at end of file diff --git a/endpoints/work/tags/POST.php b/endpoints/work/tags/POST.php deleted file mode 100644 index 1396e2e..0000000 --- a/endpoints/work/tags/POST.php +++ /dev/null @@ -1,59 +0,0 @@ -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); - } - } \ No newline at end of file