Implement api with ability to read snips and profile
This commit is contained in:
43
src/Controller/Api/ApiController.php
Normal file
43
src/Controller/Api/ApiController.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\Entity\Snip;
|
||||
use App\Entity\User;
|
||||
use App\Security\Voter\SnipVoter;
|
||||
use App\Service\SnipContent\SnipContentService;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/api')]
|
||||
class ApiController extends AbstractApiController
|
||||
{
|
||||
#[Route('/me')]
|
||||
public function me(): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
|
||||
return $this->successResponse([
|
||||
'id' => $user->getId(),
|
||||
'name' => $user->getName(),
|
||||
'email' => $user->getEmail(),
|
||||
'apiKey' => $user->getApiKey(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/snip/{snip}')]
|
||||
public function getSnip(Snip $snip, SnipContentService $cs): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted(SnipVoter::VIEW, $snip);
|
||||
|
||||
return $this->successResponse([
|
||||
'id' => $snip->getId(),
|
||||
'content' => $cs->getActiveText($snip),
|
||||
'createdBy' => [
|
||||
'id' => $snip->getCreatedBy()->getId(),
|
||||
'name' => $snip->getCreatedBy()->getName(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user