Image Endpoint

This commit is contained in:
2024-07-27 20:43:57 +02:00
parent 5ebdee09d8
commit 42529a66d4
6 changed files with 207 additions and 22 deletions
+57
View File
@@ -0,0 +1,57 @@
<?php
namespace Api\Users\Image;
use Exception;
use Khofmann\Api\Api;
use Khofmann\Input\Input;
use Khofmann\Models\User\User;
use Khofmann\Response\Response;
use Khofmann\ApiError\ApiError;
use Khofmann\Request\Request;
class Image extends Api
{
public function post($id): void
{
// Fetch all inputs.
$image = Input::file("image");
$predefined = Input::post("predefined");
// Try and update user image.
// Throw errors according to situation.
try {
Response::json(User::getByID($id)->updateImage($image, $predefined));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
throw ApiError::notFound("user");
default:
// Due to how the failed field is handled, it's ApiError is inside the models update
throw $err;
}
}
}
public function postSelf(): void
{
// Fetch all inputs.
$token = Request::token();
$image = Input::file("image");
$predefined = Input::post("predefined");
// Try and update user image.
// Throw errors according to situation.
try {
Response::json(User::getByToken($token)->updateImage($image, $predefined));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
throw ApiError::notFound("user");
default:
// Due to how the failed field is handled, it's ApiError is inside the models update
throw $err;
}
}
}
}