Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,4 @@ public function runJanitorJob(): JSONResponse {
public function runNotificationJob(): JSONResponse {
return $this->response(fn () => $this->notificationCron->manuallyRun());
}

/**
* Switch archived status (move to archived polls)
* @param int $pollId poll id
* @deprecated 8.0.0 Not used anymore (use PUT /poll/{pollId}/toggleArchive)
*/
#[FrontpageRoute(verb: 'PUT', url: '/administration/poll/{pollId}/toggleArchive')]
public function toggleArchive(int $pollId): JSONResponse {
return $this->response(fn () => $this->pollService->toggleArchive($pollId));
}

/**
* Delete poll
* @param int $pollId poll id
* @deprecated 8.0.0 Not used anymore (use DELETE /poll/{pollId})
*/
#[FrontpageRoute(verb: 'DELETE', url: '/administration/poll/{pollId}')]
public function delete(int $pollId): JSONResponse {
return $this->responseDeleteTolerant(fn () => $this->pollService->delete($pollId));
}
}
72 changes: 0 additions & 72 deletions lib/Controller/BaseApiV1Controller.php

This file was deleted.

36 changes: 7 additions & 29 deletions lib/Controller/BaseApiV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Closure;
use OCA\Polls\Exceptions\Exception;
use OCA\Polls\Exceptions\NoUpdatesException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
Expand Down Expand Up @@ -39,40 +38,19 @@ public function __construct(
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function response(Closure $callback): DataResponse {
protected function response(Closure $callback, int $successStatus = Http::STATUS_OK): DataResponse {
try {
return new DataResponse($callback());
} catch (DoesNotExistException $e) {
throw new OCSNotFoundException($e->getMessage());
} catch (Exception $e) {
throw new OCSBadRequestException($e->getMessage());
}
}
return new DataResponse($callback(), $successStatus);

/**
* response
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function responseLong(Closure $callback): DataResponse {
try {
return new DataResponse($callback());
} catch (DoesNotExistException $e) {
throw new OCSNotFoundException($e->getMessage());
} catch (NoUpdatesException $e) {
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
}
}

/**
* responseCreate
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function responseCreate(Closure $callback): DataResponse {
try {
return new DataResponse($callback(), Http::STATUS_CREATED);
} catch (Exception $e) {

if ($e->getStatus() === Http::STATUS_NOT_MODIFIED) {
return new DataResponse(statusCode: $e->getStatus());
}

throw new OCSBadRequestException($e->getMessage());
}
}
Expand Down
51 changes: 9 additions & 42 deletions lib/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

use Closure;
use OCA\Polls\Exceptions\Exception;
use OCA\Polls\Exceptions\NoUpdatesException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
Expand All @@ -32,54 +30,23 @@ public function __construct(
/**
* response
* @param Closure $callback Callback function
* @param int $successStatus HTTP status code for success
*/
#[NoAdminRequired]
protected function response(Closure $callback): JSONResponse {
protected function response(
Closure $callback,
int $successStatus = Http::STATUS_OK,
): JSONResponse {
try {
return new JSONResponse($callback());
return new JSONResponse($callback(), $successStatus);
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}

/**
* response
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function responseLong(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback());
} catch (NoUpdatesException $e) {
return new JSONResponse([], Http::STATUS_NOT_MODIFIED);
}
}
if ($e->getStatus() === Http::STATUS_NOT_MODIFIED) {
return new JSONResponse(statusCode: $e->getStatus());
}

/**
* responseCreate
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function responseCreate(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_CREATED);
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}

/**
* responseDeleteTolerant
* @param Closure $callback Callback function
*/
#[NoAdminRequired]
protected function responseDeleteTolerant(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback());
} catch (DoesNotExistException $e) {
return new JSONResponse(['message' => 'Not found, assume already deleted']);
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}
}
68 changes: 0 additions & 68 deletions lib/Controller/BasePublicController.php

This file was deleted.

7 changes: 4 additions & 3 deletions lib/Controller/OptionApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function add(
bool $voteYes = false,
?array $sequence = null,
): DataResponse {
return $this->responseCreate(fn () => array_merge(
return $this->response(fn () => array_merge(
$this->optionService->addWithSequenceAndAutoVote(
$pollId,
SimpleOption::fromArray($option),
Expand All @@ -70,7 +71,7 @@ public function add(
),
['options' => $this->optionService->list($pollId)],
['votes' => $this->voteService->list($pollId)],
));
), Http::STATUS_CREATED);
}


Expand All @@ -84,7 +85,7 @@ public function add(
#[NoCSRFRequired]
#[ApiRoute(verb: 'POST', url: '/api/v1.0/poll/{pollId}/options', requirements: ['apiVersion' => '(v2)'])]
public function addBulk(int $pollId, string $text = ''): DataResponse {
return $this->responseCreate(fn () => ['options' => $this->optionService->addBulk($pollId, $text)]);
return $this->response(fn () => ['options' => $this->optionService->addBulk($pollId, $text)], Http::STATUS_CREATED);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions lib/Controller/OptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function add(
bool $voteYes = false,
?array $sequence = null,
): JSONResponse {
return $this->responseCreate(fn () => array_merge(
return $this->response(fn () => array_merge(
$this->optionService->addWithSequenceAndAutoVote(
$pollId,
SimpleOption::fromArray($option),
Expand All @@ -69,7 +70,8 @@ public function add(
),
['options' => $this->optionService->list($pollId)],
['votes' => $this->voteService->list($pollId)],
));
),
Http::STATUS_CREATED);
}

/**
Expand All @@ -80,7 +82,7 @@ public function add(
#[NoAdminRequired]
#[FrontpageRoute(verb: 'POST', url: '/option/bulk')]
public function addBulk(int $pollId, string $text = ''): JSONResponse {
return $this->responseCreate(fn () => ['options' => $this->optionService->addBulk($pollId, $text)]);
return $this->response(fn () => ['options' => $this->optionService->addBulk($pollId, $text)], Http::STATUS_CREATED);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/Controller/PollApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function get(int $pollId): DataResponse {
#[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->responseCreate(fn () => ['poll' => $this->pollService->add($type, $title, $votingVariant)]);
return $this->response(fn () => ['poll' => $this->pollService->add($type, $title, $votingVariant)], Http::STATUS_CREATED);
}

/**
Expand Down Expand Up @@ -153,7 +154,7 @@ public function delete(int $pollId): DataResponse {
#[NoCSRFRequired]
#[ApiRoute(verb: 'POST', url: '/api/v1.0/poll/{pollId}/clone', requirements: ['apiVersion' => '(v2)'])]
public function clone(int $pollId): DataResponse {
return $this->responseCreate(fn () => ['poll' => $this->pollService->clone($pollId)]);
return $this->response(fn () => ['poll' => $this->pollService->clone($pollId)], Http::STATUS_CREATED);
}

/**
Expand Down
Loading