Completed
Pull Request — master (#966)
by René
04:37
created

PollApiController::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 12
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[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\Polls\Controller;
25
26
 use Exception;
27
 use OCP\AppFramework\Db\DoesNotExistException;
28
 use OCA\Polls\Exceptions\EmptyTitleException;
29
 use OCA\Polls\Exceptions\InvalidAccessException;
30
 use OCA\Polls\Exceptions\InvalidShowResultsException;
31
 use OCA\Polls\Exceptions\InvalidPollTypeException;
32
 use OCA\Polls\Exceptions\NotAuthorizedException;
33
34
 use OCP\IRequest;
35
 use OCP\ILogger;
36
 use OCP\AppFramework\ApiController;
37
 use OCP\AppFramework\Http;
38
 use OCP\AppFramework\Http\DataResponse;
39
40
 use OCA\Polls\Service\PollService;
41
42
 class PollApiController extends ApiController {
43
44
 	private $logger;
45
 	private $pollService;
46
47
 	/**
48
 	 * PollController constructor.
49
 	 * @param string $appName
50
 	 * @param $userId
51
 	 * @param IRequest $request
52
 	 * @param ILogger $logger
53
 	 * @param PollService $pollService
54
 	 */
55
56
 	public function __construct(
57
 		string $appName,
58
 		IRequest $request,
59
 		ILogger $logger,
60
		PollService $pollService
61
 	) {
62
 		parent::__construct($appName, $request);
63
 		$this->logger = $logger;
64
 		$this->pollService = $pollService;
65
 	}
66
67
68
	/**
69
	 * list
70
	 * @NoAdminRequired
71
	 * @NoCSRFRequired
72
	 * @CORS
73
	 * @return DataResponse
74
	 */
75
76
	public function list() {
77
		try {
78
			return new DataResponse(['polls' => $this->pollService->list()], Http::STATUS_OK);
79
		} catch (DoesNotExistException $e) {
80
			return new DataResponse([], Http::STATUS_NOT_FOUND);
81
		} catch (NotAuthorizedException $e) {
82
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
83
		}
84
	}
85
86
87
	/**
88
	 * get
89
	 * @NoAdminRequired
90
	 * @NoCSRFRequired
91
	 * @param integer $pollId
92
	 * @return array
93
	 */
94
 	public function get($pollId) {
95
		try {
96
			return new DataResponse(['poll' => $this->pollService->get($pollId)], Http::STATUS_OK);
97
		} catch (DoesNotExistException $e) {
98
			return new DataResponse(['error' => 'Not found'], Http::STATUS_NOT_FOUND);
99
		} catch (NotAuthorizedException $e) {
100
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
101
		}
102
 	}
103
104
	/**
105
	 * write
106
	 * @NoAdminRequired
107
	 * @NoCSRFRequired
108
	 * @param Array $poll
109
	 * @return DataResponse
110
	 */
111
112
	public function add($type, $title) {
113
		try {
114
			return new DataResponse(['poll' => $this->pollService->add($type, $title)], Http::STATUS_CREATED);
115
		} catch (NotAuthorizedException $e) {
116
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
117
		} catch (InvalidPollTypeException $e) {
118
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
119
		} catch (EmptyTitleException $e) {
120
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
121
		}
122
	}
123
124
	/**
125
	 * write
126
	 * @NoAdminRequired
127
	 * @NoCSRFRequired
128
	 * @param Array $poll
129
	 * @return DataResponse
130
	 */
131
132
	public function update($pollId, $poll) {
133
		try {
134
			return new DataResponse(['poll' => $this->pollService->update($pollId, $poll)], Http::STATUS_OK);
135
		} catch (DoesNotExistException $e) {
136
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
137
		} catch (NotAuthorizedException $e) {
138
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
139
		} catch (InvalidAccessException $e) {
140
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
141
		} catch (InvalidShowResultsException $e) {
142
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
143
		} catch (EmptyTitleException $e) {
144
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
145
		}
146
	}
147
148
	/**
149
	 * delete
150
	 * @NoAdminRequired
151
	 * @NoCSRFRequired
152
	 * @param Array $poll
153
	 * @return DataResponse
154
	 */
155
156
	public function delete($pollId) {
157
		try {
158
			return new DataResponse(['poll' => $this->pollService->delete($pollId)], Http::STATUS_OK);
159
		} catch (DoesNotExistException $e) {
160
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
161
		} catch (NotAuthorizedException $e) {
162
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
163
		}
164
	}
165
166
	/**
167
	 * deletePermanently
168
	 * @NoAdminRequired
169
	 * @NoCSRFRequired
170
	 * @param Array $poll
171
	 * @return DataResponse
172
	 */
173
174
	public function deletePermanently($pollId) {
175
		try {
176
			return new DataResponse(['poll' => $this->pollService->deletePermanently($pollId)], Http::STATUS_OK);
177
		} catch (DoesNotExistException $e) {
178
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
179
		} catch (NotAuthorizedException $e) {
180
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
181
		}
182
183
	}
184
185
	/**
186
	 * clone
187
	 * @NoAdminRequired
188
	 * @NoCSRFRequired
189
	 * @param integer $pollId
190
	 * @return DataResponse
191
	 */
192
	public function clone($pollId) {
193
		try {
194
			return new DataResponse(['poll' => $this->pollService->clone($pollId)], Http::STATUS_CREATED);
195
		} catch (DoesNotExistException $e) {
196
			return new DataResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
197
		} catch (NotAuthorizedException $e) {
198
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
199
		}
200
	}
201
202
	/**
203
	 * enum
204
	 * @NoAdminRequired
205
	 * @NoCSRFRequired
206
	 * @param Array $poll
207
	 * @return DataResponse
208
	 */
209
210
	public function enum() {
211
		return new DataResponse($this->pollService->getValidEnum(), Http::STATUS_OK);
212
	}
213
214
215
}
216