|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\OpenAi\Db; |
| 9 | + |
| 10 | +use OCP\AppFramework\Db\DoesNotExistException; |
| 11 | +use OCP\AppFramework\Db\MultipleObjectsReturnedException; |
| 12 | +use OCP\AppFramework\Db\QBMapper; |
| 13 | +use OCP\DB\Exception; |
| 14 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 15 | +use OCP\IDBConnection; |
| 16 | + |
| 17 | +/** |
| 18 | + * @extends QBMapper<QuotaRule> |
| 19 | + */ |
| 20 | +class QuotaRuleMapper extends QBMapper { |
| 21 | + public function __construct( |
| 22 | + IDBConnection $db, |
| 23 | + private QuotaUserMapper $quotaUserMapper, |
| 24 | + ) { |
| 25 | + parent::__construct($db, 'openai_quota_rule', QuotaRule::class); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @return array |
| 30 | + * @throws Exception |
| 31 | + */ |
| 32 | + public function getRules(): array { |
| 33 | + $qb = $this->db->getQueryBuilder(); |
| 34 | + |
| 35 | + $qb->select('*') |
| 36 | + ->from($this->getTableName()); |
| 37 | + |
| 38 | + return $this->findEntities($qb); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param int $quotaType |
| 43 | + * @param string $userId |
| 44 | + * @param array $groups |
| 45 | + * @return QuotaRule |
| 46 | + * @throws DoesNotExistException |
| 47 | + * @throws Exception |
| 48 | + * @throws MultipleObjectsReturnedException |
| 49 | + */ |
| 50 | + public function getRule(int $quotaType, string $userId, array $groups): QuotaRule { |
| 51 | + $qb = $this->db->getQueryBuilder(); |
| 52 | + |
| 53 | + $qb->select('r.*') |
| 54 | + ->from($this->getTableName(), 'r') |
| 55 | + ->leftJoin('r', 'openai_quota_user', 'u', 'r.id = u.rule_id') |
| 56 | + ->where( |
| 57 | + $qb->expr()->eq('type', $qb->createNamedParameter($quotaType, IQueryBuilder::PARAM_INT)) |
| 58 | + )->andWhere( |
| 59 | + $qb->expr()->orX( |
| 60 | + $qb->expr()->andX( |
| 61 | + $qb->expr()->eq('u.entity_type', $qb->createNamedParameter('user', IQueryBuilder::PARAM_STR)), |
| 62 | + $qb->expr()->eq('u.entity_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
| 63 | + ), |
| 64 | + $qb->expr()->andX( |
| 65 | + $qb->expr()->eq('u.entity_type', $qb->createNamedParameter('group', IQueryBuilder::PARAM_STR)), |
| 66 | + $qb->expr()->in('u.entity_id', $qb->createNamedParameter($groups, IQueryBuilder::PARAM_STR_ARRAY)) |
| 67 | + ), |
| 68 | + |
| 69 | + ) |
| 70 | + )->orderBy('r.priority', 'ASC') |
| 71 | + ->setMaxResults(1); |
| 72 | + /** @var QuotaRule $entity */ |
| 73 | + $entity = $this->findEntity($qb); |
| 74 | + return $entity; |
| 75 | + } |
| 76 | + /** |
| 77 | + * @param int $quotaType |
| 78 | + * @param int $amount |
| 79 | + * @param int $priority |
| 80 | + * @param bool $pool |
| 81 | + * @return int |
| 82 | + * @throws Exception |
| 83 | + */ |
| 84 | + public function addRule(int $quotaType, int $amount, int $priority, bool $pool): int { |
| 85 | + $qb = $this->db->getQueryBuilder(); |
| 86 | + |
| 87 | + $qb->insert($this->getTableName()) |
| 88 | + ->values( |
| 89 | + [ |
| 90 | + 'type' => $qb->createNamedParameter($quotaType, IQueryBuilder::PARAM_INT), |
| 91 | + 'amount' => $qb->createNamedParameter($amount, IQueryBuilder::PARAM_INT), |
| 92 | + 'priority' => $qb->createNamedParameter($priority, IQueryBuilder::PARAM_INT), |
| 93 | + 'pool' => $qb->createNamedParameter($pool, IQueryBuilder::PARAM_BOOL) |
| 94 | + ] |
| 95 | + ); |
| 96 | + $qb->executeStatement(); |
| 97 | + return $qb->getLastInsertId(); |
| 98 | + } |
| 99 | + /** |
| 100 | + * @param int $id |
| 101 | + * @param int $quotaType |
| 102 | + * @param int $amount |
| 103 | + * @param int $priority |
| 104 | + * @param bool $pool |
| 105 | + * @return void |
| 106 | + * @throws Exception |
| 107 | + */ |
| 108 | + public function updateRule(int $id, int $quotaType, int $amount, int $priority, bool $pool): void { |
| 109 | + $qb = $this->db->getQueryBuilder(); |
| 110 | + |
| 111 | + $qb->update($this->getTableName()) |
| 112 | + ->set('type', $qb->createNamedParameter($quotaType, IQueryBuilder::PARAM_INT)) |
| 113 | + ->set('amount', $qb->createNamedParameter($amount, IQueryBuilder::PARAM_INT)) |
| 114 | + ->set('priority', $qb->createNamedParameter($priority, IQueryBuilder::PARAM_INT)) |
| 115 | + ->set('pool', $qb->createNamedParameter($pool, IQueryBuilder::PARAM_BOOL)) |
| 116 | + ->where( |
| 117 | + $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)) |
| 118 | + ); |
| 119 | + $qb->executeStatement(); |
| 120 | + } |
| 121 | + /** |
| 122 | + * @param int $id |
| 123 | + * @throws Exception |
| 124 | + */ |
| 125 | + public function deleteRule(int $id): void { |
| 126 | + $qb = $this->db->getQueryBuilder(); |
| 127 | + |
| 128 | + $qb->delete($this->getTableName()) |
| 129 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
| 130 | + $qb->executeStatement(); |
| 131 | + } |
| 132 | +} |
0 commit comments