Skip to content

Commit 2ca20e5

Browse files
committed
Enhance API results and HTTP status/methods
1 parent 5a22c02 commit 2ca20e5

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/Dal/UserDal.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class UserDal
1010
{
1111
public const TABLE_NAME = 'users';
1212

13-
public static function create(UserEntity $userEntity): int|string|false
13+
public static function create(UserEntity $userEntity): string|false
1414
{
1515
$userBean = R::dispense(self::TABLE_NAME);
1616
$userBean->user_uuid = $userEntity->getUserUuid();
@@ -29,7 +29,10 @@ public static function create(UserEntity $userEntity): int|string|false
2929
R::close();
3030
}
3131

32-
return $redBeanIncrementId;
32+
$userBean = R::load(self::TABLE_NAME, $redBeanIncrementId);
33+
34+
// Return user UUID (UUID is a string datatype)
35+
return $userBean->user_uuid;
3336
}
3437

3538
public static function update(string $userUuid, UserEntity $userEntity): int|string|false
@@ -64,6 +67,7 @@ public static function update(string $userUuid, UserEntity $userEntity): int|str
6467
}
6568
}
6669

70+
// Return false when the requested user isn't found
6771
return false;
6872
}
6973

src/Route/user.routes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function getResponse(): string
3535
$user = new User($jwtToken);
3636

3737
try {
38-
// check first if HTTP method for the requested endpoint is valid
38+
// first, let's check if HTTP method for the requested endpoint is valid
3939
$expectHttpMethod = match ($this) {
4040
self::LOGIN => Http::POST_METHOD,
4141
self::CREATE => Http::POST_METHOD,
42-
self::UPDATE => Http::POST_METHOD,
42+
self::UPDATE => Http::PUT_METHOD,
4343
self::RETRIEVE_ALL => Http::GET_METHOD,
4444
self::RETRIEVE => Http::GET_METHOD,
4545
self::REMOVE => Http::DELETE_METHOD

src/Service/User.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ public function create(mixed $data): array|object
8383
);
8484
}
8585

86-
if (UserDal::create($userEntity) === false) {
87-
// Set an internal error 500 when we cannot add an entry to the database
88-
HttpResponse::setHeadersByCode(StatusCode::INTERNAL_SERVER_ERROR);
86+
if (!$userUuid = UserDal::create($userEntity)) {
87+
// If we receive an error while creating a user to the database, give a 400 to client
88+
HttpResponse::setHeadersByCode(StatusCode::BAD_REQUEST);
8989

9090
// Set to empty result, because an issue happened. The client has to handle this properly
9191
$data = [];
@@ -94,7 +94,10 @@ public function create(mixed $data): array|object
9494
// Send a 201 when the user has been successfully added to DB
9595
HttpResponse::setHeadersByCode(StatusCode::CREATED);
9696

97-
return $data; // return statement exists the function and doesn't go beyond this scope
97+
// Add user UUID to the object to give back the user's UUID to the client
98+
$data->userUuid = $userUuid;
99+
100+
return $data;
98101
}
99102

100103
throw new InvalidValidationException("Invalid user payload");
@@ -120,8 +123,8 @@ public function update(mixed $postBody): array|object
120123
}
121124

122125
if (UserDal::update($userUuid, $userEntity) === false) {
123-
// Set an internal error 500 when we cannot add an entry to the database
124-
HttpResponse::setHeadersByCode(StatusCode::INTERNAL_SERVER_ERROR);
126+
// Most likely, the user isn't found, set a 404 to the client
127+
HttpResponse::setHeadersByCode(StatusCode::NOT_FOUND);
125128

126129
// If invalid or got an error, give back an empty response
127130
return [];

0 commit comments

Comments
 (0)