NotebookApiController::create()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 3
nc 2
nop 3
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\Controller;
25
26
use OCA\NextNote\Db\Notebook;
27
use OCA\NextNote\Service\NotebookService;
28
use OCA\NextNote\Utility\NotFoundJSONResponse;
29
use OCA\NextNote\Utility\UnauthorizedJSONResponse;
30
use OCA\NextNote\Utility\Utils;
31
use \OCP\AppFramework\ApiController;
32
use OCP\AppFramework\Http\JSONResponse;
33
use OCP\IConfig;
34
use OCP\ILogger;
35
use \OCP\IRequest;
36
use OCP\IUserManager;
37
38
39
class NotebookApiController extends ApiController {
40
41
	private $config;
42
	private $notebookService;
43
	private $userManager;
44
45 View Code Duplication
	public function __construct($appName, IRequest $request,
46
								ILogger $logger, IConfig $config, NotebookService $notebookService, IUserManager $userManager) {
47
		parent::__construct($appName, $request);
48
		$this->config = $config;
49
		$this->notebookService = $notebookService;
50
		$this->userManager = $userManager;
51
	}
52
53
	/**
54
	 * @NoAdminRequired
55
	 * @NoCSRFRequired
56
	 * @TODO Add etag / lastmodified
57
	 * @param int|bool $deleted
58
	 * @return JSONResponse
59
	 */
60
	public function index($deleted = false) {
61
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
62
		$result = $this->notebookService->findNotebooksFromUser($uid, $deleted);
63
		$results = $result;
64
		if($result instanceof Notebook){
65
			$results = [];
66
			/**
67
			 * @var $result Notebook
68
			 */
69
			$results[$result->getId()] = $result;
70
		}
71
72
		return new JSONResponse($results);
73
	}
74
75
	/**
76
	 * @NoAdminRequired
77
	 * @NoCSRFRequired
78
	 * @TODO Add etag / lastmodified
79
	 * @param $id
80
	 * @return NotFoundJSONResponse|JSONResponse
81
	 */
82
	public function get($id) {
83
		$result = $this->notebookService->find($id);
84
		if (!$result) {
85
			return new NotFoundJSONResponse();
86
		}
87
		//@todo Check access
88
		$result = $result->jsonSerialize();
89
		return new JSONResponse($result);
90
	}
91
92
93
	/**
94
	 * @NoAdminRequired
95
	 * @NoCSRFRequired
96
	 * @param $name
97
	 * @param $color
98
	 * @param $parent_id
99
	 * @return JSONResponse
100
	 */
101
	public function create($name, $color, $parent_id) {
102
		if ($name == "" || !$name) {
103
			return new JSONResponse(['error' => 'name is missing']);
104
		}
105
106
107
		$uid = \OC::$server->getUserSession()->getUser()->getUID();
108
		$notebook = new Notebook();
109
		$notebook->setName($name);
110
		$notebook->setParentId($parent_id);
111
		$notebook->setUid($uid);
112
		$notebook->setColor($color);
113
		$notebook->setGuid(Utils::GUID());
114
115
		/*
116
		if($this->notebookService->findByName($name)){
117
			return new JSONResponse(['error' => 'Group already exists']);
118
		}*/
119
120
		$result = $this->notebookService->create($notebook, $uid)->jsonSerialize();
121
		\OC_Hook::emit('OCA\NextNote', 'post_create_notebook', ['notebook' => $notebook]);
122
		return new JSONResponse($result);
123
	}
124
125
	/**
126
	 * @NoAdminRequired
127
	 * @NoCSRFRequired
128
	 * @param $id
129
	 * @param $name
130
	 * @param $color
131
	 * @param $parent_id
132
	 * @return NotFoundJSONResponse|UnauthorizedJSONResponse|JSONResponse
133
	 */
134
	public function update($id, $name, $color, $parent_id) {
135
		if ($name == "" || !$name) {
136
			return new JSONResponse(['error' => 'title is missing']);
137
		}
138
139
		//@TODO for sharing add access check
140
		$notebook = $this->notebookService->find($id);
141
		if (!$notebook) {
142
			return new NotFoundJSONResponse();
143
		}
144
145
		$notebook->setName($name);
146
		$notebook->setParentId($parent_id);
147
		$notebook->setColor($color);
148
149
		$results = $this->notebookService->update($notebook)->jsonSerialize();
150
		\OC_Hook::emit('OCA\NextNote', 'post_update_notebook', ['notebook' => $notebook]);
151
		return new JSONResponse($results);
152
	}
153
154
	/**
155
	 * @NoAdminRequired
156
	 * @NoCSRFRequired
157
	 * @param $id
158
	 * @return NotFoundJSONResponse|UnauthorizedJSONResponse|JSONResponse
159
	 */
160 View Code Duplication
	public function delete($id) {
161
		$entity = $this->notebookService->find($id);
162
		if (!$entity) {
163
			return new NotFoundJSONResponse();
164
		}
165
166
		$this->notebookService->delete($id);
167
		$result = (object)['success' => true];
168
		\OC_Hook::emit('OCA\NextNote', 'post_delete_notebook', ['notebook_id' => $id]);
169
		return new JSONResponse($result);
170
	}
171
}
172