ringfinger/backend/classes/controller/UserPostController.php
2020-08-19 23:57:00 +02:00

54 lines
1.6 KiB
PHP

<?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());
}
}
}