Skip to content

Commit b2bc213

Browse files
authored
Merge pull request #59 from thekid/refactor/modify
Use Collection::modify() instead of invoking "findAndModify"
2 parents 9d4e3b7 + feef2e4 commit b2bc213

File tree

4 files changed

+9
-26
lines changed

4 files changed

+9
-26
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"xp-forge/yaml": "^8.0",
1919
"xp-forge/hashing": "^2.1",
2020
"xp-forge/inject": "^5.4",
21-
"xp-forge/mongodb": "^2.0"
21+
"xp-forge/mongodb": "^2.2"
2222
},
2323

2424
"require-dev": {

src/main/php/de/thekid/dialog/Modification.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/main/php/de/thekid/dialog/Repository.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace de\thekid\dialog;
22

3-
use com\mongodb\result\{Cursor, Update};
3+
use com\mongodb\result\{Cursor, Update, Modification};
44
use com\mongodb\{Database, Document};
55
use text\hash\Hashing;
66
use util\{Date, Secret};
@@ -155,13 +155,11 @@ public function children(string $slug): Cursor {
155155

156156
/** Replace an entry identified by a given slug with a given entity */
157157
public function replace(string $slug, array<string, mixed> $entity): Modification {
158-
$arguments= [
159-
'query' => ['slug' => $slug],
160-
'update' => ['$set' => ['slug' => $slug, ...$entity]],
161-
'new' => true, // Return modified document
162-
'upsert' => true,
163-
];
164-
return new Modification($this->database->collection('entries')->run('findAndModify', $arguments)->value());
158+
return $this->database->collection('entries')->modify(
159+
['slug' => $slug],
160+
['$set' => ['slug' => $slug, ...$entity]],
161+
upsert: true,
162+
);
165163
}
166164

167165
/** Modify an entry identified by a given slug with MongoDB statements */

src/main/php/de/thekid/dialog/api/Entries.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function create(#[Value] $user, string $id, #[Entity] array<string, mixed
3535
]);
3636

3737
// Ensure storage directory is created
38-
if ($result->created()) {
38+
if ($result->upserted()) {
3939
$this->storage->folder($id)->create();
4040
}
4141

42-
return $result->entry();
42+
return $result->document();
4343
}
4444

4545
#[Put('/{id:.+(/.+)?}/images/{name}')]

0 commit comments

Comments
 (0)