Completed
Push — master ( 798959...da39f8 )
by René
16s queued 10s
created

CommentController::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 5
ccs 0
cts 5
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
use OCP\AppFramework\Db\DoesNotExistException;
28
use OCA\Polls\Exceptions\NotAuthorizedException;
29
30
use OCP\IRequest;
31
use OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http;
33
use OCP\AppFramework\Http\DataResponse;
34
35
use OCA\Polls\Service\CommentService;
36
37
class CommentController extends Controller {
38
39
	/** @var CommentService */
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
	//  * Read all comments of a poll based on the poll id and return list as array
60
	//  * @NoAdminRequired
61
	//  * @param int $pollId
62
	//  * @param string $token
63
	//  * @return DataResponse
64
	//  */
65
	// public function list($pollId) {
66
	// 	return new DataResponse($this->commentService->list($pollId), Http::STATUS_OK);
67
	// }
68
	//
69
	/**
70
	 * Write a new comment to the db and returns the new comment as array
71
	 * @NoAdminRequired
72
	 * @PublicPage
73
	 * @param int $pollId
74
	 * @param string $message
75
	 * @param string $token
76
	 * @return DataResponse
77
	 */
78
	public function add($pollId, $message, $token) {
79
		try {
80
			return new DataResponse($this->commentService->add($pollId, $message, $token), Http::STATUS_OK);
81
		} catch (Exception $e) {
82
			return new DataResponse($e, Http::STATUS_UNAUTHORIZED);
83
		}
84
	}
85
86
	/**
87
	 * Delete Comment
88
	 * @NoAdminRequired
89
	 * @PublicPage
90
	 * @param int $commentId
91
	 * @param string $token
92
	 * @return DataResponse
93
	 */
94
	public function delete($commentId, $token) {
95
		try {
96
			return new DataResponse($this->commentService->delete($commentId, $token), Http::STATUS_OK);
97
		} catch (NotAuthorizedException $e) {
98
			return new DataResponse($e, Http::STATUS_FORBIDDEN);
99
		} catch (DoesNotExistException $e) {
100
			return new DataResponse($e, Http::STATUS_OK);
101
		}
102
	}
103
}
104