Implement snip api post
This commit is contained in:
parent
f56c78f626
commit
74154e240f
13
.http
13
.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"
|
||||
}
|
||||
|
@ -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" }
|
||||
|
@ -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(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
13
src/Dto/SnipPostRequest.php
Normal file
13
src/Dto/SnipPostRequest.php
Normal 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,
|
||||
) {}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user