notificaton: add comments

This commit is contained in:
Julien Fastré 2021-12-28 23:03:58 +01:00
parent 1ad6a958e2
commit 9647785d8b
3 changed files with 209 additions and 0 deletions

View File

@ -35,6 +35,12 @@ class Notification
private ?ArrayCollection $addressesOnLoad = null;
/**
* @ORM\OneToMany(targetEntity=NotificationComment::class, mappedBy="notification", orphanRemoval=true)
* @ORM\OrderBy({"createdAt": "ASC"})
*/
private Collection $comments;
/**
* @ORM\Column(type="datetime_immutable")
*/
@ -80,6 +86,7 @@ class Notification
{
$this->addressees = new ArrayCollection();
$this->unreadBy = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->setDate(new DateTimeImmutable());
}
@ -93,6 +100,16 @@ class Notification
return $this;
}
public function addComment(NotificationComment $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setNotification($this);
}
return $this;
}
public function addUnreadBy(User $user): self
{
if (!$this->unreadBy->contains($user)) {
@ -115,6 +132,11 @@ class Notification
return $this->addressees;
}
public function getComments(): Collection
{
return $this->comments;
}
public function getDate(): ?DateTimeImmutable
{
return $this->date;
@ -192,6 +214,13 @@ class Notification
return $this;
}
public function removeComment(NotificationComment $comment): self
{
$this->comments->removeElement($comment);
return $this;
}
public function removeUnreadBy(User $user): self
{
$this->unreadBy->removeElement($user);

View File

@ -0,0 +1,137 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Entity;
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table("chill_main_notification_comment")
*/
class NotificationComment implements TrackCreationInterface, TrackUpdateInterface
{
/**
* @ORM\Column(type="text")
*/
private string $content = '';
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private ?DateTimeImmutable $createdAt = null;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=true)
*/
private ?User $createdBy = null;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\ManyToOne(targetEntity=Notification::class, inversedBy="comments")
* @ORM\JoinColumn(nullable=false)
*/
private ?Notification $notification = null;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private ?DateTimeImmutable $updateAt = null;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=true)
*/
private ?User $updatedBy = null;
public function getContent(): string
{
return $this->content;
}
public function getId(): ?int
{
return $this->id;
}
public function getNotification(): ?Notification
{
return $this->notification;
}
public function getUpdateAt(): ?DateTimeImmutable
{
return $this->updateAt;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function setCreatedAt(DateTimeInterface $datetime): self
{
$this->createdAt = $datetime;
return $this;
}
public function setCreatedBy(User $user): self
{
$this->createdBy = $user;
return $this;
}
/**
* @internal use Notification::addComment
*/
public function setNotification(?Notification $notification): self
{
$this->notification = $notification;
return $this;
}
public function setUpdateAt(?DateTimeImmutable $updateAt): self
{
$this->updateAt = $updateAt;
return $this;
}
public function setUpdatedAt(DateTimeInterface $datetime): self
{
$this->updateAt = $datetime;
return $this;
}
public function setUpdatedBy(User $user): self
{
$this->updatedBy = $user;
return $this;
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20211228215919 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->addSql('DROP SEQUENCE chill_main_notification_comment_id_seq CASCADE');
$this->addSql('DROP TABLE chill_main_notification_comment');
}
public function getDescription(): string
{
return 'Notifications: add comment on notifications';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE SEQUENCE chill_main_notification_comment_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE chill_main_notification_comment (id INT NOT NULL, notification_id INT NOT NULL, content TEXT NOT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updateAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, createdBy_id INT DEFAULT NULL, updatedBy_id INT DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_983BD2CFEF1A9D84 ON chill_main_notification_comment (notification_id)');
$this->addSql('CREATE INDEX IDX_983BD2CF3174800F ON chill_main_notification_comment (createdBy_id)');
$this->addSql('CREATE INDEX IDX_983BD2CF65FF1AEC ON chill_main_notification_comment (updatedBy_id)');
$this->addSql('COMMENT ON COLUMN chill_main_notification_comment.createdAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('COMMENT ON COLUMN chill_main_notification_comment.updateAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('ALTER TABLE chill_main_notification_comment ADD CONSTRAINT FK_983BD2CFEF1A9D84 FOREIGN KEY (notification_id) REFERENCES chill_main_notification (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_main_notification_comment ADD CONSTRAINT FK_983BD2CF3174800F FOREIGN KEY (createdBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_main_notification_comment ADD CONSTRAINT FK_983BD2CF65FF1AEC FOREIGN KEY (updatedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
}
}