1 | <?php |
||
25 | class NotesService { |
||
26 | |||
27 | private $l10n; |
||
28 | private $root; |
||
29 | |||
30 | /** |
||
31 | * @param IRootFolder $root |
||
32 | * @param IL10N $l10n |
||
33 | */ |
||
34 | 11 | public function __construct (IRootFolder $root, IL10N $l10n) { |
|
38 | |||
39 | |||
40 | /** |
||
41 | * @param string $userId |
||
42 | * @return array with all notes in the current directory |
||
43 | */ |
||
44 | 1 | public function getAll ($userId){ |
|
57 | |||
58 | |||
59 | /** |
||
60 | * Used to get a single note by id |
||
61 | * @param int $id the id of the note to get |
||
62 | * @param string $userId |
||
63 | * @throws NoteDoesNotExistException if note does not exist |
||
64 | * @return Note |
||
65 | */ |
||
66 | 3 | public function get ($id, $userId) { |
|
70 | |||
71 | |||
72 | /** |
||
73 | * Creates a note and returns the empty note |
||
74 | * @param string $userId |
||
75 | * @see update for setting note content |
||
76 | * @return Note the newly created note |
||
77 | */ |
||
78 | 2 | public function create ($userId) { |
|
90 | |||
91 | |||
92 | /** |
||
93 | * Updates a note. Be sure to check the returned note since the title is |
||
94 | * dynamically generated and filename conflicts are resolved |
||
95 | * @param int $id the id of the note used to update |
||
96 | * @param string $content the content which will be written into the note |
||
97 | * the title is generated from the first line of the content |
||
98 | * @throws NoteDoesNotExistException if note does not exist |
||
99 | * @return \OCA\Notes\Db\Note the updated note |
||
100 | */ |
||
101 | 2 | public function update ($id, $content, $userId){ |
|
136 | |||
137 | |||
138 | /** |
||
139 | * Deletes a note |
||
140 | * @param int $id the id of the note which should be deleted |
||
141 | * @param string $userId |
||
142 | * @throws NoteDoesNotExistException if note does not |
||
143 | * exist |
||
144 | */ |
||
145 | 3 | public function delete ($id, $userId) { |
|
146 | 3 | $folder = $this->getFolderForUser($userId); |
|
147 | 3 | $file = $this->getFileById($folder, $id); |
|
148 | 1 | $file->delete(); |
|
149 | 1 | } |
|
150 | |||
151 | |||
152 | /** |
||
153 | * @param Folder $folder |
||
154 | * @param int $id |
||
155 | * @throws NoteDoesNotExistException |
||
156 | * @return \OCP\Files\File |
||
157 | */ |
||
158 | 8 | private function getFileById ($folder, $id) { |
|
159 | 8 | $file = $folder->getById($id); |
|
160 | |||
161 | 8 | if(count($file) <= 0 || !$this->isNote($file[0])) { |
|
162 | 4 | throw new NoteDoesNotExistException(); |
|
163 | } |
||
164 | |||
165 | 4 | return $file[0]; |
|
166 | } |
||
167 | |||
168 | |||
169 | /** |
||
170 | * @param string $userId the user id |
||
171 | * @return Folder |
||
172 | */ |
||
173 | 11 | private function getFolderForUser ($userId) { |
|
174 | 11 | $path = '/' . $userId . '/files/Notes'; |
|
175 | 11 | if ($this->root->nodeExists($path)) { |
|
176 | 11 | $folder = $this->root->get($path); |
|
177 | 11 | } else { |
|
178 | $folder = $this->root->newFolder($path); |
||
179 | } |
||
180 | 11 | return $folder; |
|
181 | } |
||
182 | |||
183 | |||
184 | /** |
||
185 | * get path of file and the title.txt and check if they are the same |
||
186 | * file. If not the title needs to be renamed |
||
187 | * |
||
188 | * @param Folder $folder a folder to the notes directory |
||
189 | * @param string $title the filename which should be used |
||
190 | * @param string $extension the extension which should be used |
||
191 | * @param int $id the id of the note for which the title should be generated |
||
192 | * used to see if the file itself has the title and not a different file for |
||
193 | * checking for filename collisions |
||
194 | * @return string the resolved filename to prevent overwriting different |
||
195 | * files with the same title |
||
196 | */ |
||
197 | 4 | private function generateFileName (Folder $folder, $title, $extension, $id) { |
|
217 | |||
218 | /** |
||
219 | * test if file is a note |
||
220 | * |
||
221 | * @param \OCP\Files\File $file |
||
222 | * @return bool |
||
223 | */ |
||
224 | 7 | private function isNote($file) { |
|
235 | |||
236 | } |
||
237 |