Image now a file

This commit is contained in:
2024-07-21 18:28:19 +02:00
parent 8d91e805dd
commit b3c5841e36
17 changed files with 346 additions and 36 deletions
+8 -1
View File
@@ -9,8 +9,13 @@ class Login
{
public function post()
{
$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 {
$response = \Khofmann\Models\User\User::logIn(Input::post("email"), Input::post("password"));
$response = \Khofmann\Models\User\User::logIn($email, $password);
return json_encode($response);
} catch (Exception $err) {
switch ($err->getMessage()) {
@@ -20,6 +25,8 @@ class Login
throw new Exception("User not Found", 404);
case "Invalid":
throw new Exception("Invalid Username or Password", 401);
default:
throw $err;
}
}
}
+1 -1
View File
@@ -9,6 +9,6 @@ class Logout
public function post()
{
$token = request()->getHeader("token");
return json_decode(User::getByToken($token)->logOut($token));
return json_decode(User::getByToken($token)->logOut());
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
namespace Api\User;
use Exception;
use Khofmann\Models\User\User as MUser;
use Khofmann\Input\Input;
class User
{
public function get($id)
{
try {
return json_encode(MUser::getByID($id));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
throw new Exception("User not Found", 404);
default:
throw $err;
}
}
}
public function post($id)
{
$username = Input::post("username");
$password = Input::post("password");
$image = Input::file("image");
try {
return json_encode(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;
}
}
}
}
+126 -2
View File
@@ -27,6 +27,15 @@ paths:
examples:
Success:
value: true
400:
description: Missing Fields
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
examples:
Missing Fields:
value: { "message": "Missing email" }
401:
description: Invalid credentials
content:
@@ -74,6 +83,97 @@ paths:
value: true
tags:
- Login/Logout
/user{id}:
get:
summary: Get user
description: Get user by ID
security:
- BasicAuth: []
parameters:
- name: id
in: path
description: User ID
required: true
schema:
type: integer
format: int14
responses:
200:
description: Success
content:
application/json:
schema:
$ref: "#/components/schemas/UserResponse"
examples:
Success:
value:
{
"id": 1,
"username": "Admin",
"status": 1,
"email": "marvin@zedat.fu-berlin.de",
"image": "profilbilder\\/admin.svg",
"isAdmin": true,
}
404:
description: User not Found
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
examples:
User not Found:
value: { "message": "User not Found" }
tags:
- User
post:
summary: Update user
description: Update user with ID. Fields are updated in order username,password,image. If one fails, subsequent are not updated
security:
- BasicAuth: []
parameters:
- name: id
in: path
description: User ID
required: true
schema:
type: integer
format: int14
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/UserUpdateRequest"
responses:
200:
description: Success
content:
application/json:
schema:
$ref: "#/components/schemas/BooleanResponse"
examples:
Success:
value: true
404:
description: User not Found
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
examples:
User not Found:
value: { "message": "User not Found" }
500:
description: Update failed
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorResponse"
examples:
User not Found:
value: { "message": "Failed to update username" }
tags:
- User
externalDocs:
url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/docs/
@@ -103,6 +203,31 @@ components:
type: string
password:
type: string
UserResponse:
type: object
properties:
id:
type: number
username:
type: string
status:
type: number
email:
type: string
image:
type: string
nullable: true
isAdmin:
type: boolean
UserUpdateRequest:
type: object
properties:
username:
type: string
password:
type: string
image:
type: string
securitySchemes:
BasicAuth:
type: apiKey
@@ -110,5 +235,4 @@ components:
in: header
tags:
- name: Login/Logout
- name: Users
- name: Posts
- name: User
File diff suppressed because one or more lines are too long