Allow uploading images (#13)

Adds an optional field for the post image.

Fixes #12

Co-authored-by: clfreville2 <clement.freville2@etu.uca.fr>
Reviewed-on: https://codefirst.iut.uca.fr/git/clement.freville2/herbarium/pulls/13
Reviewed-by: Bastien OLLIER <bastien.ollier@noreply.codefirst.iut.uca.fr>
This commit is contained in:
2024-06-12 09:05:30 +02:00
parent 8859cd0000
commit 6a6a135891
11 changed files with 280 additions and 2 deletions

View File

@@ -12,8 +12,10 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\Entity(repositoryClass: PostRepository::class)]
#[ORM\HasLifecycleCallbacks]
@@ -23,6 +25,7 @@ use Symfony\Component\Validator\Constraints as Assert;
)]
#[GetCollection(normalizationContext: ['groups' => ['post:collection:read']])]
#[Metadata\ApiFilter(filterClass: SearchFilter::class, properties: ['species' => 'exact'])]
#[Vich\Uploadable]
class Post
{
#[ORM\Id]
@@ -41,6 +44,9 @@ class Post
#[Groups(['post:collection:read'])]
private ?\DateTimeImmutable $publicationDate = null;
#[ORM\Column]
private ?\DateTimeImmutable $updatedAt = null;
#[ORM\Column(nullable: true)]
#[Groups(['post:collection:read'])]
private ?float $latitude = null;
@@ -53,6 +59,13 @@ class Post
#[Groups(['post:collection:read'])]
private ?float $altitude = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $image = null;
#[Vich\UploadableField(mapping: 'posts', fileNameProperty: 'image')]
#[Assert\Image]
private ?File $imageFile = null;
#[ORM\Column(type: Types::TEXT)]
#[Groups(['post:read'])]
#[Assert\NotBlank]
@@ -103,6 +116,18 @@ class Post
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getLatitude(): ?float
{
return $this->latitude;
@@ -139,6 +164,30 @@ class Post
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): static
{
$this->image = $image;
return $this;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageFile(?File $imageFile): static
{
$this->imageFile = $imageFile;
return $this;
}
public function getCommentary(): ?string
{
return $this->commentary;
@@ -170,6 +219,7 @@ class Post
if ($this->publicationDate === null) {
$this->publicationDate = new \DateTimeImmutable();
}
$this->updatedAt = new \DateTimeImmutable();
}
/**