You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.4 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023, Joas Schilling <coding@schilljs.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Talk\Model;
  23. use OCP\AppFramework\Db\QBMapper;
  24. use OCP\AppFramework\Db\TTransactional;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IDBConnection;
  27. /**
  28. * @method BotConversation mapRowToEntity(array $row)
  29. * @method BotConversation findEntity(IQueryBuilder $query)
  30. * @method BotConversation[] findEntities(IQueryBuilder $query)
  31. * @template-extends QBMapper<BotConversation>
  32. */
  33. class BotConversationMapper extends QBMapper {
  34. use TTransactional;
  35. public function __construct(
  36. IDBConnection $db,
  37. ) {
  38. parent::__construct($db, 'talk_bots_conversation', BotConversation::class);
  39. }
  40. /**
  41. * @return BotConversation[]
  42. */
  43. public function findForToken(string $token): array {
  44. $query = $this->db->getQueryBuilder();
  45. $query->select('*')
  46. ->from($this->getTableName())
  47. ->where($query->expr()->eq('token', $query->createNamedParameter($token)));
  48. return $this->findEntities($query);
  49. }
  50. public function deleteByBotId(int $botId): int {
  51. $query = $this->db->getQueryBuilder();
  52. $query->delete($this->getTableName())
  53. ->where($query->expr()->eq('bot_id', $query->createNamedParameter($botId, IQueryBuilder::PARAM_INT)));
  54. return $query->executeStatement();
  55. }
  56. public function deleteByBotIdAndTokens(int $botId, array $tokens): int {
  57. $query = $this->db->getQueryBuilder();
  58. $query->delete($this->getTableName())
  59. ->where($query->expr()->eq('bot_id', $query->createNamedParameter($botId, IQueryBuilder::PARAM_INT)))
  60. ->andWhere($query->expr()->in('token', $query->createNamedParameter($tokens, IQueryBuilder::PARAM_STR_ARRAY)));
  61. return $query->executeStatement();
  62. }
  63. }