Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Collection::modify() instead of invoking "findAndModify" #59

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading