Generate coordinates when offer added

This commit is contained in:
Jannis Portmann 2022-01-12 14:42:02 +01:00
parent 3a525f397c
commit c6eadf742c
3 changed files with 64 additions and 2 deletions

View file

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220112111528 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}

View file

@ -38,7 +38,7 @@ class OfferController extends AbstractController
} }
#[Route('/new', name: 'new_offer')] #[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(); $offer = new Offering();
$form = $this->createForm(OfferingFormType::class, $offer); $form = $this->createForm(OfferingFormType::class, $offer);
@ -50,6 +50,11 @@ class OfferController extends AbstractController
$offer->setByUser($user); $offer->setByUser($user);
$offer->setCreatedAt(new \DateTime()); $offer->setCreatedAt(new \DateTime());
$coordinate = $plzconverter->convertPlzToCoordinate($form['zipCode']->getData());
if ($coordinate != null) {
$offer->setCoordinate($coordinate);
}
if ($photo = $form['photo']->getData()) { if ($photo = $form['photo']->getData()) {
$offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer); $offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer);
} }

View file

@ -4,6 +4,7 @@ namespace App\Entity;
use App\Repository\OfferingRepository; use App\Repository\OfferingRepository;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Location\Coordinate;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
/** /**
@ -51,6 +52,16 @@ class Offering
*/ */
private $description; private $description;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $lat;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $lng;
public function getId(): ?int public function getId(): ?int
{ {
return $this->id; return $this->id;
@ -132,4 +143,19 @@ class Offering
{ {
return (string) $this-getTitle(); 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;
}
} }