feat: add watchlist patch with limitations

This commit is contained in:
Maël Gangloff
2024-08-14 23:23:32 +02:00
parent d41071a052
commit 5dd27a4b7b
4 changed files with 82 additions and 32 deletions

View File

@@ -1,4 +1,4 @@
import {Event, request, Watchlist} from "./index";
import {request, Watchlist} from "./index";
export async function getWatchlists() {
const response = await request({
@@ -32,3 +32,15 @@ export async function deleteWatchlist(token: string): Promise<void> {
url: 'watchlists/' + token
})
}
export async function patchWatchlist(watchlist: Partial<Watchlist> & { token: string }) {
const response = await request<Watchlist>({
method: 'PATCH',
url: 'watchlists/' + watchlist.token,
data: watchlist,
headers: {
"Content-Type": 'application/merge-patch+json'
}
})
return response.data
}

View File

@@ -44,22 +44,8 @@ class WatchListController extends AbstractController
) {
}
/**
* @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
public function verifyLimitations(WatchList $watchList)
{
$watchList = $this->serializer->deserialize($request->getContent(), WatchList::class, 'json', ['groups' => 'watchlist:create']);
/** @var User $user */
$user = $this->getUser();
$watchList->setUser($user);
@@ -99,8 +85,53 @@ class WatchListController extends AbstractController
}
}
}
}
$this->logger->info('User {username} register a Watchlist ({token}).', [
/**
* @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;
}
#[Route(
path: '/api/watchlists/{token}',
name: 'watchlist_update',
defaults: [
'_api_resource_class' => WatchList::class,
'_api_operation_name' => 'update',
],
methods: ['PATCH']
)]
public function patchWatchList(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} updates a Watchlist ({token}).', [
'username' => $user->getUserIdentifier(),
'token' => $watchList->getToken(),
]);

View File

@@ -27,7 +27,16 @@ use Symfony\Component\Uid\Uuid;
name: 'get_all_mine',
),
new Get(
normalizationContext: ['groups' => 'watchlist:item'],
normalizationContext: ['groups' => [
'watchlist:item',
'domain:item',
'event:list',
'domain-entity:entity',
'nameserver-entity:nameserver',
'nameserver-entity:entity',
'tld:item',
],
],
security: 'object.user == user'
),
new Get(
@@ -58,26 +67,24 @@ use Symfony\Component\Uid\Uuid;
denormalizationContext: ['groups' => 'watchlist:create'],
name: 'create'
),
/*
new Patch(
routeName: 'watchlist_update',
normalizationContext: ['groups' => 'watchlist:item'],
denormalizationContext: ['groups' => 'watchlist:update']
denormalizationContext: ['groups' => 'watchlist:create'],
name: 'update'
),
*/
new Delete(),
],
)]
class WatchList
{
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'watchLists')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
public ?User $user = null;
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
#[Groups(['watchlist:item', 'watchlist:list'])]
private string $token;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'watchLists')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
public ?User $user = null;
/**
* @var Collection<int, Domain>
*/
@@ -85,23 +92,23 @@ class WatchList
#[ORM\JoinTable(name: 'watch_lists_domains',
joinColumns: [new ORM\JoinColumn(name: 'watch_list_token', referencedColumnName: 'token', onDelete: 'CASCADE')],
inverseJoinColumns: [new ORM\JoinColumn(name: 'domain_ldh_name', referencedColumnName: 'ldh_name', onDelete: 'CASCADE')])]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
private Collection $domains;
/**
* @var Collection<int, WatchListTrigger>
*/
#[ORM\OneToMany(targetEntity: WatchListTrigger::class, mappedBy: 'watchList', cascade: ['persist'], orphanRemoval: true)]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
#[SerializedName('triggers')]
private Collection $watchListTriggers;
#[ORM\ManyToOne(inversedBy: 'watchLists')]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
private ?Connector $connector = null;
#[ORM\Column(length: 255, nullable: true)]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
private ?string $name = null;
#[ORM\Column]

View File

@@ -12,7 +12,7 @@ class WatchListTrigger
{
#[ORM\Id]
#[ORM\Column(length: 255)]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
private ?string $event = null;
#[ORM\Id]
@@ -22,7 +22,7 @@ class WatchListTrigger
#[ORM\Id]
#[ORM\Column(enumType: TriggerAction::class)]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create'])]
private ?TriggerAction $action = null;
public function getEvent(): ?string