More Facades, User delete

This commit is contained in:
2024-07-22 00:56:04 +02:00
parent 4b89a7e9ca
commit 7b897071f5
14 changed files with 177 additions and 63 deletions
+5 -3
View File
@@ -3,9 +3,12 @@
namespace Api\Login;
use Exception;
use Khofmann\Api\Api;
use Khofmann\Input\Input;
use Khofmann\Response\Response;
use Khofmann\Models\User\User;
class Login
class Login extends Api
{
public function post()
{
@@ -15,8 +18,7 @@ class Login
if (empty($password)) throw new Exception("Missing Password", 400);
try {
$response = \Khofmann\Models\User\User::logIn($email, $password);
return json_encode($response);
return Response::json(User::logIn($email, $password));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "Failed":
+6 -3
View File
@@ -2,13 +2,16 @@
namespace Api\Logout;
use Khofmann\Api\Api;
use \Khofmann\Models\User\User;
use Khofmann\Request\Request;
use Khofmann\Response\Response;
class Logout
class Logout extends Api
{
public function post()
{
$token = request()->getHeader("token");
return json_decode(User::getByToken($token)->logOut());
$token = Request::header("token");
return Response::json(User::getByToken($token)->logOut());
}
}
+28 -11
View File
@@ -5,13 +5,16 @@ 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
class User extends Api
{
public function get($id)
{
try {
return json_encode(MUser::getByID($id));
return Response::json(MUser::getByID($id));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
@@ -22,14 +25,14 @@ class User
}
}
public function post($id)
public function patch($id)
{
$username = Input::post("username");
$password = Input::post("password");
$username = Input::patch("username");
$password = Input::patch("password");
$image = Input::file("image");
try {
return json_encode(MUser::getByID($id)->update($username, $password, $image));
return Response::json(MUser::getByID($id)->update($username, $password, $image));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
@@ -46,15 +49,15 @@ class User
}
}
public function postSelf()
public function patchSelf()
{
$token = Input::header("token");
$username = Input::post("username");
$password = Input::post("password");
$token = Request::header("token");
$username = Input::patch("username");
$password = Input::patch("password");
$image = Input::file("image");
try {
return json_encode(MUser::getByToken($token)->update($username, $password, $image));
return Response::json(MUser::getByToken($token)->update($username, $password, $image));
} catch (Exception $err) {
switch ($err->getMessage()) {
case "NotFound":
@@ -70,4 +73,18 @@ class User
}
}
}
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;
}
}
}
}
+37 -6
View File
@@ -126,12 +126,12 @@ paths:
value: { "message": "User not Found" }
tags:
- User
post:
patch:
summary: Update user
description:
Update user with ID. Fields are updated in order username, password, image. If one fails, subsequent are not updated. <br>
Use special ID <code>self</code> to update logged in user. <br>
Requires logged in user to have admin permissions for any ID other than <code>self</code>. <br>
Requires logged in user to have admin permissions for any ID other than <code>self</code>.
security:
- BasicAuth: []
parameters:
@@ -177,17 +177,48 @@ paths:
value: { "message": "Failed to update username" }
tags:
- User
delete:
summary: Delete user
description: Delete user with ID. <br>
Requires logged in user to have admin permissions.
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/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" }
tags:
- User
externalDocs:
url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/docs/
security: []
servers:
- url: https://khofmann.userpage.fu-berlin.de/phpCourse/exam/api/
description: ""
variables: {}
components:
links: {}
callbacks: {}
schemas:
BooleanResponse:
type: boolean
File diff suppressed because one or more lines are too long