feat: update Watchlist

This commit is contained in:
Maël Gangloff
2024-08-15 03:04:31 +02:00
parent 5dd27a4b7b
commit 7d16d836f4
7 changed files with 177 additions and 79 deletions

View File

@@ -10,6 +10,7 @@ use App\Entity\WatchList;
use App\Repository\WatchListRepository;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\ORMException;
use Eluceo\iCal\Domain\Entity\Attendee;
use Eluceo\iCal\Domain\Entity\Calendar;
use Eluceo\iCal\Domain\Entity\Event;
@@ -44,7 +45,36 @@ class WatchListController extends AbstractController
) {
}
public function verifyLimitations(WatchList $watchList)
/**
* @throws \Exception
*/
#[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']);
$this->verifyLimitations($watchList);
$user = $this->getUser();
$this->logger->info('User {username} registers a Watchlist ({token}).', [
'username' => $user->getUserIdentifier(),
'token' => $watchList->getToken(),
]);
$this->em->persist($watchList);
$this->em->flush();
return $watchList;
}
public function verifyLimitations(WatchList $watchList): void
{
/** @var User $user */
$user = $this->getUser();
@@ -87,35 +117,26 @@ class WatchListController extends AbstractController
}
}
/**
* @throws \Exception
*/
#[Route(
path: '/api/watchlists',
name: 'watchlist_create',
name: 'watchlist_get_all_mine',
defaults: [
'_api_resource_class' => WatchList::class,
'_api_operation_name' => 'create',
'_api_operation_name' => 'get_all_mine',
],
methods: ['POST']
methods: ['GET']
)]
public function createWatchList(Request $request): WatchList
public function getWatchLists(): Collection
{
$watchList = $this->serializer->deserialize($request->getContent(), WatchList::class, 'json', ['groups' => 'watchlist:create']);
$this->verifyLimitations($watchList);
/** @var User $user */
$user = $this->getUser();
$this->logger->info('User {username} registers a Watchlist ({token}).', [
'username' => $user->getUserIdentifier(),
'token' => $watchList->getToken(),
]);
$this->em->persist($watchList);
$this->em->flush();
return $watchList;
return $user->getWatchLists();
}
/**
* @throws ORMException
*/
#[Route(
path: '/api/watchlists/{token}',
name: 'watchlist_update',
@@ -123,19 +144,23 @@ class WatchListController extends AbstractController
'_api_resource_class' => WatchList::class,
'_api_operation_name' => 'update',
],
methods: ['PATCH']
methods: ['PUT']
)]
public function patchWatchList(Request $request): WatchList
public function putWatchList(WatchList $watchList): WatchList
{
$watchList = $this->serializer->deserialize($request->getContent(), WatchList::class, 'json', ['groups' => 'watchlist:create']);
/** @var User $user */
$user = $this->getUser();
$this->verifyLimitations($watchList);
$user = $this->getUser();
$this->logger->info('User {username} updates a Watchlist ({token}).', [
'username' => $user->getUserIdentifier(),
'token' => $watchList->getToken(),
]);
$this->em->remove($this->em->getReference(WatchList::class, $watchList->getToken()));
$this->em->flush();
$this->em->persist($watchList);
$this->em->flush();
@@ -202,21 +227,4 @@ class WatchListController extends AbstractController
'Content-Type' => 'text/calendar; charset=utf-8',
]);
}
#[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();
}
}

View File

@@ -6,8 +6,8 @@ use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use App\Controller\WatchListController;
use App\Repository\WatchListRepository;
use Doctrine\Common\Collections\ArrayCollection;
@@ -67,10 +67,11 @@ use Symfony\Component\Uid\Uuid;
denormalizationContext: ['groups' => 'watchlist:create'],
name: 'create'
),
new Patch(
new Put(
routeName: 'watchlist_update',
normalizationContext: ['groups' => 'watchlist:item'],
denormalizationContext: ['groups' => 'watchlist:create'],
denormalizationContext: ['groups' => ['watchlist:create', 'watchlist:token']],
security: 'object.user == user',
name: 'update'
),
new Delete(),
@@ -83,7 +84,7 @@ class WatchList
public ?User $user = null;
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
#[Groups(['watchlist:item', 'watchlist:list'])]
#[Groups(['watchlist:item', 'watchlist:list', 'watchlist:token'])]
private string $token;
/**
* @var Collection<int, Domain>
@@ -128,6 +129,11 @@ class WatchList
return $this->token;
}
public function setToken(string $token): void
{
$this->token = $token;
}
public function getUser(): ?User
{
return $this->user;

View File

@@ -16,7 +16,7 @@ class WatchListTrigger
private ?string $event = null;
#[ORM\Id]
#[ORM\ManyToOne(targetEntity: WatchList::class, inversedBy: 'watchListTriggers')]
#[ORM\ManyToOne(targetEntity: WatchList::class, cascade: ['persist'], inversedBy: 'watchListTriggers')]
#[ORM\JoinColumn(referencedColumnName: 'token', nullable: false, onDelete: 'CASCADE')]
private ?WatchList $watchList = null;