NotebookMapper::insert()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 *
6
 * @copyright Copyright (c) 2017, Sander Brand ([email protected])
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
24
namespace OCA\NextNote\Db;
25
26
use \OCA\NextNote\Utility\Utils;
27
use OCP\AppFramework\Db\Entity;
28
use OCP\IDBConnection;
29
use OCP\AppFramework\Db\Mapper;
30
31
32
class NotebookMapper extends Mapper {
33
	private $utils;
34
35
	public function __construct(IDBConnection $db, Utils $utils) {
36
		parent::__construct($db, 'nextnote_groups');
37
		$this->utils = $utils;
38
	}
39
40
	/**
41
	 * Get Notebook(s)
42
	 *
43
	 * @param null|int $notebook_id
44
	 * @param null|int $user_id
45
	 * @param bool|int $deleted
46
	 * @return Notebook[]|Notebook
47
	 */
48 View Code Duplication
	public function find($notebook_id = null, $user_id = null, $deleted = false) {
49
		$qb = $this->db->getQueryBuilder();
50
		$qb->select('g.*', 'g.guid as guid')//'COUNT(n.id) as note_count'
51
		->selectAlias($qb->createFunction('COUNT(' . $qb->getColumnName('n.id') . ')'), 'note_count')
52
			->from('nextnote_groups', 'g')
53
			->leftJoin('g', 'nextnote_notes', 'n', $qb->expr()->eq('g.id', 'n.notebook'))->groupBy(['g.id']);
54
55
		$where = [];
56
		if (!is_null($notebook_id)) {
57
			$where['g.id'] = $notebook_id;
58
		}
59
60
		if ($user_id !== null) {
61
			$where['g.uid'] = $user_id;
62
		}
63
64
		if ($deleted !== false) {
65
			$where['g.deleted'] = $deleted;
66
		}
67
		$i = 0;
68
		foreach ($where as $field => $value) {
69
			if ($i === 0) {
70
				$qb->where($qb->expr()->eq($field, $qb->createNamedParameter($value)));
71
			} else {
72
				$qb->andWhere($qb->expr()->eq($field, $qb->createNamedParameter($value)));
73
			}
74
			$i++;
75
		}
76
77
		$result = $qb->execute();
78
		/**
79
		 * @var $results Notebook[]
80
		 */
81
		$results = [];
82
		while ($item = $result->fetch()) {
83
			$results[] = $this->makeEntityFromDBResult($item);
84
		}
85
		$result->closeCursor();
86
		if (count($results) === 1) {
87
			return reset($results);
88
		}
89
		return $results;
90
	}
91
92
	/**
93
	 * @param $group_name
94
	 * @param null $user_id
95
	 * @param bool $deleted
96
	 * @return Notebook[]|Notebook
97
	 */
98 View Code Duplication
	public function findByName($group_name, $user_id = null, $deleted = false) {
99
		$qb = $this->db->getQueryBuilder();
100
		$qb->select('g.*', 'g.guid as guid')//'COUNT(n.id) as note_count'
101
		->selectAlias($qb->createFunction('COUNT(' . $qb->getColumnName('n.id') . ')'), 'note_count')
102
			->from('nextnote_groups', 'g')
103
			->leftJoin('g', 'nextnote_notes', 'n', $qb->expr()->eq('g.id', 'n.notebook'))->groupBy(['g.id']);
104
105
		$where = [];
106
		if ($group_name) {
107
			$where['g.name'] = $group_name;
108
		}
109
110
		if ($user_id !== null) {
111
			$where['g.uid'] = $user_id;
112
		}
113
114
		if ($deleted !== false) {
115
			$where['g.deleted'] = $deleted;
116
		}
117
		$i = 0;
118
		foreach ($where as $field => $value) {
119
			if ($i === 0) {
120
				$qb->where($qb->expr()->eq($field, $qb->createNamedParameter($value)));
121
			} else {
122
				$qb->andWhere($qb->expr()->eq($field, $qb->createNamedParameter($value)));
123
			}
124
			$i++;
125
		}
126
127
		$result = $qb->execute();
128
129
		/**
130
		 * @var $results Notebook[]
131
		 */
132
		$results = [];
133
		while ($item = $result->fetch()) {
134
			$results[] = $this->makeEntityFromDBResult($item);
135
		}
136
		$result->closeCursor();
137
		if (count($results) === 1) {
138
			return reset($results);
139
		}
140
		return $results;
141
	}
142
143
	/**
144
	 * Creates a group
145
	 *
146
	 * @param Notebook|Entity $group
147
	 * @return Note|Entity
148
	 * @internal param $userId
149
	 */
150
	public function insert(Entity $group) {
151
		$group->setNoteCount(null);
152
		return parent::insert($group);
153
	}
154
155
	/**
156
	 * Update group
157
	 *
158
	 * @param Notebook|Entity $group
159
	 * @return Notebook|Entity
160
	 */
161
	public function update(Entity $group) {
162
		return parent::update($group);
163
	}
164
165
	/**
166
	 * Delete group
167
	 *
168
	 * @param Notebook|Entity $group
169
	 * @return Notebook|Entity
170
	 */
171
	public function delete(Entity $group) {
172
		return parent::delete($group);
173
	}
174
175
	/**
176
	 * @param $arr
177
	 * @return Notebook
178
	 */
179
	public function makeEntityFromDBResult($arr) {
180
		$group = new Notebook();
181
		$group->setId($arr['id']);
182
		$group->setName($arr['name']);
183
		$group->setGuid($arr['guid']);
184
		$group->setParentId($arr['parent_id']);
185
		$group->setColor($arr['color']);
186
		$group->setNoteCount((int)$arr['note_count']);
187
		$group->setDeleted($arr['deleted']);
188
189
		return $group;
190
	}
191
192
193
}
194