Skip to content

Commit 0d8f586

Browse files
committed
Send correct HTTP status codes for each user endpoint
1 parent ad7d009 commit 0d8f586

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/Service/User.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ public function create(mixed $data): array|object
3131

3232

3333
if (UserDal::create($userEntity) === false) {
34-
// Set an internal error when we cannot add an entry to the database
34+
// Set an internal error 500 when we cannot add an entry to the database
3535
Http::setHeadersByCode(StatusCode::INTERNAL_SERVER_ERROR);
3636

3737
// Set to empty result, because an issue happened. The client has to handle this properly
3838
$data = [];
3939
}
4040

41+
// Send a 201 when the user has been successfully added to DB
42+
Http::setHeadersByCode(StatusCode::CREATED);
43+
4144
return $data; // return statement exists the function and doesn't go beyond this scope
4245
}
4346

@@ -64,6 +67,9 @@ public function update(mixed $postBody): array|object
6467
}
6568

6669
if (UserDal::update($userUuid, $userEntity) === false) {
70+
// Set an internal error 500 when we cannot add an entry to the database
71+
Http::setHeadersByCode(StatusCode::INTERNAL_SERVER_ERROR);
72+
6773
// If invalid or got an error, give back an empty response
6874
return [];
6975
}
@@ -101,10 +107,16 @@ public function retrieve(string $userUuid): array
101107
throw new InvalidValidationException("Invalid user UUID");
102108
}
103109

104-
public function remove(object $data): bool
110+
/**
111+
* @internal Set `mixed` type, because if we get an incorrect payload with syntax errors, `json_decode` gives NULL,
112+
* and `object` wouldn't be a valid datatype here.
113+
*/
114+
public function remove(mixed $data): bool
105115
{
106116
$userValidation = new UserValidation($data);
107117
if ($userValidation->isRemoveSchemaValid()) {
118+
// Send a 204 if the user got removed
119+
//Http::setHeadersByCode(StatusCode::NO_CONTENT);
108120
return UserDal::remove($data->userUuid);
109121
}
110122

src/routes/user.routes.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
enum UserAction: string
1111
{
1212
case CREATE = 'create';
13-
case RETRIEVE_ALL = 'retrieveAll';
13+
case RETRIEVE_ALL = 'retrieveall';
1414
case RETRIEVE = 'retrieve';
1515
case REMOVE = 'remove';
1616
case UPDATE = 'update';
@@ -26,11 +26,11 @@ public function getResponse(): string
2626
$user = new User();
2727
try {
2828
$response = match ($this) {
29-
self::CREATE => $user->create($postBody), // TODO Add send 201
30-
self::RETRIEVE_ALL => $user->retrieveAll(), // TODO send 200
31-
self::RETRIEVE => $user->retrieve($userId), // TODO send 200
32-
self::REMOVE => $user->remove($postBody), // TODO send 204 status code
33-
self::UPDATE => $user->update($postBody), // TODO send 200
29+
self::CREATE => $user->create($postBody),
30+
self::RETRIEVE_ALL => $user->retrieveAll(),
31+
self::RETRIEVE => $user->retrieve($userId),
32+
self::REMOVE => $user->remove($postBody),
33+
self::UPDATE => $user->update($postBody),
3434
};
3535
} catch (InvalidValidationException $e) {
3636
// Send 400 http status code

0 commit comments

Comments
 (0)