Skip to content

Commit fe14a80

Browse files
committed
fix(api): use context to load document
Signed-off-by: Max <max@nextcloud.com>
1 parent 00402bd commit fe14a80

3 files changed

Lines changed: 6 additions & 14 deletions

File tree

lib/Db/DocumentMapper.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace OCA\Text\Db;
99

1010
use Generator;
11-
use OCA\Text\Context\IContext;
1211
use OCP\AppFramework\Db\DoesNotExistException;
1312
use OCP\AppFramework\Db\QBMapper;
1413
use OCP\DB\QueryBuilder\IQueryBuilder;
@@ -42,25 +41,19 @@ public function find(int $documentId): Document {
4241
return Document::fromRow($data);
4342
}
4443

45-
/**
46-
* @throws DoesNotExistException
47-
*/
48-
public function load(IContext $context): Document {
49-
$type = $context->getType();
50-
$id = $context->getId();
51-
44+
public function load(string $type, int $id): ?Document {
5245
/* @var $qb IQueryBuilder */
5346
$qb = $this->db->getQueryBuilder();
5447
$result = $qb->select('*')
5548
->from($this->getTableName())
5649
->where($qb->expr()->eq('context_type', $qb->createNamedParameter($type)))
57-
->where($qb->expr()->eq('context_id', $qb->createNamedParameter($id)))
50+
->andWhere($qb->expr()->eq('context_id', $qb->createNamedParameter($id)))
5851
->executeQuery();
5952

6053
$data = $result->fetchAssociative();
6154
$result->closeCursor();
6255
if ($data === false) {
63-
throw new DoesNotExistException('Document doesn\'t exist');
56+
return null;
6457
}
6558
return Document::fromRow($data);
6659
}

lib/Service/ApiService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function create(IContext $context, ?string $baseVersionEtag, ?string $gue
4848
}
4949

5050
try {
51-
$document = $this->documentService->getOrCreateDocument($document, $context);
51+
$document = $this->documentService->getOrCreateDocument($document);
5252
} catch (Exception $e) {
5353
$this->logger->error($e->getMessage(), ['exception' => $e]);
5454
return new DataResponse(['error' => 'Failed to create the document session'], Http::STATUS_INTERNAL_SERVER_ERROR);

lib/Service/DocumentService.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ public function isSaveFromText(): bool {
100100
* @throws NotPermittedException
101101
* @throws Exception
102102
*/
103-
public function getOrCreateDocument(Document $document, IContext $context): Document {
104-
// TODO: drop $context once $document contains contextId and contextType
105-
$loaded = $this->getDocument($context->getId());
103+
public function getOrCreateDocument(Document $document): Document {
104+
$loaded = $this->documentMapper->load($document->getContextType(), $document->getContextId());
106105
if ($loaded !== null) {
107106
$this->logger->info('Keep previous document of ' . $document->toString());
108107
return $loaded;

0 commit comments

Comments
 (0)