From 1533469441ea0364e8f51cb0618aea4604e94f1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Thu, 18 Jul 2024 13:40:49 +0200 Subject: [PATCH] feat: add POST watchlist endpoint --- src/Controller/MeController.php | 9 +--- src/Controller/WatchListController.php | 59 ++++++++++++++++++++++++++ src/Entity/Domain.php | 4 +- src/Entity/WatchList.php | 24 ++++++++--- 4 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 src/Controller/WatchListController.php diff --git a/src/Controller/MeController.php b/src/Controller/MeController.php index 8aac5ea..f90f9f1 100644 --- a/src/Controller/MeController.php +++ b/src/Controller/MeController.php @@ -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(); } } \ No newline at end of file diff --git a/src/Controller/WatchListController.php b/src/Controller/WatchListController.php new file mode 100644 index 0000000..0fcb621 --- /dev/null +++ b/src/Controller/WatchListController.php @@ -0,0 +1,59 @@ + 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; + + } + +} \ No newline at end of file diff --git a/src/Entity/Domain.php b/src/Entity/Domain.php index aa9618d..5368dc6 100644 --- a/src/Entity/Domain.php +++ b/src/Entity/Domain.php @@ -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; /** diff --git a/src/Entity/WatchList.php b/src/Entity/WatchList.php index 38fc9ef..ef18f06 100644 --- a/src/Entity/WatchList.php +++ b/src/Entity/WatchList.php @@ -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()