Completed
Pull Request — master (#21)
by Sander
01:21
created

NextNoteApiController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
/**
3
 * Nextcloud - NextNote
4
 *
5
 * @copyright Copyright (c) 2015, Ben Curtis <[email protected]>
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\Controller;
25
26
use OCA\NextNote\Service\NextNoteService;
27
use OCA\NextNote\Utility\NotFoundJSONResponse;
28
use \OCP\AppFramework\ApiController;
29
use OCP\AppFramework\Http\JSONResponse;
30
use OCP\IConfig;
31
use OCP\ILogger;
32
use \OCP\IRequest;
33
use \OCA\NextNote\Lib\Backend;
34
35
36
class NextNoteApiController extends ApiController {
37
38
	private $config;
39
	private $noteService;
40
41
	public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, NextNoteService $noteService) {
42
		parent::__construct($appName, $request);
43
		$this->config = $config;
44
		$this->noteService = $noteService;
45
	}
46
47
	/**
48
	 * @NoAdminRequired
49
	 * @NoCSRFRequired
50
	 * @TODO Add etag / lastmodified
51
	 * @param int|bool $deleted
52
	 * @param string|bool $group
53
	 * @return JSONResponse
54
	 */
55
	public function index($deleted = false, $group = false) {
56
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
57
		$results = $this->noteService->findNotesFromUser($uid, $deleted, $group);
58
		return new JSONResponse($results);
59
	}
60
61
	/**
62
	 * @NoAdminRequired
63
	 * @NoCSRFRequired
64
	 * @TODO Add etag / lastmodified
65
	 */
66
	public function get($id) {
67
		$results = $this->noteService->find($id);
68
		//@TODO for sharing add access check
69
		if (!$results) {
70
			return new NotFoundJSONResponse();
71
		}
72
		return new JSONResponse($results);
73
	}
74
75
76
	/**
77
	 * @NoAdminRequired
78
	 * @NoCSRFRequired
79
	 */
80
	public function create($title, $group, $note) {
81
		if($title == "" || !$title){
82
			return new JSONResponse(['error' => 'title is missing']);
83
		}
84
		$note = [
85
			'title' => $title,
86
			'name' => $title,
87
			'grouping' => $group,
88
			'note' => $note
89
		];
90
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
91
		$result = $this->noteService->create($note, $uid);
92
		return new JSONResponse($result);
93
	}
94
95
	/**
96
	 * @NoAdminRequired
97
	 * @NoCSRFRequired
98
	 */
99
	public function update($id, $title, $grouping, $content, $deleted) {
100
		if($title == "" || !$title){
101
			return new JSONResponse(['error' => 'title is missing']);
102
		}
103
104
		$note = [
105
			'id' => $id,
106
			'title' => $title,
107
			'name' => $title,
108
			'grouping' => $grouping,
109
			'note' => $content,
110
            'deleted' => $deleted
111
		];
112
        //@TODO for sharing add access check
113
		$entity = $this->noteService->find($id);
114
		if (!$entity) {
115
			return new NotFoundJSONResponse();
116
		}
117
118
		$results = $this->noteService->update($note);
119
		return new JSONResponse($results);
120
	}
121
122
	/**
123
	 * @NoAdminRequired
124
	 * @NoCSRFRequired
125
	 */
126
	public function delete($id) {
127
		$entity = $this->noteService->find($id);
128
		if (!$entity) {
129
			return new NotFoundJSONResponse();
130
		}
131
        //@TODO for sharing add access check
132
		$this->noteService->delete($id);
133
		$result = (object) ['success' => true];
134
		return new JSONResponse($result);
135
	}
136
137
}
138