From 9d81464fd84c471836158413eb9d39c861eae26d Mon Sep 17 00:00:00 2001 From: lubiana Date: Thu, 27 Jun 2024 17:43:14 +0200 Subject: [PATCH 1/3] #20 add username form --- src/Controller/HomeController.php | 18 +++++++++++++++++- src/Form/UserNameFormType.php | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/Form/UserNameFormType.php diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index a9bd31e..991729e 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -2,16 +2,32 @@ namespace App\Controller; +use App\Form\UserNameFormType; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -final class HomeController +final class HomeController extends AbstractController { + public const string DEFAULT_USERNAME = 'nobody'; #[Route('/', name: 'home')] public function home(UrlGeneratorInterface $router): Response { return new RedirectResponse($router->generate('app_food_order_index')); } + + public function usernameForm(Request $request): Response + { + $username = $request->cookies->get('username', self::DEFAULT_USERNAME); + $form = $this->createForm(UsernameFormType::class); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $username = $form->getData()['username'] ?? self::DEFAULT_USERNAME; + if ($username === '') {} + + } + } } diff --git a/src/Form/UserNameFormType.php b/src/Form/UserNameFormType.php new file mode 100644 index 0000000..63cb607 --- /dev/null +++ b/src/Form/UserNameFormType.php @@ -0,0 +1,24 @@ +add('username') + ; + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + // Configure your form options here + ]); + } +} -- 2.39.5 From 511f12f10faf1407cef0ce9dd724933aed453d18 Mon Sep 17 00:00:00 2001 From: lubiana Date: Thu, 27 Jun 2024 23:33:32 +0200 Subject: [PATCH 2/3] !20 add username to order and item --- migrations/Version20240627212849.php | 44 ++++++++++++++++++++++++ src/Controller/HomeController.php | 22 +++++++++--- src/Entity/FoodOrder.php | 17 +++++++++ src/Entity/OrderItem.php | 17 +++++++++ src/Form/OrderItemType.php | 4 ++- src/Form/UserNameFormType.php | 11 ++++-- templates/base.html.twig | 36 +++++++++++-------- templates/food_order/index.html.twig | 1 + templates/food_order/show.html.twig | 6 ++++ templates/food_order/table_row.html.twig | 1 + templates/order_item/new.html.twig | 3 +- templates/username.html.twig | 9 +++++ 12 files changed, 147 insertions(+), 24 deletions(-) create mode 100644 migrations/Version20240627212849.php create mode 100644 templates/username.html.twig diff --git a/migrations/Version20240627212849.php b/migrations/Version20240627212849.php new file mode 100644 index 0000000..2a5650b --- /dev/null +++ b/migrations/Version20240627212849.php @@ -0,0 +1,44 @@ +addSql('ALTER TABLE food_order ADD COLUMN created_by VARCHAR(255) DEFAULT \'nobody\' NOT NULL'); + $this->addSql('ALTER TABLE order_item ADD COLUMN created_by VARCHAR(255) DEFAULT \'nobody\' NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE TEMPORARY TABLE __temp__food_order AS SELECT id, closed_at, food_vendor_id FROM food_order'); + $this->addSql('DROP TABLE food_order'); + $this->addSql('CREATE TABLE food_order (id BLOB NOT NULL, closed_at DATETIME DEFAULT NULL, food_vendor_id BLOB NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_44856726EF983E8 FOREIGN KEY (food_vendor_id) REFERENCES food_vendor (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('INSERT INTO food_order (id, closed_at, food_vendor_id) SELECT id, closed_at, food_vendor_id FROM __temp__food_order'); + $this->addSql('DROP TABLE __temp__food_order'); + $this->addSql('CREATE INDEX IDX_44856726EF983E8 ON food_order (food_vendor_id)'); + $this->addSql('CREATE TEMPORARY TABLE __temp__order_item AS SELECT id, name, extras, food_order_id, menu_item_id FROM order_item'); + $this->addSql('DROP TABLE order_item'); + $this->addSql('CREATE TABLE order_item (id BLOB NOT NULL, name VARCHAR(255) NOT NULL, extras VARCHAR(255) DEFAULT 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)'); + $this->addSql('INSERT INTO order_item (id, name, extras, food_order_id, menu_item_id) SELECT id, name, extras, food_order_id, menu_item_id FROM __temp__order_item'); + $this->addSql('DROP TABLE __temp__order_item'); + $this->addSql('CREATE INDEX IDX_52EA1F09A5D24A7A ON order_item (food_order_id)'); + $this->addSql('CREATE INDEX IDX_52EA1F099AB44FE0 ON order_item (menu_item_id)'); + } +} diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 991729e..46c90c7 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Form\UserNameFormType; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -13,21 +14,34 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; final class HomeController extends AbstractController { public const string DEFAULT_USERNAME = 'nobody'; + #[Route('/', name: 'home')] public function home(UrlGeneratorInterface $router): Response { return new RedirectResponse($router->generate('app_food_order_index')); } - public function usernameForm(Request $request): Response + #[Route('/username', name: 'username')] + public function usernameForm(Request $request, UrlGeneratorInterface $router): Response { - $username = $request->cookies->get('username', self::DEFAULT_USERNAME); $form = $this->createForm(UsernameFormType::class); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $username = $form->getData()['username'] ?? self::DEFAULT_USERNAME; - if ($username === '') {} - + $response = new RedirectResponse($router->generate('app_food_order_index')); + if ($username === self::DEFAULT_USERNAME || $username === '') { + $response->headers->clearCookie('username'); + return $response; + } + $response->headers->setCookie(new Cookie('username', $username)); + return $response; } + $username = $request->cookies->get('username', self::DEFAULT_USERNAME); + $form->setData([ + 'username' => $username, + ]); + return $this->render('username.html.twig', [ + 'form' => $form, + ]); } } diff --git a/src/Entity/FoodOrder.php b/src/Entity/FoodOrder.php index 72a9530..40d7db8 100644 --- a/src/Entity/FoodOrder.php +++ b/src/Entity/FoodOrder.php @@ -34,6 +34,11 @@ class FoodOrder #[ORM\OneToMany(targetEntity: OrderItem::class, mappedBy: 'foodOrder', orphanRemoval: true)] private Collection $orderItems; + #[ORM\Column(length: 255, options: [ + 'default' => 'nobody', + ])] + private string|null $createdBy = 'nobody'; + public function __construct() { $this->orderItems = new ArrayCollection; @@ -117,4 +122,16 @@ class FoodOrder return $this; } + + public function getCreatedBy(): string|null + { + return $this->createdBy; + } + + public function setCreatedBy(string $createdBy): static + { + $this->createdBy = $createdBy; + + return $this; + } } diff --git a/src/Entity/OrderItem.php b/src/Entity/OrderItem.php index 1429a5d..cc7abd4 100644 --- a/src/Entity/OrderItem.php +++ b/src/Entity/OrderItem.php @@ -31,6 +31,11 @@ class OrderItem #[ORM\JoinColumn(nullable: false)] private MenuItem|null $menuItem = null; + #[ORM\Column(length: 255, options: [ + 'default' => 'nobody', + ])] + private string|null $createdBy = 'nobody'; + public function getId(): Ulid|null { return $this->id; @@ -83,4 +88,16 @@ class OrderItem return $this; } + + public function getCreatedBy(): string|null + { + return $this->createdBy; + } + + public function setCreatedBy(string $createdBy): static + { + $this->createdBy = $createdBy; + + return $this; + } } diff --git a/src/Form/OrderItemType.php b/src/Form/OrderItemType.php index 4716baf..2cd7f32 100644 --- a/src/Form/OrderItemType.php +++ b/src/Form/OrderItemType.php @@ -14,7 +14,9 @@ final class OrderItemType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('name') + ->add(child: 'name', options: [ + 'data' => $options['name'] ?? '', + ]) ->add('extras') ; } diff --git a/src/Form/UserNameFormType.php b/src/Form/UserNameFormType.php index 63cb607..6d2a011 100644 --- a/src/Form/UserNameFormType.php +++ b/src/Form/UserNameFormType.php @@ -1,20 +1,25 @@ -add('username') + ->add(child: 'username', options: [ + 'required' => false, + ]) ; } + #[Override] public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ diff --git a/templates/base.html.twig b/templates/base.html.twig index 1918768..a286536 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -4,23 +4,29 @@ {% block title %}Welcome!{% endblock %} - + + -
- - -
-
- {% block body %}{% endblock %} -
+
+

Hello {{ app.request.cookies.get('username', 'nobody') }} - change name

+ +
+
+ {% block body %}{% endblock %} +
+ diff --git a/templates/food_order/index.html.twig b/templates/food_order/index.html.twig index 1a80bb5..e0aaf6c 100644 --- a/templates/food_order/index.html.twig +++ b/templates/food_order/index.html.twig @@ -8,6 +8,7 @@ + diff --git a/templates/food_order/show.html.twig b/templates/food_order/show.html.twig index 556de6a..ddf36c3 100644 --- a/templates/food_order/show.html.twig +++ b/templates/food_order/show.html.twig @@ -11,6 +11,10 @@ + + + + @@ -32,6 +36,7 @@
CreatedBy Vendor CreatedAt ClosedAtVendor {{ food_order.foodVendor.name }}
Created By{{ food_order.createdBy }}
CreatedAt {{ food_order.createdAt ? food_order.createdAt|date('Y-m-d H:i:s') : '' }}
+ @@ -40,6 +45,7 @@ {% for item in food_order.orderItems %} + + diff --git a/templates/order_item/new.html.twig b/templates/order_item/new.html.twig index 5b90051..53aff8d 100644 --- a/templates/order_item/new.html.twig +++ b/templates/order_item/new.html.twig @@ -12,7 +12,7 @@
{% for menuItem in menuItems %} - + {{ menuItem.name }} {% endfor %}
@@ -23,6 +23,7 @@
username name extras actions
{{ item.createdBy }} {{ item.name }} {{ item.extras }} diff --git a/templates/food_order/table_row.html.twig b/templates/food_order/table_row.html.twig index e42bcb9..5f0bff9 100644 --- a/templates/food_order/table_row.html.twig +++ b/templates/food_order/table_row.html.twig @@ -1,4 +1,5 @@
{{ food_order.createdBy }} {{ food_order.foodVendor.name }} {{ food_order.createdAt ? food_order.createdAt|date('Y-m-d H:i:s') : '' }} {{ food_order.closedAt ? food_order.closedAt|date('Y-m-d H:i:s') : '' }}