Add DeleteEntity class and integrate it into Method for handling entity deletion requests

This commit is contained in:
netkas 2025-06-03 20:50:34 -04:00
parent 14effd7ef8
commit 6e68877a08
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
2 changed files with 54 additions and 1 deletions

View file

@ -8,6 +8,7 @@
use FederationServer\Methods\Attachments\UploadAttachment;
use FederationServer\Methods\Audit\ListAuditLogs;
use FederationServer\Methods\Audit\ViewAuditEntry;
use FederationServer\Methods\Entities\DeleteEntity;
use FederationServer\Methods\Entities\GetEntity;
use FederationServer\Methods\Entities\ListEntities;
use FederationServer\Methods\Entities\PushEntity;
@ -42,6 +43,7 @@
case MANAGE_CLIENT_PERMISSION;
case GET_ENTITY;
case DELETE_ENTITY;
case LIST_ENTITIES;
case QUERY_ENTITY;
case PUSH_ENTITY;
@ -86,6 +88,9 @@
case self::GET_ENTITY:
GetEntity::handleRequest();
break;
case self::DELETE_ENTITY:
DeleteEntity::handleRequest();
break;
case self::PUSH_ENTITY:
PushEntity::handleRequest();
break;
@ -146,8 +151,9 @@
$path === '/entities' && $requestMethod === 'GET' => Method::LIST_ENTITIES,
$path === '/entities' && $requestMethod === 'POST' => Method::PUSH_ENTITY,
preg_match('#^/entities/([a-fA-F0-9\-]{36,})$#', $path) && $requestMethod === 'GET' => Method::GET_ENTITY,
$path === '/entities/query' && $requestMethod === 'POST' => Method::QUERY_ENTITY,
preg_match('#^/entities/([a-fA-F0-9\-]{36,})$#', $path) && $requestMethod === 'GET' => Method::GET_ENTITY,
preg_match('#^/entities/([a-fA-F0-9\-]{36,})$#', $path) && $requestMethod === 'DELETE' => Method::DELETE_ENTITY,
$path === '/operators' && $requestMethod === 'GET' => Method::LIST_OPERATORS,
$path === '/operators' && $requestMethod === 'POST' => Method::CREATE_OPERATOR,

View file

@ -0,0 +1,47 @@
<?php
namespace FederationServer\Methods\Entities;
use FederationServer\Classes\Managers\EntitiesManager;
use FederationServer\Classes\RequestHandler;
use FederationServer\Classes\Validate;
use FederationServer\Exceptions\DatabaseOperationException;
use FederationServer\Exceptions\RequestException;
use FederationServer\FederationServer;
class DeleteEntity extends RequestHandler
{
/**
* @inheritDoc
*/
public static function handleRequest(): void
{
if(!preg_match('#^/entities/([a-fA-F0-9\-]{36,})$#', FederationServer::getPath(), $matches))
{
throw new RequestException('Bad Request: Entity UUID is required', 400);
}
$entityUuid = $matches[1];
if(!$entityUuid || !Validate::uuid($entityUuid))
{
throw new RequestException('Bad Request: Entity UUID is required', 400);
}
try
{
if(!EntitiesManager::entityExistsByUuid($entityUuid))
{
throw new RequestException('Not Found: Entity does not exist', 404);
}
EntitiesManager::deleteEntity($entityUuid);
}
catch (DatabaseOperationException $e)
{
throw new RequestException('Internal Server Error: Unable to delete entity', 500, $e);
}
self::successResponse();
}
}