forked from nextcloud/polls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollApiController.php
More file actions
206 lines (189 loc) · 6.56 KB
/
PollApiController.php
File metadata and controls
206 lines (189 loc) · 6.56 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?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\Db\Poll;
use OCA\Polls\Service\CommentService;
use OCA\Polls\Service\OptionService;
use OCA\Polls\Service\PollService;
use OCA\Polls\Service\ShareService;
use OCA\Polls\Service\SubscriptionService;
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
* @psalm-import-type PollsPoll from \OCA\Polls\ResponseDefinitions
* */
class PollApiController extends BaseApiV2Controller {
public function __construct(
string $appName,
IRequest $request,
private CommentService $commentService,
private PollService $pollService,
private OptionService $optionService,
private ShareService $shareService,
private SubscriptionService $subscriptionService,
private VoteService $voteService,
) {
parent::__construct($appName, $request);
}
/**
* Get list of polls
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'GET', url: '/api/v1.0/polls', requirements: ['apiVersion' => '(v2)'])]
public function list(): DataResponse {
return $this->response(fn () => ['polls' => $this->pollService->list()]);
}
/**
* get complete poll
* @param int $pollId Poll id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'GET', url: '/api/v1.0/poll/{pollId}', requirements: ['apiVersion' => '(v2)'])]
public function get(int $pollId): DataResponse {
return $this->response(fn () => [
'poll' => $this->pollService->get($pollId),
'options' => $this->optionService->list($pollId),
'votes' => $this->voteService->list($pollId),
'comments' => $this->commentService->list($pollId),
'shares' => $this->shareService->list($pollId),
'subscribed' => $this->subscriptionService->get($pollId),
]);
}
/**
* Add poll
* @param string $title Poll title
* @param string $type Poll type ('datePoll', 'textPoll')
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'POST', url: '/api/v1.0/poll', requirements: ['apiVersion' => '(v2)'])]
public function add(string $type, string $title, string $votingVariant = Poll::VARIANT_SIMPLE): DataResponse {
return $this->response(fn () => ['poll' => $this->pollService->add($type, $title, $votingVariant)], Http::STATUS_CREATED);
}
/**
* Update poll configuration
* @param int $pollId Poll id
* @param array $pollConfiguration poll config
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/poll/{pollId}', requirements: ['apiVersion' => '(v2)'])]
public function update(int $pollId, array $pollConfiguration): DataResponse {
return $this->response(fn () => ['poll' => $this->pollService->update($pollId, $pollConfiguration)]);
}
/**
* Switch deleted status (move to deleted polls)
* @param int $pollId Poll id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/poll/{pollId}/archive/toggle', requirements: ['apiVersion' => '(v2)'])]
public function toggleArchive(int $pollId): DataResponse {
return $this->response(fn () => ['poll' => $this->pollService->toggleArchive($pollId)]);
}
/**
* Close poll
* @param int $pollId Poll id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/poll/{pollId}/close', requirements: ['apiVersion' => '(v2)'])]
public function close(int $pollId): DataResponse {
return $this->response(fn () => ['poll' => $this->pollService->close($pollId)]);
}
/**
* Reopen poll
* @param int $pollId Poll id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/poll/{pollId}/reopen', requirements: ['apiVersion' => '(v2)'])]
public function reopen(int $pollId): DataResponse {
return $this->response(fn () => ['poll' => $this->pollService->reopen($pollId)]);
}
/**
* Delete poll
* @param int $pollId Poll id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'DELETE', url: '/api/v1.0/poll/{pollId}', requirements: ['apiVersion' => '(v2)'])]
public function delete(int $pollId): DataResponse {
return $this->response(fn () => ['poll' => $this->pollService->delete($pollId)]);
}
/**
* Clone poll
* @param int $pollId Poll id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'POST', url: '/api/v1.0/poll/{pollId}/clone', requirements: ['apiVersion' => '(v2)'])]
public function clone(int $pollId): DataResponse {
return $this->response(fn () => ['poll' => $this->pollService->clone($pollId)], Http::STATUS_CREATED);
}
/**
* Transfer all polls from one user to another (change owner of poll)
* @param string $sourceUser User to transfer polls from
* @param string $targetUser User to transfer polls to
*/
#[CORS]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/poll/transfer/{sourceUser}/{targetUser}', requirements: ['apiVersion' => '(v2)'])]
public function transferPolls(string $sourceUser, string $targetUser): DataResponse {
return $this->response(fn () => ['transferred' => $this->pollService->transferPolls($sourceUser, $targetUser)]);
}
/**
* Transfer single poll to another user (change owner of poll)
* @param int $pollId Poll to transfer
* @param string $targetUser User to transfer the poll to
*/
#[CORS]
#[NoCSRFRequired]
#[ApiRoute(verb: 'PUT', url: '/api/v1.0/poll/{pollId}/transfer/{targetUser}', requirements: ['apiVersion' => '(v2)'])]
public function transferPoll(int $pollId, string $targetUser): DataResponse {
return $this->response(fn () => ['transferred' => $this->pollService->transferPoll($pollId, $targetUser)]);
}
/**
* Collect email addresses from particitipants
* @param int $pollId Poll id
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'GET', url: '/api/v1.0/poll/{pollId}/addresses', requirements: ['apiVersion' => '(v2)'])]
public function getParticipantsEmailAddresses(int $pollId): DataResponse {
return $this->response(fn () => ['addresses' => $this->pollService->getParticipantsEmailAddresses($pollId)]);
}
/**
* Get valid values for configuration options
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
#[ApiRoute(verb: 'GET', url: '/api/v1.0/enum', requirements: ['apiVersion' => '(v2)'])]
public function enum(): DataResponse {
return $this->response(fn () => ['enum' => $this->pollService->getValidEnum()]);
}
}