feat : ajout des notifications

This commit is contained in:
2026-03-02 16:17:08 +01:00
parent e0f2a84f2c
commit 7a3d01d77f
14 changed files with 512 additions and 3 deletions

134
src/Entity/Notification.php Normal file
View File

@@ -0,0 +1,134 @@
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Repository\NotificationRepository;
use App\State\MarkAllNotificationsReadProcessor;
use App\State\UnreadNotificationsProvider;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: '/notifications/unread',
normalizationContext: ['groups' => ['notification:read']],
security: "is_granted('ROLE_USER')",
provider: UnreadNotificationsProvider::class,
paginationEnabled: false
),
new Post(
uriTemplate: '/notifications/mark-all-read',
security: "is_granted('ROLE_USER')",
input: false,
output: false,
read: false,
processor: MarkAllNotificationsReadProcessor::class
),
]
)]
#[ORM\Entity(repositoryClass: NotificationRepository::class)]
#[ORM\Table(name: 'notifications')]
#[ORM\Index(columns: ['recipient_id', 'is_read', 'created_at'], name: 'idx_notifications_recipient_read_created')]
class Notification
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['notification:read'])]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: User::class)]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
private ?User $recipient = null;
#[ORM\Column(type: 'string', length: 120)]
#[Groups(['notification:read'])]
private string $title = '';
#[ORM\Column(type: 'text')]
#[Groups(['notification:read'])]
private string $message = '';
#[ORM\Column(type: 'boolean', options: ['default' => false])]
#[Groups(['notification:read'])]
private bool $isRead = false;
#[ORM\Column(type: 'datetime_immutable')]
#[Groups(['notification:read'])]
private DateTimeImmutable $createdAt;
public function __construct()
{
$this->createdAt = new DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getRecipient(): ?User
{
return $this->recipient;
}
public function setRecipient(?User $recipient): self
{
$this->recipient = $recipient;
return $this;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getMessage(): string
{
return $this->message;
}
public function setMessage(string $message): self
{
$this->message = $message;
return $this;
}
public function isRead(): bool
{
return $this->isRead;
}
public function getIsRead(): bool
{
return $this->isRead;
}
public function setIsRead(bool $isRead): self
{
$this->isRead = $isRead;
return $this;
}
public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}
}

View File

@@ -10,6 +10,7 @@ use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Repository\UserRepository;
use App\State\CurrentUserProvider;
use App\State\UserPasswordHasherProcessor;
use Doctrine\Common\Collections\ArrayCollection;
@@ -52,7 +53,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
),
]
)]
#[ORM\Entity]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: 'users')]
#[ORM\UniqueConstraint(name: 'uniq_users_username', fields: ['username'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface