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

CommentApiController::list()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
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
29
use OCP\IRequest;
30
use \OCP\IURLGenerator;
31
use OCP\AppFramework\ApiController;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
35
use OCA\Polls\Exceptions\NotAuthorizedException;
36
37
use OCA\Polls\Service\CommentService;
38
39
40
41
class CommentApiController extends ApiController {
42
43
	private $commentService;
44
	/**
45
	 * CommentApiController constructor.
46
	 * @param string $appName
47
	 * @param IRequest $request
48
	 * @param CommentService $commentService
49
	 */
50
51
	public function __construct(
52
		string $appName,
53
		IRequest $request,
54
		CommentService $commentService
55
	) {
56
		parent::__construct($appName,
57
			$request,
58
			'POST, GET, DELETE',
59
            'Authorization, Content-Type, Accept',
60
            1728000);
61
		$this->commentService = $commentService;
62
	}
63
64
	/**
65
	 * get
66
	 * Read all comments of a poll based on the poll id and return list as array
67
	 * @NoAdminRequired
68
	 * @CORS
69
	 * @NoCSRFRequired
70
	 * @param integer $pollId
71
	 * @return DataResponse
72
	 */
73
	public function list($pollId) {
74
		try {
75
			return new DataResponse(['comments' => $this->commentService->list($pollId)], Http::STATUS_OK);
76
		} catch (DoesNotExistException $e) {
77
			return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND);
78
		} catch (NotAuthorizedException $e) {
79
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
80
		}
81
	}
82
83
	/**
84
	 * Write a new comment to the db and returns the new comment as array
85
	 * @NoAdminRequired
86
	 * @CORS
87
	 * @NoCSRFRequired
88
	 * @param int $pollId
89
	 * @param string $message
90
	 * @return DataResponse
91
	 */
92
	public function add($pollId, $message) {
93
		try {
94
			return new DataResponse(['comment' => $this->commentService->add($pollId, $message)], Http::STATUS_CREATED);
95
		} catch (DoesNotExistException $e) {
96
			return new DataResponse(['error' => 'Poll with id ' . $pollId . ' not found'], Http::STATUS_NOT_FOUND);
97
		} catch (NotAuthorizedException $e) {
98
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
99
		}
100
	}
101
102
	/**
103
	 * Delete Comment
104
	 * @NoAdminRequired
105
	 * @CORS
106
	 * @NoCSRFRequired
107
	 * @param int $commentId
108
	 * @return DataResponse
109
	 */
110
	public function delete($commentId) {
111
		try {
112
			return new DataResponse(['comment' => $this->commentService->delete($commentId)], Http::STATUS_OK);
113
		} catch (DoesNotExistException $e) {
114
			return new DataResponse(['error' => 'Comment id ' . $commentId . ' does not exist'], Http::STATUS_NOT_FOUND);
115
		} catch (NotAuthorizedException $e) {
116
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
117
		}
118
	}
119
120
}
121