ringfinger/backend/classes/controller/UserPostController.php

54 lines
1.3 KiB
PHP
Raw Normal View History

2020-08-19 23:57:00 +02:00
<?php
declare(strict_types=1);
final class UserPostController extends AbstractController
{
2020-08-23 12:37:39 +02:00
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());
}
}
2020-08-19 23:57:00 +02:00
}