From c6eadf742ccd6c66e016d2814749d68b8fff42eb Mon Sep 17 00:00:00 2001 From: thisfro Date: Wed, 12 Jan 2022 14:42:02 +0100 Subject: [PATCH] Generate coordinates when offer added --- migrations/Version20220112111528.php | 31 ++++++++++++++++++++++++++++ src/Controller/OfferController.php | 7 ++++++- src/Entity/Offering.php | 28 ++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 migrations/Version20220112111528.php diff --git a/migrations/Version20220112111528.php b/migrations/Version20220112111528.php new file mode 100644 index 0000000..2e11440 --- /dev/null +++ b/migrations/Version20220112111528.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE offering ADD lat DOUBLE PRECISION DEFAULT NULL, ADD lng DOUBLE PRECISION DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE offering DROP lat, DROP lng'); + } +} diff --git a/src/Controller/OfferController.php b/src/Controller/OfferController.php index 5c56165..d6f51c6 100644 --- a/src/Controller/OfferController.php +++ b/src/Controller/OfferController.php @@ -38,7 +38,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, PlzToCoordinate $plzconverter, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response { $offer = new Offering(); $form = $this->createForm(OfferingFormType::class, $offer); @@ -50,6 +50,11 @@ class OfferController extends AbstractController $offer->setByUser($user); $offer->setCreatedAt(new \DateTime()); + $coordinate = $plzconverter->convertPlzToCoordinate($form['zipCode']->getData()); + if ($coordinate != null) { + $offer->setCoordinate($coordinate); + } + if ($photo = $form['photo']->getData()) { $offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer); } diff --git a/src/Entity/Offering.php b/src/Entity/Offering.php index 6f8ac66..dcbae52 100644 --- a/src/Entity/Offering.php +++ b/src/Entity/Offering.php @@ -4,6 +4,7 @@ namespace App\Entity; use App\Repository\OfferingRepository; use Doctrine\ORM\Mapping as ORM; +use Location\Coordinate; use Symfony\Component\Validator\Constraints as Assert; /** @@ -51,6 +52,16 @@ class Offering */ private $description; + /** + * @ORM\Column(type="float", nullable=true) + */ + private $lat; + + /** + * @ORM\Column(type="float", nullable=true) + */ + private $lng; + public function getId(): ?int { return $this->id; @@ -132,4 +143,19 @@ class Offering { return (string) $this-getTitle(); } -} + + public function getCoordinate(): ?Coordinate + { + $coordinate = new Coordinate($this->long, $this->lat); + + return $coordinate; + } + + public function setCoordinate(Coordinate $coordinate): self + { + $this->lat = $coordinate->getLat(); + $this->lng = $coordinate->getLng(); + + return $this; + } +} \ No newline at end of file