forked from nextcloud/polls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionApiController.php
More file actions
154 lines (141 loc) · 4.6 KB
/
OptionApiController.php
File metadata and controls
154 lines (141 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Controller;
use OCA\Polls\Model\Sequence;
use OCA\Polls\Model\SimpleOption;
use OCA\Polls\Service\OptionService;
use OCA\Polls\Service\VoteService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\Attribute\CORS;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
/**
* @psalm-api
*/
class OptionApiController extends BaseApiV2Controller {
public function __construct(
string $appName,
IRequest $request,
private OptionService $optionService,
private VoteService $voteService,
) {
parent::__construct($appName, $request);
}
/**
* Get all options of given poll
* @param int $pollId Poll id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'GET', url: '/api/v1.0/poll/{pollId}/options', requirements: ['apiVersion' => '(v2)'])]
public function list(int $pollId): DataResponse {
return $this->response(fn () => ['options' => $this->optionService->list($pollId)]);
}
/**
* Add a new option
* @param int $pollId poll id
* @param array $option Options text for text poll
* @param array $sequence Sequence of the option
* @param bool $voteYes Vote yes
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'POST', url: '/api/v1.0/poll/{pollId}/option', requirements: ['apiVersion' => '(v2)'])]
public function add(
int $pollId,
array $option,
bool $voteYes = false,
?array $sequence = null,
): DataResponse {
return $this->response(fn () => array_merge(
$this->optionService->addWithSequenceAndAutoVote(
$pollId,
SimpleOption::fromArray($option),
$voteYes,
Sequence::fromArray($sequence),
),
['options' => $this->optionService->list($pollId)],
['votes' => $this->voteService->list($pollId)],
), Http::STATUS_CREATED);
}
/**
* Add mulitple new options
* @param int $pollId poll id
* @param string $text Options text for text poll
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'POST', url: '/api/v1.0/poll/{pollId}/options', requirements: ['apiVersion' => '(v2)'])]
public function addBulk(int $pollId, string $text = ''): DataResponse {
return $this->response(fn () => ['options' => $this->optionService->addBulk($pollId, $text)], Http::STATUS_CREATED);
}
/**
* Update option
* @param int $optionId Share token
* @param int $timestamp timestamp for datepoll
* @param string $text Option text for text poll
* @param int duration duration of option
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/option/{optionId}', requirements: ['apiVersion' => '(v2)'])]
public function update(int $optionId, int $timestamp = 0, string $text = '', int $duration = 0): DataResponse {
return $this->response(fn () => ['option' => $this->optionService->update($optionId, $timestamp, $text, $duration)]);
}
/**
* Delete option
* @param int $optionId option id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'DELETE', url: '/api/v1.0/option/{optionId}', requirements: ['apiVersion' => '(v2)'])]
public function delete(int $optionId): DataResponse {
return $this->response(fn () => ['option' => $this->optionService->delete($optionId)]);
}
/**
* Restore option
* @param int $optionId option id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/option/{optionId}/restore', requirements: ['apiVersion' => '(v2)'])]
public function restore(int $optionId): DataResponse {
return $this->response(fn () => ['option' => $this->optionService->delete($optionId, true)]);
}
/**
* Switch option confirmation
* @param int $optionId option id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/option/{optionId}/confirm', requirements: ['apiVersion' => '(v2)'])]
public function confirm(int $optionId): DataResponse {
return $this->response(fn () => ['option' => $this->optionService->confirm($optionId)]);
}
/**
* Set order position for option
* @param int $optionId option id
* @param int $order option's new position
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/option/{optionId}/order/{order}', requirements: ['apiVersion' => '(v2)'])]
public function setOrder(int $optionId, int $order): DataResponse {
return $this->response(fn () => ['option' => $this->optionService->setOrder($optionId, $order)]);
}
}