feat: add POST watchlist endpoint

This commit is contained in:
Maël Gangloff
2024-07-18 13:40:49 +02:00
parent 2efc3da018
commit 1533469441
4 changed files with 81 additions and 15 deletions

View File

@@ -3,21 +3,14 @@
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\User\UserInterface;
class MeController extends AbstractController
{
public function __construct(
private readonly Security $security
)
{
}
public function __invoke(): UserInterface
{
return $this->security->getUser();
return $this->getUser();
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\WatchList;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;
class WatchListController extends AbstractController
{
public function __construct(
private readonly SerializerInterface $serializer, private readonly EntityManagerInterface $em
)
{
}
#[Route(
path: '/api/watchlists',
name: 'watchlist_get_all_mine',
defaults: [
'_api_resource_class' => WatchList::class,
'_api_operation_name' => 'get_all_mine',
],
methods: ['GET']
)]
public function getWatchLists(): Collection
{
/** @var User $user */
$user = $this->getUser();
return $user->getWatchLists();
}
#[Route(
path: '/api/watchlists',
name: 'watchlist_create',
defaults: [
'_api_resource_class' => WatchList::class,
'_api_operation_name' => 'create',
],
methods: ['POST']
)]
public function createWatchList(Request $request): WatchList
{
$watchList = $this->serializer->deserialize($request->getContent(), WatchList::class, 'json', ['groups' => 'watchlist:create']);
$watchList->setUser($this->getUser());
$this->em->persist($watchList);
$this->em->flush();
return $watchList;
}
}

View File

@@ -58,11 +58,11 @@ class Domain
{
#[ORM\Id]
#[ORM\Column(length: 255)]
#[Groups(['domain:item', 'domain:list'])]
#[Groups(['domain:item', 'domain:list', 'watchlist:item', 'watchlist:create'])]
private ?string $ldhName = null;
#[ORM\Column(length: 255)]
#[Groups(['domain:item', 'domain:list'])]
#[Groups(['domain:item', 'domain:list', 'watchlist:item'])]
private ?string $handle = null;
/**

View File

@@ -3,6 +3,9 @@
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Repository\WatchListRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
@@ -13,15 +16,26 @@ use Symfony\Component\Uid\Uuid;
#[ORM\Entity(repositoryClass: WatchListRepository::class)]
#[ApiResource(
shortName: 'Watchlist',
normalizationContext: ['groups' => 'watchlist:item', 'domain:list'],
denormalizationContext: ['groups' => 'watchlist:item', 'domain:list'],
paginationEnabled: false
operations: [
new GetCollection(
routeName: 'watchlist_get_all_mine', normalizationContext: ['groups' => 'watchlist:list'],
name: 'get_all_mine',
),
new Get(
normalizationContext: ['groups' => 'watchlist:item']
),
new Post(
routeName: 'watchlist_create', normalizationContext: ['groups' => 'watchlist:list'],
denormalizationContext: ['groups' => 'watchlist:create'],
name: 'create'
)
],
)]
class WatchList
{
#[ORM\Id]
#[ORM\Column(length: 36)]
#[Groups(['watchlist:item'])]
#[Groups(['watchlist:item', 'watchlist:list'])]
private string $token;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'watchLists')]
@@ -35,7 +49,7 @@ class WatchList
#[ORM\JoinTable(name: 'watch_lists_domains',
joinColumns: [new ORM\JoinColumn(name: 'watch_list_token', referencedColumnName: 'token')],
inverseJoinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name')])]
#[Groups(['watchlist:item'])]
#[Groups(['watchlist:item', 'watchlist:create'])]
private Collection $domains;
public function __construct()