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

CommentController::deleteByToken()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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