Skip to content

Commit

Permalink
Merge pull request #59 from thekid/refactor/modify
Browse files Browse the repository at this point in the history
Use Collection::modify() instead of invoking "findAndModify"
  • Loading branch information
thekid authored Mar 2, 2024
2 parents 9d4e3b7 + feef2e4 commit b2bc213
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 26 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"xp-forge/yaml": "^8.0",
"xp-forge/hashing": "^2.1",
"xp-forge/inject": "^5.4",
"xp-forge/mongodb": "^2.0"
"xp-forge/mongodb": "^2.2"
},

"require-dev": {
Expand Down
15 changes: 0 additions & 15 deletions src/main/php/de/thekid/dialog/Modification.php

This file was deleted.

14 changes: 6 additions & 8 deletions src/main/php/de/thekid/dialog/Repository.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace de\thekid\dialog;

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

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

/** Modify an entry identified by a given slug with MongoDB statements */
Expand Down
4 changes: 2 additions & 2 deletions src/main/php/de/thekid/dialog/api/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public function create(#[Value] $user, string $id, #[Entity] array<string, mixed
]);

// Ensure storage directory is created
if ($result->created()) {
if ($result->upserted()) {
$this->storage->folder($id)->create();
}

return $result->entry();
return $result->document();
}

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

0 comments on commit b2bc213

Please sign in to comment.