From 2bdd5f9ac2d18d716d866c6076a8b63c52ecfb9f Mon Sep 17 00:00:00 2001 From: lubiana Date: Sun, 29 Jun 2025 19:08:08 +0200 Subject: [PATCH] wip --- config/packages/api_platform.yaml | 7 +++ config/routes/api_platform.yaml | 4 ++ ecs.php | 4 +- migrations/Version20250629160123.php | 56 +++++++++++++++++++++++ migrations/Version20250629160639.php | 53 +++++++++++++++++++++ src/Controller/OrderItemController.php | 13 ------ src/EventListener/OrderItemPrePersist.php | 35 ++++++++++++++ 7 files changed, 158 insertions(+), 14 deletions(-) create mode 100644 config/packages/api_platform.yaml create mode 100644 config/routes/api_platform.yaml create mode 100644 migrations/Version20250629160123.php create mode 100644 migrations/Version20250629160639.php create mode 100644 src/EventListener/OrderItemPrePersist.php diff --git a/config/packages/api_platform.yaml b/config/packages/api_platform.yaml new file mode 100644 index 0000000..02f295a --- /dev/null +++ b/config/packages/api_platform.yaml @@ -0,0 +1,7 @@ +api_platform: + title: Hello API Platform + version: 1.0.0 + defaults: + stateless: true + cache_headers: + vary: ['Content-Type', 'Authorization', 'Origin'] diff --git a/config/routes/api_platform.yaml b/config/routes/api_platform.yaml new file mode 100644 index 0000000..38f11cb --- /dev/null +++ b/config/routes/api_platform.yaml @@ -0,0 +1,4 @@ +api_platform: + resource: . + type: api_platform + prefix: /api diff --git a/ecs.php b/ecs.php index 4815bcb..c26fa01 100644 --- a/ecs.php +++ b/ecs.php @@ -13,5 +13,7 @@ return ECSConfig::configure() __DIR__ . '/tests', ]) ->withRootFiles() - ->withRules([FinalClassFixer::class]) + ->withRules([ + FinalClassFixer::class, + ]) ->withSets([LubiSetList::ECS]); diff --git a/migrations/Version20250629160123.php b/migrations/Version20250629160123.php new file mode 100644 index 0000000..78542b7 --- /dev/null +++ b/migrations/Version20250629160123.php @@ -0,0 +1,56 @@ +addSql(<<<'SQL' + ALTER TABLE order_item ADD COLUMN is_paid BOOLEAN DEFAULT 0 NOT NULL + SQL); + $this->addSql(<<<'SQL' + ALTER TABLE order_item ADD COLUMN price_cents INTEGER DEFAULT 0 NOT NULL + SQL); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql(<<<'SQL' + CREATE TEMPORARY TABLE __temp__order_item AS SELECT name, extras, created_by, id, food_order_id, menu_item_id FROM order_item + SQL); + $this->addSql(<<<'SQL' + DROP TABLE order_item + SQL); + $this->addSql(<<<'SQL' + CREATE TABLE order_item (name VARCHAR(255) NOT NULL, extras VARCHAR(255) DEFAULT NULL, created_by VARCHAR(255) DEFAULT 'nobody' NOT NULL, id BLOB NOT NULL, food_order_id BLOB DEFAULT NULL, menu_item_id BLOB NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_52EA1F09A5D24A7A FOREIGN KEY (food_order_id) REFERENCES food_order (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_52EA1F099AB44FE0 FOREIGN KEY (menu_item_id) REFERENCES menu_item (id) NOT DEFERRABLE INITIALLY IMMEDIATE) + SQL); + $this->addSql(<<<'SQL' + INSERT INTO order_item (name, extras, created_by, id, food_order_id, menu_item_id) SELECT name, extras, created_by, id, food_order_id, menu_item_id FROM __temp__order_item + SQL); + $this->addSql(<<<'SQL' + DROP TABLE __temp__order_item + SQL); + $this->addSql(<<<'SQL' + CREATE INDEX IDX_52EA1F09A5D24A7A ON order_item (food_order_id) + SQL); + $this->addSql(<<<'SQL' + CREATE INDEX IDX_52EA1F099AB44FE0 ON order_item (menu_item_id) + SQL); + } +} diff --git a/migrations/Version20250629160639.php b/migrations/Version20250629160639.php new file mode 100644 index 0000000..b567250 --- /dev/null +++ b/migrations/Version20250629160639.php @@ -0,0 +1,53 @@ +addSql(<<<'SQL' + ALTER TABLE menu_item ADD COLUMN price_cents INTEGER DEFAULT 0 NOT NULL + SQL); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql(<<<'SQL' + CREATE TEMPORARY TABLE __temp__menu_item AS SELECT name, deleted_at, id, food_vendor_id, alias_of_id FROM menu_item + SQL); + $this->addSql(<<<'SQL' + DROP TABLE menu_item + SQL); + $this->addSql(<<<'SQL' + CREATE TABLE menu_item (name VARCHAR(255) NOT NULL, deleted_at DATETIME DEFAULT NULL, id BLOB NOT NULL, food_vendor_id BLOB NOT NULL, alias_of_id BLOB DEFAULT NULL, PRIMARY KEY(id), CONSTRAINT FK_D754D5506EF983E8 FOREIGN KEY (food_vendor_id) REFERENCES food_vendor (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_D754D55061F0AFC5 FOREIGN KEY (alias_of_id) REFERENCES menu_item (id) NOT DEFERRABLE INITIALLY IMMEDIATE) + SQL); + $this->addSql(<<<'SQL' + INSERT INTO menu_item (name, deleted_at, id, food_vendor_id, alias_of_id) SELECT name, deleted_at, id, food_vendor_id, alias_of_id FROM __temp__menu_item + SQL); + $this->addSql(<<<'SQL' + DROP TABLE __temp__menu_item + SQL); + $this->addSql(<<<'SQL' + CREATE INDEX IDX_D754D5506EF983E8 ON menu_item (food_vendor_id) + SQL); + $this->addSql(<<<'SQL' + CREATE INDEX IDX_D754D55061F0AFC5 ON menu_item (alias_of_id) + SQL); + } +} diff --git a/src/Controller/OrderItemController.php b/src/Controller/OrderItemController.php index f52d202..1bbd616 100644 --- a/src/Controller/OrderItemController.php +++ b/src/Controller/OrderItemController.php @@ -75,19 +75,6 @@ final class OrderItemController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $menuItem = $menuItemRepository->findOneBy([ - 'name' => $orderItem->getName(), - 'foodVendor' => $foodOrder->getFoodVendor(), - ]); - - if ($menuItem === null) { - $menuItem = new MenuItem; - $menuItem->setName($orderItem->getName()); - $menuItem->setFoodVendor($foodOrder->getFoodVendor()); - $entityManager->persist($menuItem); - } - - $orderItem->setMenuItem($menuItem); $orderItem->setFoodOrder($foodOrder); $entityManager->persist($orderItem); $entityManager->flush(); diff --git a/src/EventListener/OrderItemPrePersist.php b/src/EventListener/OrderItemPrePersist.php new file mode 100644 index 0000000..26ae9aa --- /dev/null +++ b/src/EventListener/OrderItemPrePersist.php @@ -0,0 +1,35 @@ +menuItemRepository->findOneBy([ + 'name' => $orderItem->getName(), + 'foodVendor' => $orderItem->getFoodOrder() + ->getFoodVendor(), + ]); + if ($menuItem === null) { + $menuItem = new MenuItem; + $menuItem->setName($orderItem->getName()); + $menuItem->setFoodVendor($orderItem->getFoodOrder()->getFoodVendor()); + $eventArgs->getObjectManager() + ->persist($menuItem); + } + $orderItem->setMenuItem($menuItem); + } +}