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

VoteApiController::set()   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 3
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\NotAuthorizedException;
29
30
use OCP\IRequest;
31
use OCP\ILogger;
32
use OCP\AppFramework\ApiController;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\DataResponse;
35
36
use OCA\Polls\Service\VoteService;
37
38
class VoteApiController extends ApiController {
39
40
	private $logger;
41
	private $voteService;
42
43
	/**
44
	 * VoteController constructor.
45
	 * @param string $appName
46
	 * @param IRequest $request
47
	 * @param ILogger $logger
48
	 * @param VoteService $voteService
49
	 */
50
	public function __construct(
51
		string $appName,
52
		IRequest $request,
53
		ILogger $logger,
54
		VoteService $voteService
55
	) {
56
		parent::__construct($appName,
57
			$request,
58
			'PUT, GET, DELETE',
59
            'Authorization, Content-Type, Accept',
60
            1728000);
61
		$this->voteService = $voteService;
62
		$this->logger = $logger;
63
	}
64
65
	/**
66
	 * Get all votes of given poll
67
	 * Read all votes of a poll based on the poll id and return list as array
68
	 * @NoAdminRequired
69
	 * @NoCSRFRequired
70
	 * @CORS
71
	 * @param integer $pollId
72
	 * @return DataResponse
73
	 */
74
	public function list($pollId) {
75
		try {
76
			return new DataResponse(['votes' => $this->voteService->list($pollId)], Http::STATUS_OK);
77
		} catch (DoesNotExistException $e) {
78
			return new DataResponse(['error' => 'No votes'], Http::STATUS_NOT_FOUND);
79
		} catch (NotAuthorizedException $e) {
80
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
81
		}
82
	}
83
84
	/**
85
	 * set
86
	 * @NoAdminRequired
87
	 * @NoCSRFRequired
88
	 * @CORS
89
	 * @param integer $pollId
90
	 * @param Array $option
91
	 * @param string $userId
92
	 * @param string $setTo
93
	 * @return DataResponse
94
	 */
95
	public function set($pollId, $pollOptionText, $setTo) {
96
		try {
97
			return new DataResponse(['vote' => $this->voteService->set($pollId, $pollOptionText, $setTo)], Http::STATUS_OK);
98
		} catch (DoesNotExistException $e) {
99
			return new DataResponse(['error' => 'Option not found'], Http::STATUS_NOT_FOUND);
100
		} catch (NotAuthorizedException $e) {
101
			return new DataResponse(['error' => $e->getMessage()], $e->getStatus());
102
		}
103
104
	}
105
}
106