Skip to content

Commit

Permalink
Create/Update/Delete Schedule (#80)
Browse files Browse the repository at this point in the history
* Add delete controller

* Update/Delete schedules

* Remove unused method

* Rename method

* Consistency check

* Finalize
  • Loading branch information
mattamon authored May 29, 2024
1 parent 0bb0619 commit a7068f7
Show file tree
Hide file tree
Showing 13 changed files with 653 additions and 9 deletions.
31 changes: 31 additions & 0 deletions src/Exception/DatabaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Exception;

/**
* @internal
*/
final class DatabaseException extends AbstractApiException
{
public function __construct()
{
parent::__construct(
500,
'A database error occurred.'
);
}
}
34 changes: 34 additions & 0 deletions src/Schedule/Attributes/Request/ElementScheduleRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Schedule\Attributes\Request;

use Attribute;
use OpenApi\Attributes\RequestBody;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Content\ItemsJson;
use Pimcore\Bundle\StudioBackendBundle\Schedule\Schema\UpdateSchedule;

#[Attribute(Attribute::TARGET_METHOD)]
final class ElementScheduleRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: true,
content: new ItemsJson(UpdateSchedule::class)
);
}
}
74 changes: 74 additions & 0 deletions src/Schedule/Controller/DeleteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Schedule\Controller;

use OpenApi\Attributes\Delete;
use OpenApi\Attributes\Schema;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\IdJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Schedule\Service\ScheduleServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class DeleteController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly ScheduleServiceInterface $scheduleService
) {
parent::__construct($serializer);
}

/**
* @throws ElementNotFoundException|DatabaseException
*/
#[Route('/schedules/{id}', name: 'pimcore_studio_api_delete_schedule', methods: ['DELETE'])]
//#[IsGranted('STUDIO_API')]
#[Delete(
path: self::API_PATH . '/schedules/{id}',
operationId: 'deleteSchedule',
summary: 'Delete schedule with given id',
security: self::SECURITY_SCHEME,
tags: [Tags::Schedule->name]
)]
#[IdParameter(type: 'schedule', schema: new Schema(type: 'integer', example: 123))]
#[SuccessResponse(
description: 'Id of deleted schedule',
content: new IdJson('ID of deleted schedule')
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
public function deleteSchedule(int $id): JsonResponse
{
$this->scheduleService->deleteSchedule($id);

return $this->jsonResponse(['id' => $id]);
}
}
8 changes: 2 additions & 6 deletions src/Schedule/Controller/Element/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Note\Attributes\Response\Property\NoteCollection;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Content\ItemsJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\CollectionJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
Expand Down Expand Up @@ -60,7 +58,7 @@ public function __construct(
#[ElementTypeParameter]
#[IdParameter(type: 'element')]
#[SuccessResponse(
description: 'Paginated schedules',
description: 'List of schedules',
content: new ItemsJson(Schedule::class)
)]
#[DefaultResponses([
Expand All @@ -72,8 +70,6 @@ public function getSchedules(
int $id
): JsonResponse
{
$tasks = $this->scheduleService->listSchedules($elementType, $id);

return $this->jsonResponse(['items' => $tasks]);
return $this->jsonResponse(['items' => $this->scheduleService->listSchedules($elementType, $id)]);
}
}
73 changes: 73 additions & 0 deletions src/Schedule/Controller/Element/CreateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Schedule\Controller\Element;

use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\ElementNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\NotAuthorizedException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Schedule\Schema\Schedule;
use Pimcore\Bundle\StudioBackendBundle\Schedule\Service\ScheduleServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class CreateController extends AbstractApiController
{
public function __construct(
SerializerInterface $serializer,
private readonly ScheduleServiceInterface $scheduleService
) {
parent::__construct($serializer);
}

/**
* @throws NotAuthorizedException|ElementNotFoundException
*/
#[Route('/schedules/{elementType}/{id}', name: 'pimcore_studio_api_create_schedule', methods: ['POST'])]
//#[IsGranted('STUDIO_API')]
#[POST(
path: self::API_PATH . '/schedules/{elementType}/{id}',
operationId: 'createSchedule',
summary: 'Create schedule for element',
security: self::SECURITY_SCHEME,
tags: [Tags::Schedule->name]
)]
#[ElementTypeParameter]
#[IdParameter(type: 'element')]
#[SuccessResponse(
description: 'Created schedule',
content: new JsonContent(ref: Schedule::class)
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
])]
public function createSchedule(string $elementType, int $id): JsonResponse
{
return $this->jsonResponse($this->scheduleService->createSchedule($elementType, $id));
}
}
86 changes: 86 additions & 0 deletions src/Schedule/Controller/Element/UpdateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Schedule\Controller\Element;

use OpenApi\Attributes\Put;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Content\ItemsJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\ElementTypeParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Schedule\Attributes\Request\ElementScheduleRequestBody;
use Pimcore\Bundle\StudioBackendBundle\Schedule\Request\UpdateElementSchedules;
use Pimcore\Bundle\StudioBackendBundle\Schedule\Schema\Schedule;
use Pimcore\Bundle\StudioBackendBundle\Schedule\Service\ScheduleServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constants\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\PaginatedResponseTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class UpdateController extends AbstractApiController
{
use PaginatedResponseTrait;

public function __construct(
SerializerInterface $serializer,
private readonly ScheduleServiceInterface $scheduleService
)
{
parent::__construct($serializer);
}

/**
* @throws DatabaseException
*/
#[Route('/schedules/{elementType}/{id}', name: 'pimcore_studio_api_update_schedules', methods: ['PUT'])]
#[Put(
path: self::API_PATH . '/schedules/{elementType}/{id}',
operationId: 'updateSchedulesForElementByTypeAndId',
summary: 'Update schedules for an element',
security: self::SECURITY_SCHEME,
tags: [Tags::Schedule->name]
)]
#[ElementTypeParameter]
#[IdParameter(type: 'element')]
#[ElementScheduleRequestBody]
#[SuccessResponse(
description: 'List of schedules',
content: new ItemsJson(Schedule::class)
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND
])]
public function updateSchedules(
string $elementType,
int $id,
#[MapRequestPayload] UpdateElementSchedules $updateElementSchedules
): JsonResponse
{
$this->scheduleService->updateSchedules($elementType, $id, $updateElementSchedules);

return $this->jsonResponse(['items' => $this->scheduleService->listSchedules($elementType, $id)]);
}
}
Loading

0 comments on commit a7068f7

Please sign in to comment.