NotesApiController::update()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 5
nop 5
1
<?php
2
3
namespace OCA\Notes\Controller;
4
5
use OCP\AppFramework\ApiController;
6
use OCP\AppFramework\Http;
7
use OCP\AppFramework\Http\DataResponse;
8
use OCP\IRequest;
9
use OCP\IUserSession;
10
11
use OCA\Notes\Service\NotesService;
12
use OCA\Notes\Service\MetaService;
13
use OCA\Notes\Service\InsufficientStorageException;
14
use OCA\Notes\Service\NoteDoesNotExistException;
15
use OCA\Notes\Db\Note;
16
17
/**
18
 * Class NotesApiController
19
 *
20
 * @package OCA\Notes\Controller
21
 */
22
class NotesApiController extends ApiController {
23
24
	use Errors;
25
26
	/** @var NotesService */
27
	private $service;
28
	/** @var MetaService */
29
	private $metaService;
30
	/** @var IUserSession */
31
	private $userSession;
32
33
	/**
34
	 * @param string $AppName
35
	 * @param IRequest $request
36
	 * @param NotesService $service
37
	 * @param IUserSession $userSession
38
	 */
39
	public function __construct(
40
		$AppName,
41
		IRequest $request,
42
		NotesService $service,
43
		MetaService $metaService,
44
		IUserSession $userSession
45
	) {
46
		parent::__construct($AppName, $request);
47
		$this->service = $service;
48
		$this->metaService = $metaService;
49
		$this->userSession = $userSession;
50
	}
51
52
	private function getUID() {
53
		return $this->userSession->getUser()->getUID();
54
	}
55
56
	/**
57
	 * @param Note $note
58
	 * @param string[] $exclude the fields that should be removed from the
59
	 * notes
60
	 * @return Note
61
	 */
62
	private function excludeFields(Note &$note, array $exclude) {
63
		if (count($exclude) > 0) {
64
			foreach ($exclude as $field) {
65
				if (property_exists($note, $field)) {
66
					unset($note->$field);
67
				}
68
			}
69
		}
70
		return $note;
71
	}
72
73
74
	/**
75
	 * @NoAdminRequired
76
	 * @CORS
77
	 * @NoCSRFRequired
78
	 *
79
	 * @param string $exclude
80
	 * @return DataResponse
81
	 */
82
	public function index($exclude = '', $pruneBefore = 0) {
83
		$exclude = explode(',', $exclude);
84
		$now = new \DateTime(); // this must be before loading notes if there are concurrent changes possible
85
		$notes = $this->service->getAll($this->getUID());
86
		$metas = $this->metaService->updateAll($this->getUID(), $notes);
87
		foreach ($notes as $note) {
88
			$lastUpdate = $metas[$note->getId()]->getLastUpdate();
89
			if ($pruneBefore && $lastUpdate<$pruneBefore) {
90
				$vars = get_object_vars($note);
91
				unset($vars['id']);
92
				$this->excludeFields($note, array_keys($vars));
93
			} else {
94
				$this->excludeFields($note, $exclude);
95
			}
96
		}
97
		$etag = md5(json_encode($notes));
98
		if ($this->request->getHeader('If-None-Match') === '"'.$etag.'"') {
99
			return new DataResponse([], Http::STATUS_NOT_MODIFIED);
100
		}
101
		return (new DataResponse($notes))
102
			->setLastModified($now)
103
			->setETag($etag);
104
	}
105
106
107
	/**
108
	 * @NoAdminRequired
109
	 * @CORS
110
	 * @NoCSRFRequired
111
	 *
112
	 * @param int $id
113
	 * @param string $exclude
114
	 * @return DataResponse
115
	 */
116
	public function get($id, $exclude = '') {
117
		try {
118
			$exclude = explode(',', $exclude);
119
			$note = $this->service->get($id, $this->getUID());
120
			$note = $this->excludeFields($note, $exclude);
121
			return new DataResponse($note);
122
		} catch (NoteDoesNotExistException $e) {
123
			return new DataResponse([], Http::STATUS_NOT_FOUND);
124
		}
125
	}
126
127
128
	/**
129
	 * @NoAdminRequired
130
	 * @CORS
131
	 * @NoCSRFRequired
132
	 *
133
	 * @param string $content
134
	 * @param string $category
135
	 * @param int $modified
136
	 * @param boolean $favorite
137
	 * @return DataResponse
138
	 */
139
	public function create($content, $category = null, $modified = 0, $favorite = null) {
140
		try {
141
			$note = $this->service->create($this->getUID());
142
			try {
143
				$note = $this->updateData($note->getId(), $content, $category, $modified, $favorite);
144
			} catch (\Throwable $e) {
145
				// roll-back note creation
146
				$this->service->delete($note->getId(), $this->getUID());
147
				throw $e;
148
			}
149
			return new DataResponse($note);
150
		} catch (InsufficientStorageException $e) {
151
			return new DataResponse([], Http::STATUS_INSUFFICIENT_STORAGE);
152
		}
153
	}
154
155
156
	/**
157
	 * @NoAdminRequired
158
	 * @CORS
159
	 * @NoCSRFRequired
160
	 *
161
	 * @param int $id
162
	 * @param string $content
163
	 * @param string $category
164
	 * @param int $modified
165
	 * @param boolean $favorite
166
	 * @return DataResponse
167
	 */
168
	public function update($id, $content = null, $category = null, $modified = 0, $favorite = null) {
169
		try {
170
			$note = $this->updateData($id, $content, $category, $modified, $favorite);
171
			return new DataResponse($note);
172
		} catch (NoteDoesNotExistException $e) {
173
			return new DataResponse([], Http::STATUS_NOT_FOUND);
174
		} catch (InsufficientStorageException $e) {
175
			return new DataResponse([], Http::STATUS_INSUFFICIENT_STORAGE);
176
		}
177
	}
178
179
	/**
180
	 * Updates a note, used by create and update
181
	 * @param int $id
182
	 * @param string|null $content
183
	 * @param int $modified
184
	 * @param boolean|null $favorite
185
	 * @return Note
186
	 */
187
	private function updateData($id, $content, $category, $modified, $favorite) {
188
		if ($favorite!==null) {
189
			$this->service->favorite($id, $favorite, $this->getUID());
190
		}
191
		if ($content===null) {
192
			return $this->service->get($id, $this->getUID());
193
		} else {
194
			return $this->service->update($id, $content, $this->getUID(), $category, $modified);
195
		}
196
	}
197
198
	/**
199
	 * @NoAdminRequired
200
	 * @CORS
201
	 * @NoCSRFRequired
202
	 *
203
	 * @param int $id
204
	 * @return DataResponse
205
	 */
206
	public function destroy($id) {
207
		try {
208
			$this->service->delete($id, $this->getUID());
209
			return new DataResponse([]);
210
		} catch (NoteDoesNotExistException $e) {
211
			return new DataResponse([], Http::STATUS_NOT_FOUND);
212
		}
213
	}
214
}
215