Skip to content

Commit b0956b7

Browse files
committed
Retrieve, Retrieve All and Remove user from database
1 parent 04131ca commit b0956b7

File tree

4 files changed

+63
-28
lines changed

4 files changed

+63
-28
lines changed

src/Dal/UserDal.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,28 @@ public static function create(UserEntity $userEntity): int|string
2828

2929
return $id;
3030
}
31+
32+
public static function get(string $userUuid): ?array
33+
{
34+
$bindings = ['userUuid' => $userUuid];
35+
$userBean = R::findOne(self::TABLE_NAME, 'user_uuid = :userUuid ', $bindings);
36+
37+
return $userBean?->export();
38+
}
39+
40+
public static function getAll(): array
41+
{
42+
return R::findAll(self::TABLE_NAME);
43+
}
44+
45+
public static function remove(string $userUuid): bool
46+
{
47+
$userBean = R::findOne(self::TABLE_NAME, 'user_uuid = :userUuid', ['userUuid' => $userUuid]);
48+
49+
if ($userBean) {
50+
return (bool)R::trash($userBean);
51+
}
52+
53+
return false;
54+
}
3155
}

src/Service/User.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class User
1717

1818
public function create(mixed $data): object
1919
{
20-
// validate data
2120
$userValidation = new UserValidation($data);
2221
if ($userValidation->isCreationSchemaValid()) {
2322
$userUuid = Uuid::uuid4(); // assigning a UUID to the user
@@ -49,23 +48,33 @@ public function create(mixed $data): object
4948

5049
public function retrieveAll(): array
5150
{
52-
return [];
51+
$users = UserDal::getAll();
52+
53+
return array_map(function (object $user): object {
54+
// Remove unnecessary "id" field
55+
unset($user['id']);
56+
return $user;
57+
}, $users);
5358
}
5459

55-
public function retrieve(string $userId): self
60+
public function retrieve(string $userUuid): array
5661
{
57-
if (v::uuid()->validate($userId)) {
58-
// TODO To be implemented
62+
if (v::uuid()->validate($userUuid)) {
63+
if ($user = UserDal::get($userUuid)) {
64+
// Removing fields we don't want to expose
65+
unset($user['id']);
66+
67+
return $user;
68+
}
5969

60-
return $this;
70+
return [];
6171
}
6272

6373
throw new InvalidValidationException("Invalid user UUID");
6474
}
6575

6676
public function update(mixed $postBody): object
6777
{
68-
// validation schema
6978
$userValidation = new UserValidation($postBody);
7079
if ($userValidation->isUpdateSchemaValid()) {
7180
return $postBody;
@@ -74,16 +83,13 @@ public function update(mixed $postBody): object
7483
throw new InvalidValidationException("Invalid user payload");
7584
}
7685

77-
public function remove(string $userId): bool
86+
public function remove(object $data): bool
7887
{
79-
if (v::uuid()->validate($userId)) {
80-
// TODO To be implemented
81-
} else {
82-
throw new InvalidValidationException("Invalid user UUID");
88+
$userValidation = new UserValidation($data);
89+
if ($userValidation->isRemoveSchemaValid()) {
90+
return UserDal::remove($data->userUuid);
8391
}
8492

85-
86-
// TODO Lookup the the DB user row with this userId
87-
return true; // default value
93+
throw new InvalidValidationException("Invalid user UUID");
8894
}
8995
}

src/Validation/UserValidation.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ class UserValidation
1010
private const MINIMUM_NAME_LENGTH = 2;
1111
private const MAXIMUM_NAME_LENGTH = 40;
1212

13-
public function __construct(private mixed $data) {}
13+
public function __construct(private readonly mixed $data) {}
1414

1515
public function isCreationSchemaValid(): bool
1616
{
17-
// validation schema
1817
$schemaValidation =
1918
v::attribute('first', v::stringType()->length(self::MINIMUM_NAME_LENGTH, self::MAXIMUM_NAME_LENGTH))
2019
->attribute('last', v::stringType()->length(self::MINIMUM_NAME_LENGTH, self::MAXIMUM_NAME_LENGTH))
@@ -24,6 +23,11 @@ public function isCreationSchemaValid(): bool
2423
return $schemaValidation->validate($this->data);
2524
}
2625

26+
public function isRemoveSchemaValid(): bool
27+
{
28+
return v::attribute('userUuid', v::uuid())->validate($this->data);
29+
}
30+
2731
public function isUpdateSchemaValid(): bool
2832
{
2933
// same schema for both creation and update

src/routes/user.routes.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ public function getResponse(): string
2727
try {
2828
$response = match ($this) {
2929
self::CREATE => $user->create($postBody),
30-
self::RETRIEVE_ALL => $user->retrieveAll(),
31-
self::RETRIEVE => $user->retrieve($userId),
32-
self::REMOVE => $user->remove($userId),
33-
self::UPDATE => $user->update($postBody),
34-
};
35-
} catch (InvalidValidationException $e) {
36-
// Send 400 http status code
37-
Http::setHeadersByCode(StatusCode::BAD_REQUEST);
30+
self::RETRIEVE_ALL => $user->retrieveAll(),
31+
self::RETRIEVE => $user->retrieve($userId),
32+
self::REMOVE => $user->remove($postBody),
33+
self::UPDATE => $user->update($postBody),
34+
};
35+
} catch (InvalidValidationException $e) {
36+
// Send 400 http status code
37+
Http::setHeadersByCode(StatusCode::BAD_REQUEST);
3838

39-
$response = [
40-
'errors' => [
41-
'message' => $e->getMessage(),
39+
$response = [
40+
'errors' => [
41+
'message' => $e->getMessage(),
4242
'code' => $e->getCode()
4343
]
4444
];
@@ -55,6 +55,7 @@ public function getResponse(): string
5555
$userAction = match ($action) {
5656
'create' => UserAction::CREATE, // send 201
5757
'retrieve' => UserAction::RETRIEVE, // send 200
58+
'retrieveall' => UserAction::RETRIEVE_ALL,
5859
'remove' => UserAction::REMOVE, // send 204 status code
5960
'update' => UserAction::UPDATE, //
6061
default => UserAction::RETRIEVE_ALL, // send 200

0 commit comments

Comments
 (0)