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

CommentController::list()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
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\ILogger;
31
use OCP\AppFramework\Controller;
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 CommentController extends Controller {
42
43
	private $commentService;
44
45
	/**
46
	 * CommentController constructor.
47
	 * @param string $appName
48
	 * @param IRequest $request
49
	 * @param CommentService $commentService
50
	 */
51
52
	public function __construct(
53
		string $appName,
54
		IRequest $request,
55
		CommentService $commentService
56
	) {
57
		parent::__construct($appName, $request);
58
		$this->commentService = $commentService;
59
	}
60
61
	/**
62
	 * get
63
	 * Read all comments of a poll based on the poll id and return list as array
64
	 * @NoAdminRequired
65
	 * @NoCSRFRequired
66
	 * @param integer $pollId
67
	 * @return DataResponse
68
	 */
69
	public function list($pollId) {
70
		return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK);
71
	}
72
73
	// /**
74
	//  * Read all comments of a poll based on a share token and return list as array
75
	//  * @NoAdminRequired
76
	//  * @NoCSRFRequired
77
	//  * @PublicPage
78
	//  * @param string $token
79
	//  * @return DataResponse
80
	//  */
81
	// public function getByToken($token) {
82
	// 	return new DataResponse($this->commentService->get(0, $token), Http::STATUS_OK);
83
	// }
84
	//
85
	/**
86
	 * Write a new comment to the db and returns the new comment as array
87
	 * @NoAdminRequired
88
	 * @PublicPage
89
	 * @param int $pollId
90
	 * @param string $message
91
	 * @param string $token
92
	 * @return DataResponse
93
	 */
94
	public function add($pollId, $message, $token) {
95
		try {
96
			return new DataResponse($this->commentService->add($pollId, $message, $token), Http::STATUS_OK);
97
		} catch (Exception $e) {
98
			return new DataResponse($e, Http::STATUS_UNAUTHORIZED);
99
		}
100
	}
101
102
	/**
103
	 * Delete Comment
104
	 * @NoAdminRequired
105
	 * @PublicPage
106
	 * @param int $commentId
107
	 * @param string $token
108
	 * @return DataResponse
109
	 */
110
	public function delete($commentId, $token) {
111
		try {
112
			return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK);
113
		} catch (NotAuthorizedException $e) {
114
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
115
		} catch (DoesNotExistException $e) {
116
			return new DataResponse($e, Http::STATUS_OK);
117
		}
118
119
	}
120
121
}
122