Add image entity
This commit is contained in:
parent
325e255d75
commit
b1c9fdbdf7
19
src/Controller/HomeController.php
Normal file
19
src/Controller/HomeController.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class HomeController extends AbstractController
|
||||
{
|
||||
#[Route('/', name: 'app_home')]
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return $this->json([
|
||||
'message' => 'Path not found',
|
||||
'status' => 404
|
||||
], 404);
|
||||
}
|
||||
}
|
@ -3,6 +3,9 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Site;
|
||||
use App\Entity\Image;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
@ -10,24 +13,27 @@ use Symfony\Bridge\Doctrine\Attribute\MapEntity;
|
||||
|
||||
class SiteController extends AbstractController
|
||||
{
|
||||
#[Route('/', name: 'app_home')]
|
||||
public function root(): JsonResponse
|
||||
{
|
||||
return $this->json([
|
||||
'message' => 'Path not found',
|
||||
'status' => 404
|
||||
], 404);
|
||||
}
|
||||
|
||||
#[Route('/site/{gisId<\w+>}', name: 'app_site')]
|
||||
public function index(
|
||||
#[MapEntity(mapping: ['gisId' => 'gisId'])]
|
||||
Site $site
|
||||
Site $site,
|
||||
EntityManagerInterface $em
|
||||
): JsonResponse
|
||||
{
|
||||
return $this->json([
|
||||
'message' => 'Request successful',
|
||||
'site' => $site,
|
||||
]);
|
||||
$repo = $em->getRepository(Site::class);
|
||||
$coords = $repo->coordinates($site->getId());
|
||||
$repo = $em->getRepository(Image::class);
|
||||
$images = new ArrayCollection(
|
||||
$repo->findBy(
|
||||
['site' => $site->getId()],
|
||||
['sequence' => 'DESC']
|
||||
)
|
||||
);
|
||||
$site->setImages($images);
|
||||
|
||||
$site->setLat((float) $coords['lat']);
|
||||
$site->setLng((float) $coords['lng']);
|
||||
|
||||
return $this->json( $site );
|
||||
}
|
||||
}
|
||||
|
114
src/Entity/Image.php
Normal file
114
src/Entity/Image.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\ImageRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: ImageRepository::class)]
|
||||
#[ORM\Table(name: 'immagine')]
|
||||
class Image implements \JsonSerializable
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
private ?string $filename = null;
|
||||
|
||||
#[ORM\Column(name: 'didascalia', length: 255, nullable: true)]
|
||||
private ?string $caption = null;
|
||||
|
||||
#[ORM\Column(name: 'autore', length: 255, nullable: true)]
|
||||
private ?string $author = null;
|
||||
|
||||
#[ORM\Column(name: 'sito', type: Types::BIGINT, nullable: true)]
|
||||
private ?string $site = null;
|
||||
|
||||
#[ORM\Column(name: 'ordine', type: Types::SMALLINT)]
|
||||
private ?int $sequence = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setId(int $id): static
|
||||
{
|
||||
$this->id = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFilename(): ?string
|
||||
{
|
||||
return $this->filename;
|
||||
}
|
||||
|
||||
public function setFilename(string $filename): static
|
||||
{
|
||||
$this->filename = $filename;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCaption(): ?string
|
||||
{
|
||||
return $this->caption;
|
||||
}
|
||||
|
||||
public function setCaption(?string $caption): static
|
||||
{
|
||||
$this->caption = $caption;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAuthor(): ?string
|
||||
{
|
||||
return $this->author;
|
||||
}
|
||||
|
||||
public function setAuthor(?string $author): static
|
||||
{
|
||||
$this->author = $author;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSite(): ?string
|
||||
{
|
||||
return $this->site;
|
||||
}
|
||||
|
||||
public function setSite(?string $site): static
|
||||
{
|
||||
$this->site = $site;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSequence(): ?int
|
||||
{
|
||||
return $this->sequence;
|
||||
}
|
||||
|
||||
public function setSequence(int $sequence): static
|
||||
{
|
||||
$this->sequence = $sequence;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'filename' => $this->filename,
|
||||
'caption' => $this->caption,
|
||||
'author' => $this->author,
|
||||
];
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\SiteRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use \JsonSerializable;
|
||||
@ -73,6 +74,12 @@ class Site implements JsonSerializable
|
||||
#[ORM\Column(name: 'loc_generica', length: 200, nullable: true)]
|
||||
private ?string $genericPlace = null;
|
||||
|
||||
private ?float $lat;
|
||||
|
||||
private ?float $lng;
|
||||
|
||||
private ?ArrayCollection $images;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
@ -313,6 +320,40 @@ class Site implements JsonSerializable
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLat(): ?float
|
||||
{
|
||||
return $this->lat;
|
||||
}
|
||||
|
||||
public function setLat(float $lat): static
|
||||
{
|
||||
$this->lat = $lat;
|
||||
|
||||
return $this;
|
||||
}
|
||||
public function getLng(): ?float
|
||||
{
|
||||
return $this->lng;
|
||||
}
|
||||
public function setLng(float $lng): static
|
||||
{
|
||||
$this->lng = $lng;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getImages(): ?ArrayCollection
|
||||
{
|
||||
return $this->images;
|
||||
}
|
||||
|
||||
public function setImages(ArrayCollection $images): static
|
||||
{
|
||||
$this->images = $images;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
@ -321,6 +362,7 @@ class Site implements JsonSerializable
|
||||
'genericPlace' => $this->genericPlace,
|
||||
'address' => $this->address,
|
||||
'localization' => $this->localization,
|
||||
'coordinates' => [$this->lat, $this->lng],
|
||||
'denomination' => $this->denomination,
|
||||
'definition' => $this->definition,
|
||||
'period' => $this->period,
|
||||
@ -334,6 +376,7 @@ class Site implements JsonSerializable
|
||||
'description' => $this->description,
|
||||
'shortDescription' => $this->shortDescription,
|
||||
'ownedBy' => $this->ownedBy,
|
||||
'images' => $this->images->toArray(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
43
src/Repository/ImageRepository.php
Normal file
43
src/Repository/ImageRepository.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Image;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Image>
|
||||
*/
|
||||
class ImageRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Image::class);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Image[] Returns an array of Image objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('i')
|
||||
// ->andWhere('i.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('i.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
|
||||
// public function findOneBySomeField($value): ?Image
|
||||
// {
|
||||
// return $this->createQueryBuilder('i')
|
||||
// ->andWhere('i.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
}
|
@ -16,6 +16,23 @@ class SiteRepository extends ServiceEntityRepository
|
||||
parent::__construct($registry, Site::class);
|
||||
}
|
||||
|
||||
public function coordinates(int $id): array|bool
|
||||
{
|
||||
$conn = $this->getEntityManager()->getConnection();
|
||||
|
||||
$sql = '
|
||||
SELECT
|
||||
ST_X(ST_AsText(coordinate)) as lng,
|
||||
ST_Y(ST_AsText(coordinate)) as lat
|
||||
FROM
|
||||
sito
|
||||
WHERE id = :id
|
||||
';
|
||||
|
||||
return $conn->executeQuery($sql, ['id' => $id])
|
||||
->fetchAssociative();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Site[] Returns an array of Site objects
|
||||
// */
|
||||
|
Loading…
Reference in New Issue
Block a user