From d190971718afda425e94bc44963ea88580f295f8 Mon Sep 17 00:00:00 2001 From: thisfro Date: Mon, 17 Jan 2022 19:11:00 +0100 Subject: [PATCH] Don't use primary key for offers --- migrations/Version20220117175334.php | 32 ++++++++++++++++++++++++++++ src/Controller/OfferController.php | 7 +++--- src/Controller/TradeController.php | 2 +- src/Entity/Offering.php | 19 ++++++++++++++++- templates/app/offer.html.twig | 6 +++--- templates/offer/index.html.twig | 2 +- templates/user/offers.html.twig | 10 ++++----- templates/user/public.html.twig | 2 +- 8 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 migrations/Version20220117175334.php diff --git a/migrations/Version20220117175334.php b/migrations/Version20220117175334.php new file mode 100644 index 0000000..abf636d --- /dev/null +++ b/migrations/Version20220117175334.php @@ -0,0 +1,32 @@ +addSql('ALTER TABLE offering ADD url_id VARCHAR(13) NOT 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 url_id'); + } +} diff --git a/src/Controller/OfferController.php b/src/Controller/OfferController.php index 9b2572e..3130dba 100644 --- a/src/Controller/OfferController.php +++ b/src/Controller/OfferController.php @@ -83,6 +83,7 @@ class OfferController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $offer->setByUser($user); $offer->setCreatedAt(new \DateTime()); + $offer->setUrlId(uniqid()); $coordinate = $plzconverter->convertPlzToCoordinate($form['zipCode']->getData()); if ($coordinate != null) { @@ -108,7 +109,7 @@ class OfferController extends AbstractController ]); } - #[Route('/offer/{id}', name: 'show_offer')] + #[Route('/offer/{urlId}', name: 'show_offer')] public function showOffer(Offering $offer, WishRepository $wishRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response { $distance = null; @@ -142,7 +143,7 @@ class OfferController extends AbstractController ]); } - #[Route('/offer/edit/{id}', name: 'edit_offer')] + #[Route('/offer/edit/{urlId}', name: 'edit_offer')] public function editOffer(Offering $offer, OfferingRepository $offerRepository, Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response { $form = $this->createForm(OfferingFormType::class, $offer); @@ -175,7 +176,7 @@ class OfferController extends AbstractController throw new HttpException(403, "No permission"); } - #[Route('/offer/delete/{id}', name: 'delete_offer')] + #[Route('/offer/delete/{urlId}', name: 'delete_offer')] public function deleteOffer(Offering $offer, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response { $user = $this->getUser(); diff --git a/src/Controller/TradeController.php b/src/Controller/TradeController.php index 83d88be..f8f7882 100644 --- a/src/Controller/TradeController.php +++ b/src/Controller/TradeController.php @@ -49,6 +49,6 @@ class TradeController extends AbstractController $this->addFlash('error','You can\'t trade with yourself!'); } - return $this->redirectToRoute('show_offer', ['id' => $offer->getId()]); + return $this->redirectToRoute('show_offer', ['urlId' => $offer->getId()]); } } diff --git a/src/Entity/Offering.php b/src/Entity/Offering.php index 8bf6c1c..eca0e07 100644 --- a/src/Entity/Offering.php +++ b/src/Entity/Offering.php @@ -62,6 +62,11 @@ class Offering */ private $lng; + /** + * @ORM\Column(type="string", length=13) + */ + private $urlId; + public function getId(): ?int { return $this->id; @@ -141,7 +146,7 @@ class Offering public function __toString(): string { - return (string) $this->getTitle(); + return (string) $this-getTitle(); } public function getCoordinate(): ?Coordinate @@ -158,4 +163,16 @@ class Offering return $this; } + + public function getUrlId(): ?string + { + return $this->urlId; + } + + public function setUrlId(string $urlId): self + { + $this->urlId = $urlId; + + return $this; + } } \ No newline at end of file diff --git a/templates/app/offer.html.twig b/templates/app/offer.html.twig index f5cae81..a07dda6 100644 --- a/templates/app/offer.html.twig +++ b/templates/app/offer.html.twig @@ -47,7 +47,7 @@ {% if offer.byUser == user %} - + @@ -85,6 +85,6 @@ {% endif %} - Offer trade + Offer trade {% endif %} {% endblock %} diff --git a/templates/offer/index.html.twig b/templates/offer/index.html.twig index 68476c0..d917e2f 100644 --- a/templates/offer/index.html.twig +++ b/templates/offer/index.html.twig @@ -30,7 +30,7 @@ {% for offer in offers %}
- + {% if offer.photoFilename %} {% else %} diff --git a/templates/user/offers.html.twig b/templates/user/offers.html.twig index 4981a6c..9afebb1 100644 --- a/templates/user/offers.html.twig +++ b/templates/user/offers.html.twig @@ -14,7 +14,7 @@ {% for offer in offers %} -