Completed
Push — master ( c9fc28...4733eb )
by Sander
11s
created

NoteService::createExampleNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
/**
3
 * Nextcloud - namespace OCA\Nextnote
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
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
24
namespace OCA\NextNote\Service;
25
26
use OCA\NextNote\Db\Notebook;
27
use OCA\NextNote\Db\Note;
28
use OCA\NextNote\Fixtures\ExampleNote;
29
use OCA\NextNote\Fixtures\ShareFix;
30
use OCA\NextNote\ShareBackend\NextNoteShareBackend;
31
use OCA\NextNote\Utility\Utils;
32
use OCA\NextNote\Db\NoteMapper;
33
34
35
class NoteService {
36
37
	private $noteMapper;
38
	private $utils;
39
	private $sharing;
40
	private $groupService;
41
42
	public function __construct(NoteMapper $noteMapper, Utils $utils, NextNoteShareBackend $shareBackend, NotebookService $groupService) {
43
		$this->noteMapper = $noteMapper;
44
		$this->utils = $utils;
45
		$this->sharing = $shareBackend;
46
		$this->groupService = $groupService;
47
	}
48
49
	/**
50
	 * Get notes from a user.
51
	 *
52
	 * @param $userId
53
	 * @param int|bool $deleted
54
	 * @param string|bool $grouping
55
	 * @return Note[]
56
	 */
57
	public function findNotesFromUser($userId, $deleted = false, $grouping = false) {
58
		// Get shares
59
60
		$dbNotes = $this->noteMapper->findNotesFromUser($userId, $deleted, $grouping);
61
		$sharedNotes = $this->sharing->getSharedNotes();
62
		$notes = array_merge($dbNotes, $sharedNotes);
63
		return $notes;
64
	}
65
66
	/**
67
	 * Get a single note
68
	 *
69
	 * @param $note_id
70
	 * @param $user_id
71
	 * @param bool|int $deleted
72
	 * @return Note
73
	 * @internal param $vault_id
74
	 */
75
	public function find($note_id, $user_id = null, $deleted = false) {
76
		$note = $this->noteMapper->find($note_id, $user_id, $deleted);
77
		return $note;
78
	}
79
80
	/**
81
	 * Creates a note
82
	 *
83
	 * @param Note $note
84
	 * @return Note
85
	 * @throws \Exception
86
	 */
87
	public function create(Note $note) {
88
		if (!$note instanceof Note) {
89
			throw new \Exception("Expected Note object!");
90
		}
91
92
		return $this->noteMapper->insert($note);
93
	}
94
95
	/**
96
	 * Update note
97
	 *
98
	 * @param $note Note
99
	 * @return Note|bool
100
	 * @throws \Exception
101
	 * @internal param $userId
102
	 * @internal param $vault
103
	 */
104
	public function update(Note $note) {
105
		if (!$note instanceof Note) {
106
			throw new \Exception("Expected Note object!");
107
		}
108
109
		return $this->noteMapper->updateNote($note);
110
	}
111
112
	/**
113
	 * Delete a note from user
114
	 *
115
	 * @param $note_id
116
	 * @param string $user_id
117
	 * @return bool
118
	 * @internal param string $vault_guid
119
	 */
120
	public function delete($note_id, $user_id = null) {
121
		$note = $this->noteMapper->find($note_id, $user_id);
122
		if ($note instanceof Note) {
123
			$this->noteMapper->deleteNote($note);
124
			return true;
125
		} else {
126
			return false;
127
		}
128
	}
129
130
	/**
131
	 * Creates an example note for a user.
132
	 * @param $userId
133
	 */
134
	public function createExampleNote($userId) {
135
		$note = new Note();
136
		$note->setName(ExampleNote::TITLE);
137
		$note->setNote(ExampleNote::NOTE_CONTENT);
138
		$note->setUid($userId);
139
		$note->setMtime(time());
140
		$note->setGuid(Utils::GUID());
141
		$this->create($note);
142
	}
143
}
144