PHP-Course/exam/api/Register/Register.php
2024-07-22 02:21:30 +02:00

34 lines
869 B
PHP

<?php
namespace Api\Register;
use Exception;
use Khofmann\Api\Api;
use Khofmann\Input\Input;
use Khofmann\Response\Response;
use Khofmann\Models\User\User;
class Register extends Api
{
public function post()
{
$username = Input::post("username");
if (empty($username)) throw new Exception("Missing username", 400);
$email = Input::post("email");
if (empty($email)) throw new Exception("Missing email", 400);
$password = Input::post("password");
if (empty($password)) throw new Exception("Missing password", 400);
try {
return Response::json(User::create($username, $email, $password));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "Duplicate":
throw new Exception("A user with this username or email already exists", 400);
default:
throw $err;
}
}
}
}