Skip to content

Commit 72cd8a1

Browse files
committed
Allow to update a user saved in the database
1 parent b0956b7 commit 72cd8a1

File tree

4 files changed

+78
-23
lines changed

4 files changed

+78
-23
lines changed

src/Dal/UserDal.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ public static function create(UserEntity $userEntity): int|string
2929
return $id;
3030
}
3131

32+
public static function update(string $userUuid, UserEntity $userEntity): int|string
33+
{
34+
$userBean = R::findOne(self::TABLE_NAME, 'user_uuid = :userUuid', ['userUuid' => $userUuid]);
35+
36+
// If the user exists, update it
37+
if ($userBean) {
38+
$firstName = $userEntity->getFirstName();
39+
$lastName = $userEntity->getLastName();
40+
$phone = $userEntity->getPhone();
41+
42+
if ($firstName) {
43+
$userBean->firstName = $firstName;
44+
}
45+
46+
if ($lastName) {
47+
$userBean->lastName = $lastName;
48+
}
49+
50+
if ($phone) {
51+
$userBean->phone = $phone;
52+
}
53+
54+
// save the user
55+
return R::store($userBean);
56+
}
57+
58+
return 0;
59+
}
60+
3261
public static function get(string $userUuid): ?array
3362
{
3463
$bindings = ['userUuid' => $userUuid];

src/Entity/User.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ class User
55
{
66
private string $userUuid;
77

8-
private string $firstName;
8+
private ?string $firstName = null;
99

10-
private string $lastName;
10+
private ?string $lastName = null;
1111

12-
private string $email;
12+
private ?string $email = null;
1313

14-
private string $phone;
14+
private ?string $phone = null;
1515

1616
private string $password;
1717

@@ -36,7 +36,7 @@ public function setFirstName(string $firstName): self
3636
return $this;
3737
}
3838

39-
public function getFirstName(): string
39+
public function getFirstName(): ?string
4040
{
4141
return $this->firstName;
4242
}
@@ -48,7 +48,7 @@ public function setLastName(string $lastName): self
4848
return $this;
4949
}
5050

51-
public function getLastName(): string
51+
public function getLastName(): ?string
5252
{
5353
return $this->lastName;
5454
}
@@ -60,7 +60,7 @@ public function setEmail(string $email): self
6060
return $this;
6161
}
6262

63-
public function getEmail(): string
63+
public function getEmail(): ?string
6464
{
6565
return $this->email;
6666
}
@@ -72,7 +72,7 @@ public function setPhone(string $phone): self
7272
return $this;
7373
}
7474

75-
public function getPhone(): string
75+
public function getPhone(): ?string
7676
{
7777
return $this->phone;
7878
}

src/Service/User.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class User
1515
{
1616
public const DATE_TIME_FORMAT = 'Y-m-d H:i:s';
1717

18-
public function create(mixed $data): object
18+
public function create(mixed $data): array|object
1919
{
2020
$userValidation = new UserValidation($data);
2121
if ($userValidation->isCreationSchemaValid()) {
@@ -46,6 +46,37 @@ public function create(mixed $data): object
4646
throw new InvalidValidationException("Invalid user payload");
4747
}
4848

49+
public function update(mixed $postBody): array|object
50+
{
51+
$userValidation = new UserValidation($postBody);
52+
if ($userValidation->isUpdateSchemaValid()) {
53+
$userUuid = $postBody->userUuid;
54+
55+
$userEntity = new UserEntity();
56+
if (!empty($postBody->first)) {
57+
$userEntity->setFirstName($postBody->first);
58+
}
59+
60+
if (!empty($postBody->last)) {
61+
$userEntity->setLastName($postBody->last);
62+
}
63+
64+
if (!empty($postBody->phone)) {
65+
$userEntity->setPhone($postBody->phone);
66+
}
67+
68+
$result = UserDal::update($userUuid, $userEntity);
69+
if ($result) {
70+
return $postBody;
71+
}
72+
73+
// if invalid, give back an empty response
74+
return [];
75+
}
76+
77+
throw new InvalidValidationException("Invalid user payload");
78+
}
79+
4980
public function retrieveAll(): array
5081
{
5182
$users = UserDal::getAll();
@@ -73,16 +104,6 @@ public function retrieve(string $userUuid): array
73104
throw new InvalidValidationException("Invalid user UUID");
74105
}
75106

76-
public function update(mixed $postBody): object
77-
{
78-
$userValidation = new UserValidation($postBody);
79-
if ($userValidation->isUpdateSchemaValid()) {
80-
return $postBody;
81-
}
82-
83-
throw new InvalidValidationException("Invalid user payload");
84-
}
85-
86107
public function remove(object $data): bool
87108
{
88109
$userValidation = new UserValidation($data);

src/Validation/UserValidation.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function isCreationSchemaValid(): bool
1717
$schemaValidation =
1818
v::attribute('first', v::stringType()->length(self::MINIMUM_NAME_LENGTH, self::MAXIMUM_NAME_LENGTH))
1919
->attribute('last', v::stringType()->length(self::MINIMUM_NAME_LENGTH, self::MAXIMUM_NAME_LENGTH))
20-
->attribute('email', v::email(), mandatory: false)
21-
->attribute('phone', v::phone(), mandatory: false);
20+
->attribute('email', v::email())
21+
->attribute('phone', v::phone());
2222

2323
return $schemaValidation->validate($this->data);
2424
}
@@ -30,7 +30,12 @@ public function isRemoveSchemaValid(): bool
3030

3131
public function isUpdateSchemaValid(): bool
3232
{
33-
// same schema for both creation and update
34-
return $this->isCreationSchemaValid();
33+
$schemaValidation =
34+
v::attribute('userUuid', v::uuid())
35+
->attribute('first', v::stringType()->length(self::MINIMUM_NAME_LENGTH, self::MAXIMUM_NAME_LENGTH), mandatory: false)
36+
->attribute('last', v::stringType()->length(self::MINIMUM_NAME_LENGTH, self::MAXIMUM_NAME_LENGTH), mandatory: false)
37+
->attribute('phone', v::phone(), mandatory: false);
38+
39+
return $schemaValidation->validate($this->data);
3540
}
3641
}

0 commit comments

Comments
 (0)