1<?php
2
3declare(strict_types=1);
4/**
5 * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
6 *
7 * @license GNU AGPL version 3 or any later version
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24namespace OCA\Talk\Model;
25
26use OCP\AppFramework\Db\QBMapper;
27use OCP\DB\QueryBuilder\IQueryBuilder;
28use OCP\IDBConnection;
29
30/**
31 * @method Session mapRowToEntity(array $row)
32 */
33class SessionMapper extends QBMapper {
34
35	/**
36	 * @param IDBConnection $db
37	 */
38	public function __construct(IDBConnection $db) {
39		parent::__construct($db, 'talk_sessions', Session::class);
40	}
41
42	/**
43	 * @param string $sessionId
44	 * @return Session
45	 * @throws \OCP\AppFramework\Db\DoesNotExistException
46	 */
47	public function findBySessionId(string $sessionId): Session {
48		$query = $this->db->getQueryBuilder();
49		$query->select('*')
50			->from($this->getTableName())
51			->where($query->expr()->eq('session_id', $query->createNamedParameter($sessionId)));
52
53		return $this->findEntity($query);
54	}
55
56	/**
57	 * @param int $attendeeId
58	 * @return Session[]
59	 */
60	public function findByAttendeeId(int $attendeeId): array {
61		$query = $this->db->getQueryBuilder();
62		$query->select('*')
63			->from($this->getTableName())
64			->where($query->expr()->eq('attendee_id', $query->createNamedParameter($attendeeId)));
65
66		return $this->findEntities($query);
67	}
68
69	/**
70	 * @param int $attendeeId
71	 * @return int Number of deleted entities
72	 */
73	public function deleteByAttendeeId(int $attendeeId): int {
74		$delete = $this->db->getQueryBuilder();
75		$delete->delete($this->getTableName())
76			->where($delete->expr()->eq('attendee_id', $delete->createNamedParameter($attendeeId, IQueryBuilder::PARAM_INT)));
77
78		return (int) $delete->executeStatement();
79	}
80
81	/**
82	 * @param int[] $ids
83	 * @return int Number of deleted entities
84	 */
85	public function deleteByIds(array $ids): int {
86		$delete = $this->db->getQueryBuilder();
87		$delete->delete($this->getTableName())
88			->where($delete->expr()->in('id', $delete->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
89
90		return (int) $delete->executeStatement();
91	}
92
93	public function createSessionFromRow(array $row): Session {
94		return $this->mapRowToEntity([
95			'id' => $row['s_id'],
96			'session_id' => $row['session_id'],
97			'attendee_id' => (int) $row['a_id'],
98			'in_call' => (int) $row['in_call'],
99			'last_ping' => (int) $row['last_ping'],
100		]);
101	}
102}
103