<?php
// api/src/Entity/MediaObject.php
namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Delete;
use App\Controller\CreateMediaObjectAction;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use Doctrine\DBAL\Types\Types;
use Symfony\Component\Serializer\Annotation\MaxDepth;
use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
#[Vich\Uploadable]
#[ORM\Entity]
#[ApiResource(
normalizationContext: ['groups' => ['media_object:read']],
types: ['https://schema.org/MediaObject'],
operations: [
new Get(),
new GetCollection(),
new Delete(),
new Post(
controller: CreateMediaObjectAction::class,
deserialize: false,
validationContext: ['groups' => ['Default', 'media_object_create']],
openapiContext: [
'requestBody' => [
'content' => [
'multipart/form-data' => [
'schema' => [
'type' => 'object',
'properties' => [
'file' => [
'type' => 'string',
'format' => 'binary'
]
]
]
]
]
]
]
)
],
order: ['id' => 'DESC'],
)]
#[ApiFilter(DateFilter::class, properties: ['uploadTimestamp'])]
#[ORM\HasLifecycleCallbacks]
class MediaObject
{
#[Groups(['media_object:read', 'PwAccident:read'])]
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
private ?int $id = null;
#[ApiProperty(types: ['https://schema.org/contentUrl'])]
#[Groups(['media_object:read', 'PwAccident:read'])]
public ?string $contentUrl = null;
#[Vich\UploadableField(mapping: "media_object", fileNameProperty: "filePath")]
#[Assert\NotNull(groups: ['media_object_create'])]
public ?File $file = null;
#[Groups(['media_object:read', 'PwAccident:read'])]
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
public $uploadTimestamp;
#[ORM\Column(nullable: true)]
#[Groups(['media_object:read', 'PwAccident:read'])]
public ?string $filePath = null;
public function getId(): ?int
{
return $this->id;
}
public function getUploadTimestamp(): ?\DateTimeInterface
{
return $this->uploadTimestamp;
}
#[ORM\PrePersist]
public function setCreatedAtValue(): void
{
$this->uploadTimestamp = new \DateTime();
}
}