refactored handleAllDomainsGetRequest

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-02-06 17:58:52 +01:00
parent 0241afaa25
commit 4fb5cdcffd
1 changed files with 112 additions and 82 deletions

View File

@ -4,12 +4,11 @@ namespace App\Controller;
error_reporting(error_level: E_ALL); error_reporting(error_level: E_ALL);
use App\Entity\Domain;
use App\Repository\ApikeyRepository; use App\Repository\ApikeyRepository;
use App\Repository\DomainRepository; use App\Repository\DomainRepository;
use DI\Container; use DI\Container;
use DI\ContainerBuilder; use DI\ContainerBuilder;
use OpenApi\Annotations\ServerVariable;
use OpenApi\Annotations\Tag;
use OpenApi\Generator; use OpenApi\Generator;
use UnhandledMatchError; use UnhandledMatchError;
use function DI\autowire; use function DI\autowire;
@ -18,7 +17,32 @@ use OpenApi\Attributes as OAT;
/** /**
* *
*/ */
#[OAT\Info(version: '0.0.1', title: 'bindAPI' )] #[OAT\Info(version: '0.0.1', title: 'bindAPI')]
#[OAT\Server(
url: "{schema}://{hostname}/api",
description: "The bindAPI URL.",
variables: [
new OAT\ServerVariable(
serverVariable: "schema",
default: "https",
enum: ["https", "http"]
),
new OAT\ServerVariable(
serverVariable: "hostname",
default: "ns2.24unix.net",
)
]
)]
#[OAT\Tag(
name: "Server"
)]
#[OAT\SecurityScheme(
securityScheme: "Authorization",
type: "apiKey",
description: "description",
name: "X-API-Key",
in: "header"
)]
class RequestController class RequestController
{ {
//private DatabaseConnection $databaseConnection; //private DatabaseConnection $databaseConnection;
@ -51,12 +75,56 @@ class RequestController
$this->apikeyRepository = $this->container->get(name: ApikeyRepository::class); $this->apikeyRepository = $this->container->get(name: ApikeyRepository::class);
} }
/** /**
* @OA\Server( * @return void
* url = "https://ns2.24unix.net/api" */
* ) #[OAT\Get(
path: '/domains',
operationId: 'getAllDomains',
description: 'Returns a list of all domains on this server.',
summary: 'Listing all domains.',
// security: [
// 'Authorization' => [
//
// "read:api"
// ]
// ],
servers: [],
tags: ['Domains'],
responses: [
new OAT\Response(
response: 200,
description: 'OK'
),
new OAT\Response(
response: 401,
description: 'API key is missing or invalid.'
),
new OAT\Response(
response: 404,
description: 'Domain not found.'
)]
)]
public function handleAllDomainsGetRequest(): void
{
$domains = $this->domainRepository->findAll();
$resultDomain = [];
foreach ($domains as $singleDomain) {
$domain = [
'id' => $singleDomain->getId(),
'name' => $singleDomain->getName(),
'panel_id' => $singleDomain->getPanelId(),
'a' => $singleDomain->getA(),
'aaaa' => $singleDomain->getAaaa()
];
$resultDomain[] = $domain;
}
$this->result = $resultDomain;
}
/**
* @OA\Tag(name = "Server") * @OA\Tag(name = "Server")
* @OA\Get( * @OA\Get(
* path = "/ping", * path = "/ping",
@ -70,61 +138,7 @@ class RequestController
* } * }
* ) * )
* *
* @OA\SecurityScheme (name="bindAPISecurity",
* type="apiKey",
* description="description",
* name="X-API-Key",
* in="header",
* securityScheme="Authorization"
*
* )
* @SwaggerDefinition(
* securityDefinition = @SecurityDefinition(
* apiKeyAuthDefinitions = {
* @ApiKeyAuthDefinition(
* key = "X-API-Key", in = ApiKeyAuthDefinition.ApiKeyLocation.HEADER, name = "X-API-KEY"
* )
* }
* )
* )
* @OA\Tag(name = "Domains") * @OA\Tag(name = "Domains")
* @OA\Get(
* path="/domains",
* summary="Listing all domains.",
* description="desc",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Post(
* path="/domains",
* summary="Create a domain.",
* description="Creates a new domain.",
* tags={"Domains"},
* @OA\Response(response="201", description="Created"),
* @OA\Response(response = "400", description = "Invalid request body."),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Get(
* path="/domains/{name}",
* summary="Returns a single domain.",
* description="Returns information of a single domain specified by its domain name.",
* tags={"Domains"},
* @OA\Response(response="200", description="OK"),
* @OA\Response(response = "401", description = "API key is missing or invalid."),
* @OA\Response(response="404", description="Domain not found."),
* security={
* {"Authorization":{"read":"write"}}
* }
* )
* @OA\Put( * @OA\Put(
* path="/domains/{name}", * path="/domains/{name}",
* summary="Updates a domain.", * summary="Updates a domain.",
@ -149,9 +163,35 @@ class RequestController
* {"Authorization":{"read":"write"}} * {"Authorization":{"read":"write"}}
* } * }
* ) * )
*
* @return void * @return void
*/ */
#[OAT\Get(
path: '/domains/{name}',
operationId: 'getSingleDomain',
description: 'Returns information of a single domain specified by its domain name.',
summary: 'Returns a single domain.',
security: [
],
tags: ['Domains'],
parameters: [
new OAT\Parameter(name: 'name', in: 'path', required: true, schema: new OAT\Schema(type: 'string')),
],
responses: [
new OAT\Response(
response: 200,
description: 'OK'
),
new OAT\Response(
response: 401,
description: 'API key is missing or invalid.'
),
new OAT\Response(
response: 404,
description: 'Domain not found.'
)]
)]
public function processRequest() public function processRequest()
{ {
$command = $this->uri[2]; $command = $this->uri[2];
@ -253,19 +293,7 @@ class RequestController
public function handleDomainGetRequest(): void public function handleDomainGetRequest(): void
{ {
if (empty($this->uri[3])) { if (empty($this->uri[3])) {
$domains = $this->domainRepository->findAll(); $this->handleAllDomainsGetRequest();
$resultDomain = [];
foreach ($domains as $singleDomain) {
$domain = [
'id' => $singleDomain->getId(),
'name' => $singleDomain->getName(),
'panel_id' => $singleDomain->getPanelId(),
'a' => $singleDomain->getA(),
'aaaa' => $singleDomain->getAaaa()
];
$resultDomain[] = $domain;
}
$this->result = $resultDomain;
} else { } else {
if ($result = $this->domainRepository->findByName(name: $this->uri[3])) { if ($result = $this->domainRepository->findByName(name: $this->uri[3])) {
$domain = [ $domain = [
@ -288,8 +316,7 @@ class RequestController
/** /**
* @return void * @return void
*/ */
public public function handleDomainPostRequest(): void
function handleDomainPostRequest(): void
{ {
$name = $_POST['name'] ?? ''; $name = $_POST['name'] ?? '';
$panelID = intval(value: $_POST['panel_id'] ?? 0); $panelID = intval(value: $_POST['panel_id'] ?? 0);
@ -310,7 +337,8 @@ class RequestController
$this->status = "400 Bad request"; $this->status = "400 Bad request";
$this->message = "Domain: $name already exists."; $this->message = "Domain: $name already exists.";
} else { } else {
$result = $this->domainRepository->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); $domain = new Domain(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa);
$result = $this->domainRepository->insert(domain: $domain);
$this->status = "201 Created"; $this->status = "201 Created";
$this->message = $result; $this->message = $result;
} }
@ -334,7 +362,7 @@ class RequestController
$put[$key] = $value; $put[$key] = $value;
} }
$id = $put['id'] ?? 0; $id = $put['id'] ?? 0;
$name = $put['name'] ?? ""; $name = $put['name'] ?? '';
$panelID = $put['panel_id'] ?? ""; $panelID = $put['panel_id'] ?? "";
$a = $put['a'] ?? ""; $a = $put['a'] ?? "";
$aaaa = $put['aaaa'] ?? ""; $aaaa = $put['aaaa'] ?? "";
@ -356,7 +384,8 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "At least one IP address is required."; $this->message = "At least one IP address is required.";
} else { } else {
$this->domainRepository->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa); $domain = new Domain(name: $panelID, id: $id, panelID: $name, a: $a, aaaa: $aaaa);
$this->domainRepository->update(domain: $domain);
$this->header = "201 Updated"; $this->header = "201 Updated";
$this->status = "201 Updated"; $this->status = "201 Updated";
$this->message = "201 Updated"; $this->message = "201 Updated";
@ -389,12 +418,13 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "You need to supply an ID."; $this->message = "You need to supply an ID.";
} else { } else {
if (!$this->domainRepository->findByID(id: $id)) {
if (!$domain = $this->domainRepository->findByID(id: $id)) {
$this->header = "400 Bad Request"; $this->header = "400 Bad Request";
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id."; $this->message = "There is no domain with ID $id.";
} else { } else {
$this->domainRepository->delete(id: $id); $this->domainRepository->delete(domain: $domain);
$this->header = "204 No content."; $this->header = "204 No content.";
$this->status = "204 No content."; $this->status = "204 No content.";
$this->message = "The domain $id has been deleted."; $this->message = "The domain $id has been deleted.";