From ddb815d1b46b4e0366bb55659597a5cad3bb94c0 Mon Sep 17 00:00:00 2001 From: thisfro Date: Fri, 16 Jul 2021 11:00:02 +0200 Subject: [PATCH] Save cooridnates as GeoJSON in database --- migrations/Version20210715135559.php | 33 +++++++++++++++++++++++++ migrations/Version20210715142350.php | 35 +++++++++++++++++++++++++++ migrations/Version20210715142732.php | 36 ++++++++++++++++++++++++++++ src/Controller/OfferController.php | 11 +++++++-- src/Entity/Offering.php | 17 +++++++++++++ 5 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 migrations/Version20210715135559.php create mode 100644 migrations/Version20210715142350.php create mode 100644 migrations/Version20210715142732.php diff --git a/migrations/Version20210715135559.php b/migrations/Version20210715135559.php new file mode 100644 index 0000000..3ea2340 --- /dev/null +++ b/migrations/Version20210715135559.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE offering ADD coordinates TEXT DEFAULT NULL'); + $this->addSql('COMMENT ON COLUMN offering.coordinates IS \'(DC2Type:array)\''); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE offering DROP coordinates'); + } +} diff --git a/migrations/Version20210715142350.php b/migrations/Version20210715142350.php new file mode 100644 index 0000000..ef51b4a --- /dev/null +++ b/migrations/Version20210715142350.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE offering ADD coordinate JSON DEFAULT NULL'); + $this->addSql('ALTER TABLE offering DROP coordinates'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE offering ADD coordinates TEXT DEFAULT NULL'); + $this->addSql('ALTER TABLE offering DROP coordinate'); + $this->addSql('COMMENT ON COLUMN offering.coordinates IS \'(DC2Type:array)\''); + } +} diff --git a/migrations/Version20210715142732.php b/migrations/Version20210715142732.php new file mode 100644 index 0000000..bb54fde --- /dev/null +++ b/migrations/Version20210715142732.php @@ -0,0 +1,36 @@ +addSql('ALTER TABLE offering ALTER coordinate TYPE TEXT'); + $this->addSql('ALTER TABLE offering ALTER coordinate DROP DEFAULT'); + $this->addSql('COMMENT ON COLUMN offering.coordinate IS \'(DC2Type:object)\''); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE SCHEMA public'); + $this->addSql('ALTER TABLE offering ALTER coordinate TYPE JSON'); + $this->addSql('ALTER TABLE offering ALTER coordinate DROP DEFAULT'); + $this->addSql('COMMENT ON COLUMN offering.coordinate IS NULL'); + } +} diff --git a/src/Controller/OfferController.php b/src/Controller/OfferController.php index 345a13a..1268d18 100644 --- a/src/Controller/OfferController.php +++ b/src/Controller/OfferController.php @@ -13,6 +13,8 @@ use App\Service\DistanceCalculator; use App\Service\OfferPhotoHelper; use Doctrine\ORM\EntityManagerInterface; +use Location\Coordinate; +use Location\Formatter\Coordinate\GeoJSON; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -38,7 +40,7 @@ class OfferController extends AbstractController } #[Route('/new', name: 'new_offer')] - public function newOffer(Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response + public function newOffer(Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper, PlzToCoordinate $plzconverter): Response { $offer = new Offering(); $form = $this->createForm(OfferingFormType::class, $offer); @@ -50,6 +52,11 @@ class OfferController extends AbstractController $offer->setByUser($user); $offer->setCreatedAt(new \DateTime()); + if ($offerPlz = $form['zipCode']->getData()) { + $coordinate = $plzconverter->convertPlzToCoordinate($offerPlz); + $offer->setCoordinate($coordinate->format(new GeoJSON())); + } + if ($photo = $form['photo']->getData()) { $offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer); } @@ -81,7 +88,7 @@ class OfferController extends AbstractController if (isset($userPlz)) { - $distance = $distanceCalculator->calculateDistance($plzconverter->getCoordinates($offerPlz), $plzconverter->getCoordinates($userPlz)); + $distance = $distanceCalculator->calculateDistance($plzconverter->convertPlzToCoordinate($offerPlz), $plzconverter->convertPlzToCoordinate($userPlz)); } return $this->render('app/offer.html.twig', [ diff --git a/src/Entity/Offering.php b/src/Entity/Offering.php index 6f8ac66..62a9d96 100644 --- a/src/Entity/Offering.php +++ b/src/Entity/Offering.php @@ -51,6 +51,11 @@ class Offering */ private $description; + /** + * @ORM\Column(type="object", nullable=true) + */ + private $coordinate; + public function getId(): ?int { return $this->id; @@ -132,4 +137,16 @@ class Offering { return (string) $this-getTitle(); } + + public function getCoordinate() + { + return $this->coordinate; + } + + public function setCoordinate($coordinate): self + { + $this->coordinate = $coordinate; + + return $this; + } }