forked from nextcloud/polls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionController.php
More file actions
176 lines (161 loc) · 5.27 KB
/
Copy pathOptionController.php
File metadata and controls
176 lines (161 loc) · 5.27 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
<?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\CalendarService;
use OCA\Polls\Service\OptionService;
use OCA\Polls\Service\VoteService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
/**
* @psalm-api
*/
class OptionController extends BaseController {
public function __construct(
string $appName,
IRequest $request,
private OptionService $optionService,
private CalendarService $calendarService,
private VoteService $voteService,
) {
parent::__construct($appName, $request);
}
/**
* Get all options of given poll
* @param int $pollId Poll id
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'GET', url: '/poll/{pollId}/options')]
public function list(int $pollId): JSONResponse {
return $this->response(function () use ($pollId) {
return ['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 new options
* @param bool $voteYes vote yes
* @return JSONResponse
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'POST', url: '/poll/{pollId}/option')]
public function add(
int $pollId,
array $option,
bool $voteYes = false,
?array $sequence = null,
): JSONResponse {
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
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'POST', url: '/option/bulk')]
public function addBulk(int $pollId, string $text = ''): JSONResponse {
return $this->response(fn () => ['options' => $this->optionService->addBulk($pollId, $text)], Http::STATUS_CREATED);
}
/**
* Update option
* @param int $optionId option id
* @param int $timestamp timestamp for datepoll
* @param string $text Option text for text poll
* @param int duration duration of option
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'PUT', url: '/option/{optionId}')]
public function update(int $optionId, int $timestamp, string $text, int $duration): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->update($optionId, $timestamp, $text, $duration)]);
}
/**
* Delete option
* @param int $optionId option id
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'DELETE', url: '/option/{optionId}')]
public function delete(int $optionId): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->delete($optionId)]);
}
/**
* Restore option
* @param int $optionId option id
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'PUT', url: '/option/{optionId}/restore')]
public function restore(int $optionId): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->delete($optionId, true)]);
}
/**
* Switch option confirmation
* @param int $optionId option id
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'PUT', url: '/option/{optionId}/confirm')]
public function confirm(int $optionId): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->confirm($optionId)]);
}
/**
* Reorder options
* @param int $pollId option id
* @param array $options options in new order
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'POST', url: '/poll/{pollId}/options/reorder')]
public function reorder(int $pollId, array $options): JSONResponse {
return $this->response(fn () => ['options' => $this->optionService->reorder($pollId, $options)]);
}
/**
* clone options in date poll
* @param int $optionId clone template
* @param array $sequence sequence of new options
* @param bool $voteYes vote yes
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'POST', url: '/option/{optionId}/sequence')]
public function sequence(int $optionId, array $sequence, bool $voteYes = false): JSONResponse {
return $this->response(fn () => ['options' => $this->optionService->sequence($optionId, Sequence::fromArray($sequence), $voteYes)]);
}
/**
* Shift options
* @param int $pollId poll id
* @param int $step step width
* @param string $unit Unit for shift steps
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'POST', url: '/poll/{pollId}/shift')]
public function shift(int $pollId, int $step, string $unit): JSONResponse {
return $this->response(fn () => ['options' => $this->optionService->shift($pollId, $step, $unit)]);
}
/**
* findCalendarEvents
* @param int $optionId option id
* @param $tz Timezone to use
*/
#[NoAdminRequired]
#[FrontpageRoute(verb: 'GET', url: '/option/{optionId}/events')]
public function findCalendarEvents(int $optionId): JSONResponse {
return $this->response(fn () => ['events' => $this->calendarService->getEvents($optionId)]);
}
}