Implement api with ability to read snips and profile
This commit is contained in:
39
src/Controller/Api/AbstractApiController.php
Normal file
39
src/Controller/Api/AbstractApiController.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class AbstractApiController extends AbstractController
|
||||
{
|
||||
private function apiResponse(bool $success, array|NormalizableInterface $data): Response
|
||||
{
|
||||
if ($data instanceof NormalizableInterface) {
|
||||
$data = $data->normalize();
|
||||
}
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $key => $value) {
|
||||
if ($value instanceof NormalizableInterface) {
|
||||
$data[$key] = $value->normalize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new JsonResponse([
|
||||
'success' => $success,
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
final protected function errorResponse(string $message): Response
|
||||
{
|
||||
return $this->apiResponse(false, ['message' => $message]);
|
||||
}
|
||||
|
||||
final protected function successResponse(array|NormalizableInterface $data = []): Response
|
||||
{
|
||||
return $this->apiResponse(true, $data);
|
||||
}
|
||||
}
|
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(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
8
src/Controller/Api/NormalizableInterface.php
Normal file
8
src/Controller/Api/NormalizableInterface.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
interface NormalizableInterface
|
||||
{
|
||||
public function normalize(): array;
|
||||
}
|
@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Form\ProfileType;
|
||||
use App\Form\UserSettingsType;
|
||||
use App\Service\LastRelease;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[Route('/user', name: 'user')]
|
||||
class UserController extends AbstractController
|
||||
@ -58,4 +59,20 @@ class UserController extends AbstractController
|
||||
'release' => $lastRelease,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/apikey/generate', name: '_apikey_generate')]
|
||||
public function apiKeyGenerate(): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
|
||||
$apiKey = Uuid::v4()->toBase58();
|
||||
$user->setApiKey($apiKey);
|
||||
$this->em->persist($user);
|
||||
$this->em->flush();
|
||||
|
||||
$this->addFlash('success', sprintf('Successfully generated new api key: "%s"', $apiKey));
|
||||
|
||||
return $this->redirectToRoute('user_profile');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user