From 42529a66d47f7783ef20d9460a8bd86a3c1e1c99 Mon Sep 17 00:00:00 2001 From: Kilian Hofmann Date: Sat, 27 Jul 2024 20:43:57 +0200 Subject: [PATCH] Image Endpoint --- exam/api/Users/Image/Image.php | 57 ++++++++++++++++++++++++ exam/api/docs/api.yaml | 64 ++++++++++++++++++++++++++- exam/api/docs/index.html | 16 +++++-- exam/classes/Input/Input.php | 2 +- exam/classes/Models/User/User.php | 73 +++++++++++++++++++++++++++++-- exam/routes/routes.php | 17 ++----- 6 files changed, 207 insertions(+), 22 deletions(-) create mode 100644 exam/api/Users/Image/Image.php diff --git a/exam/api/Users/Image/Image.php b/exam/api/Users/Image/Image.php new file mode 100644 index 0000000..de7bd6f --- /dev/null +++ b/exam/api/Users/Image/Image.php @@ -0,0 +1,57 @@ +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; + } + } + } +} diff --git a/exam/api/docs/api.yaml b/exam/api/docs/api.yaml index 571a6a1..c822571 100644 --- a/exam/api/docs/api.yaml +++ b/exam/api/docs/api.yaml @@ -491,7 +491,7 @@ paths: $ref: "#/components/schemas/NotFoundResponse" examples: User not found: - value: { "code": "NotFound", "entity": "username" } + value: { "code": "NotFound", "entity": "user" } 500: description: Update failed. content: @@ -539,6 +539,60 @@ paths: value: { "code": "NotFound", "entity": "user" } tags: - User + /users/{id}/image: + post: + summary: Update user image + description: Update user image with ID.
+ Use special ID self to update logged in user.
+ Requires logged in user to have admin permissions for any ID other than self. + security: + - BasicAuth: [] + - BasicAuth: [isAdmin] + parameters: + - name: id + in: path + description: User ID + required: true + schema: + type: integer + format: int14 + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/UserImageUpdateRequest" + responses: + 200: + description: Success. + content: + application/json: + schema: + $ref: "#/components/schemas/UserResponse" + 404: + description: User not found. + content: + application/json: + schema: + $ref: "#/components/schemas/NotFoundResponse" + examples: + User not found: + value: { "code": "NotFound", "entity": "user" } + 500: + description: Update failed. + content: + application/json: + schema: + $ref: "#/components/schemas/FailedUpdateResponse" + examples: + Failed username: + value: + { + "code": "FailedUpdate", + "fields": ["image", "predefined"], + "reasons": ["string", "string"], + } + tags: + - User externalDocs: url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/docs/ @@ -658,6 +712,14 @@ components: type: string email: type: string + UserImageUpdateRequest: + type: object + properties: + image: + type: string + format: binary + predefined: + type: string RegisterRequest: type: object required: diff --git a/exam/api/docs/index.html b/exam/api/docs/index.html index 0e93c01..165d24a 100644 --- a/exam/api/docs/index.html +++ b/exam/api/docs/index.html @@ -384,7 +384,7 @@ data-styled.g137[id="sc-kvXgyf"]{content:"fBvPoH,"}/*!sc*/ -

Success.

Response samples

Content type
application/json
{
  • "id": 0,
  • "username": "string",
  • "status": 0,
  • "email": "string",
  • "image": "string",
  • "isAdmin": true,
  • "memberSince": {
    },
  • "postCount": 0
}
+

Response samples

Content type
application/json
{
  • "id": 0,
  • "username": "string",
  • "status": 0,
  • "email": "string",
  • "image": "string",
  • "isAdmin": true,
  • "memberSince": {
    },
  • "postCount": 0
}

Update user image

Update user image with ID.
Use special ID self to update logged in user.
Requires logged in user to have admin permissions for any ID other than self.

+
Authorizations:
BasicAuthBasicAuth
path Parameters
id
required
integer <int14>

User ID

+
Request Body schema: application/json
image
string <binary>
predefined
string

Responses

Request samples

Content type
application/json
{
  • "image": "string",
  • "predefined": "string"
}

Response samples

Content type
application/json
{
  • "id": 0,
  • "username": "string",
  • "status": 0,
  • "email": "string",
  • "image": "string",
  • "isAdmin": true,
  • "memberSince": {
    },
  • "postCount": 0
}