Implement snip api post

This commit is contained in:
Tim 2025-04-27 23:18:20 +02:00
parent f56c78f626
commit 74154e240f
4 changed files with 78 additions and 5 deletions

13
.http
View File

@ -3,7 +3,16 @@ GET {{host}}/api/me
Accept: application/json Accept: application/json
X-AUTH-TOKEN: {{apiKey}} X-AUTH-TOKEN: {{apiKey}}
### api snip ### api snip get
GET {{host}}/api/snip/2 GET {{host}}/api/snip/22
Accept: application/json Accept: application/json
X-AUTH-TOKEN: {{apiKey}} 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"
}

View File

@ -3,3 +3,12 @@ controllers:
path: ../src/Controller/ path: ../src/Controller/
namespace: App\Controller namespace: App\Controller
type: attribute type: attribute
exclude: ../src/Controller/Api/
rest_controllers:
resource:
path: ../src/Controller/Api/
namespace: App\Controller\Api
type: attribute
prefix: /api
defaults: { _format: "json" }

View File

@ -2,17 +2,19 @@
namespace App\Controller\Api; namespace App\Controller\Api;
use App\Dto\SnipPostRequest;
use App\Entity\Snip; use App\Entity\Snip;
use App\Entity\User; use App\Entity\User;
use App\Repository\SnipRepository;
use App\Security\Voter\SnipVoter; use App\Security\Voter\SnipVoter;
use App\Service\SnipContent\SnipContentService; use App\Service\SnipContent\SnipContentService;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
#[Route('/api')]
class ApiController extends AbstractApiController class ApiController extends AbstractApiController
{ {
#[Route('/me')] #[Route('/me', methods: ['GET'])]
public function me(): Response public function me(): Response
{ {
/** @var User $user */ /** @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 public function getSnip(Snip $snip, SnipContentService $cs): Response
{ {
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip); $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(),
],
]);
}
} }

View File

@ -0,0 +1,13 @@
<?php
namespace App\Dto;
class SnipPostRequest
{
public function __construct(
public ?string $name = null,
public ?string $content = null,
public ?bool $public = null,
public ?bool $visible = null,
) {}
}