From 74154e240fc2ca33d9c22ba95319e8e95b604d2b Mon Sep 17 00:00:00 2001 From: Tim Date: Sun, 27 Apr 2025 23:18:20 +0200 Subject: [PATCH] Implement snip api post --- .http | 13 ++++++-- config/routes.yaml | 9 ++++++ src/Controller/Api/ApiController.php | 48 ++++++++++++++++++++++++++-- src/Dto/SnipPostRequest.php | 13 ++++++++ 4 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 src/Dto/SnipPostRequest.php diff --git a/.http b/.http index 0f757cf..2501ca3 100644 --- a/.http +++ b/.http @@ -3,7 +3,16 @@ GET {{host}}/api/me Accept: application/json X-AUTH-TOKEN: {{apiKey}} -### api snip -GET {{host}}/api/snip/2 +### api snip get +GET {{host}}/api/snip/22 Accept: application/json X-AUTH-TOKEN: {{apiKey}} + +### api snip post edit +POST {{host}}/api/snip/22 +Content-Type: application/json +X-AUTH-TOKEN: {{apiKey}} + +{ + "content": "snip api test push number 3 without name" +} diff --git a/config/routes.yaml b/config/routes.yaml index 41ef814..9b68905 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -3,3 +3,12 @@ controllers: path: ../src/Controller/ namespace: App\Controller type: attribute + exclude: ../src/Controller/Api/ + +rest_controllers: + resource: + path: ../src/Controller/Api/ + namespace: App\Controller\Api + type: attribute + prefix: /api + defaults: { _format: "json" } diff --git a/src/Controller/Api/ApiController.php b/src/Controller/Api/ApiController.php index 9a36ffe..7f129c4 100644 --- a/src/Controller/Api/ApiController.php +++ b/src/Controller/Api/ApiController.php @@ -2,17 +2,19 @@ namespace App\Controller\Api; +use App\Dto\SnipPostRequest; use App\Entity\Snip; use App\Entity\User; +use App\Repository\SnipRepository; use App\Security\Voter\SnipVoter; use App\Service\SnipContent\SnipContentService; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; use Symfony\Component\Routing\Attribute\Route; -#[Route('/api')] class ApiController extends AbstractApiController { - #[Route('/me')] + #[Route('/me', methods: ['GET'])] public function me(): Response { /** @var User $user */ @@ -26,7 +28,7 @@ class ApiController extends AbstractApiController ]); } - #[Route('/snip/{snip}')] + #[Route('/snip/{snip}', methods: ['GET'])] public function getSnip(Snip $snip, SnipContentService $cs): Response { $this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip); @@ -40,4 +42,44 @@ class ApiController extends AbstractApiController ], ]); } + + #[Route('/snip/{snip}', methods: ['POST'])] + public function postSnip( + Snip $snip, + #[MapRequestPayload] SnipPostRequest $request, + SnipContentService $cs, + SnipRepository $repo, + ): Response + { + $this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip); + + if (!($snip->getActiveVersion() === $snip->getLatestVersion())) { + return $this->errorResponse('Snip is not the latest version'); + } + + if ($request->name) { + $snip->setName($request->name); + } + if ($request->content) { + $cs->update($snip, $request->content); + } + if ($request->public !== null) { + $snip->setPublic($request->public); + } + if ($request->visible !== null) { + $snip->setVisible($request->visible); + } + + $repo->save($snip); + + return $this->successResponse([ + 'id' => $snip->getId(), + 'name' => $snip->getName(), + 'content' => $cs->getActiveText($snip), + 'createdBy' => [ + 'id' => $snip->getCreatedBy()->getId(), + 'name' => $snip->getCreatedBy()->getName(), + ], + ]); + } } \ No newline at end of file diff --git a/src/Dto/SnipPostRequest.php b/src/Dto/SnipPostRequest.php new file mode 100644 index 0000000..2dc0862 --- /dev/null +++ b/src/Dto/SnipPostRequest.php @@ -0,0 +1,13 @@ +