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:
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -6,6 +6,7 @@ use App\Entity\Post;
|
||||
use App\Entity\Species;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
@@ -20,6 +21,7 @@ class PostType extends AbstractType
|
||||
->add('latitude')
|
||||
->add('longitude')
|
||||
->add('altitude')
|
||||
->add('imageFile', FileType::class)
|
||||
->add('commentary')
|
||||
->add('species', EntityType::class, [
|
||||
'class' => Species::class,
|
||||
|
Reference in New Issue
Block a user