Endpoint for user creation added

This commit is contained in:
Mal 2020-08-19 23:57:00 +02:00
parent 3a1a026e2f
commit 0a8c71b735
1 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
final class UserPostController extends AbstractController
{
protected string $route = '/api/v1/user';
protected array $mandatoryAttributes = [
'username',
'password',
'email',
'jabberAddress',
];
public function handle(): void
{
parent::handle();
if ($this->response->getStatus() !== ServerStatus::OK) {
return;
}
$json = json_decode($this->requestBody);
try {
$user = new User();
$user->setUsername($json->username);
$user->setPassword(Password::GetHash($json->password));
$user->setEmail($json->email);
$user->setJabberAddress($json->jabberAddress);
$user->Save();
$this->response = new ApiJsonResponse();
$this->response->setParameter('userId', $user->getUserId());
} catch (DatabaseException $e) {
$this->response = new ApiJsonResponse();
$this->response->setParameter('success', false);
$this->response->setMessage($e->getMessage());
switch ($e->getCode()) {
case DatabaseException::CONNECTION_FAILED:
$this->response->setStatus(ServerStatus::INTERNAL_ERROR);
break;
default:
$this->response->setStatus(ServerStatus::INTERNAL_ERROR);
}
} catch (Throwable $e) {
$this->response = new ApiJsonResponse(ServerStatus::BAD_REQUEST);
$this->response->setParameter('success', false);
$this->response->setMessage($e->getMessage());
}
}
}