-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathAttachmentMapper.php
More file actions
157 lines (140 loc) · 4.5 KB
/
Copy pathAttachmentMapper.php
File metadata and controls
157 lines (140 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Deck\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Entity;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUserManager;
/** @template-extends DeckMapper<Attachment> */
class AttachmentMapper extends DeckMapper implements IPermissionMapper {
private $cardMapper;
private $userManager;
private $qb;
/**
* AttachmentMapper constructor.
*
* @param IDBConnection $db
* @param CardMapper $cardMapper
* @param IUserManager $userManager
*/
public function __construct(IDBConnection $db, CardMapper $cardMapper, IUserManager $userManager) {
parent::__construct($db, 'deck_attachment', Attachment::class);
$this->cardMapper = $cardMapper;
$this->userManager = $userManager;
$this->qb = $this->db->getQueryBuilder();
}
/**
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function find(int $id): Attachment {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
return $this->findEntity($qb);
}
/**
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
public function findByData(int $cardId, string $data): Attachment {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('data', $qb->createNamedParameter($data, IQueryBuilder::PARAM_STR)));
return $this->findEntity($qb);
}
/**
* Returns a map of cardId => attachment count for the given card IDs in a single query.
*
* @param int[] $cardIds
* @return array<int, int>
* @throws \OCP\DB\Exception
*/
public function findCountByCardIds(array $cardIds): array {
if (empty($cardIds)) {
return [];
}
$qb = $this->db->getQueryBuilder();
$qb->select('card_id')
->selectAlias($qb->func()->count('id'), 'attachment_count')
->from($this->getTableName())
->where($qb->expr()->in('card_id', $qb->createNamedParameter($cardIds, IQueryBuilder::PARAM_INT_ARRAY)))
->andWhere($qb->expr()->eq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
->groupBy('card_id');
$counts = [];
$cursor = $qb->executeQuery();
while ($row = $cursor->fetch()) {
$counts[(int)$row['card_id']] = (int)$row['attachment_count'];
}
$cursor->closeCursor();
return $counts;
}
/**
* @return Entity[]
* @throws \OCP\DB\Exception
*/
public function findAll(int $cardId): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
return $this->findEntities($qb);
}
/**
* @return Attachment[]
*/
public function findToDelete(?int $cardId = null, bool $withOffset = true): array {
// add buffer of 5 min
$timeLimit = time() - (60 * 5);
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->getTableName())
->where($qb->expr()->gt('deleted_at', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
if ($withOffset) {
$qb
->andWhere($qb->expr()->lt('deleted_at', $qb->createNamedParameter($timeLimit, IQueryBuilder::PARAM_INT)));
}
if ($cardId !== null) {
$qb
->andWhere($qb->expr()->eq('card_id', $qb->createNamedParameter($cardId, IQueryBuilder::PARAM_INT)));
}
return $this->findEntities($qb);
}
/**
* Check if $userId is owner of Entity with $id
*/
public function isOwner(string $userId, int $id): bool {
try {
$attachment = $this->find($id);
return $this->cardMapper->isOwner($userId, $attachment->getCardId());
} catch (DoesNotExistException $e) {
} catch (MultipleObjectsReturnedException $e) {
}
return false;
}
/**
* Query boardId for Entity of given $id
*
* @param $id int unique entity identifier
* @return int|null id of Board
*/
public function findBoardId(int $id): ?int {
try {
$attachment = $this->find($id);
} catch (\Exception $e) {
return null;
}
return $this->cardMapper->findBoardId($attachment->getCardId());
}
}