2024-07-18 13:40:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
|
|
use App\Entity\User;
|
|
|
|
|
use App\Entity\WatchList;
|
|
|
|
|
use Doctrine\Common\Collections\Collection;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2024-07-29 16:01:32 +02:00
|
|
|
use Exception;
|
2024-07-18 13:40:49 +02:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 16:01:32 +02:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2024-07-18 13:40:49 +02:00
|
|
|
#[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());
|
2024-07-29 16:01:32 +02:00
|
|
|
|
2024-07-18 13:40:49 +02:00
|
|
|
$this->em->persist($watchList);
|
|
|
|
|
$this->em->flush();
|
|
|
|
|
|
|
|
|
|
return $watchList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|