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

91 lines
2.3 KiB
PHP

<?php
namespace Api\User;
use Exception;
use Khofmann\Models\User\User as MUser;
use Khofmann\Input\Input;
use Khofmann\Response\Response;
use Khofmann\Api\Api;
use Khofmann\Request\Request;
class User extends Api
{
public function get($id)
{
try {
return Response::json(MUser::getByID($id));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
throw new Exception("User not found", 404);
default:
throw $err;
}
}
}
public function patch($id)
{
$username = Input::patch("username");
$password = Input::patch("password");
$image = Input::file("image");
try {
return Response::json(MUser::getByID($id)->update($username, $password, $image));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
throw new Exception("User not found", 404);
case "FailedUsername":
throw new Exception("Failed to update username", 500);
case "FailedPassword":
throw new Exception("Failed to update password", 500);
case "FailedImage":
throw new Exception("Failed to update image", 500);
default:
throw $err;
}
}
}
public function patchSelf()
{
$token = Request::header("token");
$username = Input::patch("username");
$password = Input::patch("password");
$image = Input::file("image");
try {
return Response::json(MUser::getByToken($token)->update($username, $password, $image));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
throw new Exception("User not found", 404);
case "FailedUsername":
throw new Exception("Failed to update username", 500);
case "FailedPassword":
throw new Exception("Failed to update password", 500);
case "FailedImage":
throw new Exception("Failed to update image", 500);
default:
throw $err;
}
}
}
public function delete($id)
{
try {
return Response::json(MUser::getByID($id)->delete());
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
throw new Exception("User not found", 404);
default:
throw $err;
}
}
}
}