From 814dc4a41bbc0f751f753d8466652b516e64ef28 Mon Sep 17 00:00:00 2001 From: lubiana Date: Fri, 14 Jun 2024 18:48:32 +0200 Subject: [PATCH 1/2] remove unneeded test add order item stuff --- src/Command/FakeDataCommand.php | 76 +++++++++++++++++++ src/Controller/FoodOrderController.php | 2 +- src/Controller/OrderItemController.php | 77 +++++++++++++------- src/Repository/FoodOrderRepository.php | 15 ++++ templates/food_order/show.html.twig | 30 +++++++- templates/order_item/_delete_form.html.twig | 4 - templates/order_item/edit.html.twig | 4 +- templates/order_item/index.html.twig | 37 ---------- templates/order_item/new.html.twig | 2 +- templates/order_item/show.html.twig | 30 -------- tests/Controller/OrderItemControllerTest.php | 11 --- 11 files changed, 175 insertions(+), 113 deletions(-) create mode 100644 src/Command/FakeDataCommand.php delete mode 100644 templates/order_item/_delete_form.html.twig delete mode 100644 templates/order_item/index.html.twig delete mode 100644 templates/order_item/show.html.twig diff --git a/src/Command/FakeDataCommand.php b/src/Command/FakeDataCommand.php new file mode 100644 index 0000000..1eceac3 --- /dev/null +++ b/src/Command/FakeDataCommand.php @@ -0,0 +1,76 @@ +getArgument('arg1'); + + if ($arg1) { + $io->note(sprintf('You passed an argument: %s', $arg1)); + } + + if ($input->getOption('option1')) { + // ... + } + + $io->success('You have a new command! Now make it your own! Pass --help to see your options.'); + + return Command::SUCCESS; + } + + /** + * @return FoodVendor[] + */ + public function generateVendors(int $amount = 10): array + { + $vendors = []; + foreach (range(1, $amount) as $i) { + $vendor = new FoodVendor(); + $vendor->setName('Food Vendor ' . $i); + $this->entityManager->persist($vendor); + $vendors[] = $vendor; + } + return $vendors; + } + + public function generateOrdersForVendor(FoodVendor $vendor, int $amount = 10): array + { + $orders = []; + foreach (range(1, $amount) as $i) { + $order = new FoodOrder(); + $order->setFoodVendor($vendor); + if ($i % 2 === 0) { + $order->close(); + } + $orders[] = $order; + } + return $orders; + } +} diff --git a/src/Controller/FoodOrderController.php b/src/Controller/FoodOrderController.php index 69e3292..7251951 100644 --- a/src/Controller/FoodOrderController.php +++ b/src/Controller/FoodOrderController.php @@ -18,7 +18,7 @@ final class FoodOrderController extends AbstractController public function index(FoodOrderRepository $foodOrderRepository): Response { return $this->render('food_order/index.html.twig', [ - 'food_orders' => $foodOrderRepository->findAll(), + 'food_orders' => $foodOrderRepository->findLatestEntries(), ]); } diff --git a/src/Controller/OrderItemController.php b/src/Controller/OrderItemController.php index 8658048..1716839 100644 --- a/src/Controller/OrderItemController.php +++ b/src/Controller/OrderItemController.php @@ -2,9 +2,9 @@ namespace App\Controller; +use App\Entity\FoodOrder; use App\Entity\OrderItem; use App\Form\OrderItemType; -use App\Repository\OrderItemRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -14,52 +14,74 @@ use Symfony\Component\Routing\Attribute\Route; #[Route('/order/item')] final class OrderItemController extends AbstractController { - #[Route('/', name: 'app_order_item_index', methods: ['GET'])] - public function index(OrderItemRepository $orderItemRepository): Response - { - return $this->render('order_item/index.html.twig', [ - 'order_items' => $orderItemRepository->findAll(), - ]); - } - - #[Route('/new', name: 'app_order_item_new', methods: ['GET', 'POST'])] - public function new(Request $request, EntityManagerInterface $entityManager): Response + #[Route('/new/{foodOrder}', name: 'app_order_item_new', methods: ['GET', 'POST'])] + public function new(Request $request, FoodOrder $foodOrder, EntityManagerInterface $entityManager): Response { + if ($foodOrder->isClosed()) { + return $this->redirectToRoute('app_food_order_show', [ + 'id' => $foodOrder->getId(), + ], Response::HTTP_SEE_OTHER); + } $orderItem = new OrderItem; $form = $this->createForm(OrderItemType::class, $orderItem); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $orderItem->setFoodOrder($foodOrder); $entityManager->persist($orderItem); $entityManager->flush(); - return $this->redirectToRoute('app_order_item_index', [], Response::HTTP_SEE_OTHER); + return $this->redirectToRoute('app_food_order_show', [ + 'id' => $foodOrder->getId(), + ], Response::HTTP_SEE_OTHER); } return $this->render('order_item/new.html.twig', [ 'order_item' => $orderItem, + 'food_order' => $foodOrder, 'form' => $form, ]); } - #[Route('/{id}', name: 'app_order_item_show', methods: ['GET'])] - public function show(OrderItem $orderItem): Response + #[Route('/{id}/copy', name: 'app_order_item_copy', methods: ['GET'])] + public function copy(OrderItem $orderItem, EntityManagerInterface $entityManager): Response { - return $this->render('order_item/show.html.twig', [ - 'order_item' => $orderItem, - ]); + $foodOrder = $orderItem->getFoodOrder(); + if ($foodOrder->isClosed()) { + return $this->redirectToRoute('app_food_order_show', [ + 'id' => $foodOrder->getId(), + ], Response::HTTP_SEE_OTHER); + } + $newOrderItem = new OrderItem; + $newOrderItem->setFoodOrder($orderItem->getFoodOrder()); + $newOrderItem->setName($orderItem->getName()); + $newOrderItem->setExtras($orderItem->getExtras()); + + $entityManager->persist($newOrderItem); + $entityManager->flush(); + return $this->redirectToRoute('app_food_order_show', [ + 'id' => $orderItem->getFoodOrder()->getId(), + ], Response::HTTP_SEE_OTHER); } #[Route('/{id}/edit', name: 'app_order_item_edit', methods: ['GET', 'POST'])] public function edit(Request $request, OrderItem $orderItem, EntityManagerInterface $entityManager): Response { + $foodOrder = $orderItem->getFoodOrder(); + if ($foodOrder->isClosed()) { + return $this->redirectToRoute('app_food_order_show', [ + 'id' => $foodOrder->getId(), + ], Response::HTTP_SEE_OTHER); + } $form = $this->createForm(OrderItemType::class, $orderItem); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $entityManager->flush(); - return $this->redirectToRoute('app_order_item_index', [], Response::HTTP_SEE_OTHER); + return $this->redirectToRoute('app_food_order_show', [ + 'id' => $orderItem->getFoodOrder()->getId(), + ], Response::HTTP_SEE_OTHER); } return $this->render('order_item/edit.html.twig', [ @@ -68,14 +90,19 @@ final class OrderItemController extends AbstractController ]); } - #[Route('/{id}', name: 'app_order_item_delete', methods: ['POST'])] - public function delete(Request $request, OrderItem $orderItem, EntityManagerInterface $entityManager): Response + #[Route('/{id}', name: 'app_order_item_delete')] + public function delete(OrderItem $orderItem, EntityManagerInterface $entityManager): Response { - if ($this->isCsrfTokenValid('delete' . $orderItem->getId(), $request->getPayload()->getString('_token'))) { - $entityManager->remove($orderItem); - $entityManager->flush(); + $foodOrder = $orderItem->getFoodOrder(); + if ($foodOrder->isClosed()) { + return $this->redirectToRoute('app_food_order_show', [ + 'id' => $foodOrder->getId(), + ], Response::HTTP_SEE_OTHER); } - - return $this->redirectToRoute('app_order_item_index', [], Response::HTTP_SEE_OTHER); + $entityManager->remove($orderItem); + $entityManager->flush(); + return $this->redirectToRoute('app_food_order_show', [ + 'id' => $orderItem->getFoodOrder()->getId(), + ], Response::HTTP_SEE_OTHER); } } diff --git a/src/Repository/FoodOrderRepository.php b/src/Repository/FoodOrderRepository.php index 33407c8..fec91cb 100644 --- a/src/Repository/FoodOrderRepository.php +++ b/src/Repository/FoodOrderRepository.php @@ -23,4 +23,19 @@ final class FoodOrderRepository extends ServiceEntityRepository $this->getEntityManager() ->flush(); } + + /** + * @return FoodOrder[] + */ + public function findLatestEntries(int $limit = 10): array + { + $qb = $this->createQueryBuilder('alias'); + + $qb->orderBy('alias.createdAt', 'DESC'); + $qb->setMaxResults($limit); + + $query = $qb->getQuery(); + + return $query->getResult(); + } } diff --git a/templates/food_order/show.html.twig b/templates/food_order/show.html.twig index b058205..556de6a 100644 --- a/templates/food_order/show.html.twig +++ b/templates/food_order/show.html.twig @@ -21,11 +21,39 @@ - back to list {% if(food_order.isClosed) %} reopen {% else %} close {% endif %} + +

Items

+ + + + + + + + + + {% for item in food_order.orderItems %} + + + + + + {% endfor %} + +
nameextrasactions
{{ item.name }}{{ item.extras }} + {% if(food_order.isClosed) %} + {% else %} + edit + copy + remove + {% endif %} +
+ New Item + {% endblock %} diff --git a/templates/order_item/_delete_form.html.twig b/templates/order_item/_delete_form.html.twig deleted file mode 100644 index a36a5a5..0000000 --- a/templates/order_item/_delete_form.html.twig +++ /dev/null @@ -1,4 +0,0 @@ -
- - -
diff --git a/templates/order_item/edit.html.twig b/templates/order_item/edit.html.twig index c640aec..2f85f44 100644 --- a/templates/order_item/edit.html.twig +++ b/templates/order_item/edit.html.twig @@ -7,7 +7,5 @@ {{ include('order_item/_form.html.twig', {'button_label': 'Update'}) }} - back to list - - {{ include('order_item/_delete_form.html.twig') }} + back to list {% endblock %} diff --git a/templates/order_item/index.html.twig b/templates/order_item/index.html.twig deleted file mode 100644 index feace1e..0000000 --- a/templates/order_item/index.html.twig +++ /dev/null @@ -1,37 +0,0 @@ -{% extends 'base.html.twig' %} - -{% block title %}OrderItem index{% endblock %} - -{% block body %} -

OrderItem index

- - - - - - - - - - - - {% for order_item in order_items %} - - - - - - - {% else %} - - - - {% endfor %} - -
IdNameExtrasactions
{{ order_item.id }}{{ order_item.name }}{{ order_item.extras }} - show - edit -
no records found
- - Create new -{% endblock %} diff --git a/templates/order_item/new.html.twig b/templates/order_item/new.html.twig index 0e9cf7e..bd4ac99 100644 --- a/templates/order_item/new.html.twig +++ b/templates/order_item/new.html.twig @@ -7,5 +7,5 @@ {{ include('order_item/_form.html.twig') }} - back to list + back to list {% endblock %} diff --git a/templates/order_item/show.html.twig b/templates/order_item/show.html.twig deleted file mode 100644 index 2612685..0000000 --- a/templates/order_item/show.html.twig +++ /dev/null @@ -1,30 +0,0 @@ -{% extends 'base.html.twig' %} - -{% block title %}OrderItem{% endblock %} - -{% block body %} -

OrderItem

- - - - - - - - - - - - - - - - -
Id{{ order_item.id }}
Name{{ order_item.name }}
Extras{{ order_item.extras }}
- - back to list - - edit - - {{ include('order_item/_delete_form.html.twig') }} -{% endblock %} diff --git a/tests/Controller/OrderItemControllerTest.php b/tests/Controller/OrderItemControllerTest.php index 32821cd..f6251d5 100644 --- a/tests/Controller/OrderItemControllerTest.php +++ b/tests/Controller/OrderItemControllerTest.php @@ -12,17 +12,6 @@ final class OrderItemControllerTest extends DbWebTest { private string $path = '/order/item/'; - public function testIndex(): void - { - $this->client->request('GET', $this->path); - - self::assertResponseStatusCodeSame(200); - self::assertPageTitleContains('OrderItem index'); - - // Use the $crawler to perform additional assertions e.g. - // self::assertSame('Some text on the page', $crawler->filter('.p')->first()); - } - public function testNew(): void { $this->markTestIncomplete(); -- 2.39.5 From b18ee3cbd5e438cd73fb5b5e3cafae895da8714d Mon Sep 17 00:00:00 2001 From: Continuous Integration Date: Fri, 14 Jun 2024 16:56:37 +0000 Subject: [PATCH 2/2] Continuous Integration Fixes --- src/Command/FakeDataCommand.php | 21 +++++++++++---------- src/Controller/OrderItemController.php | 9 ++++++--- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Command/FakeDataCommand.php b/src/Command/FakeDataCommand.php index 1eceac3..82e1fd6 100644 --- a/src/Command/FakeDataCommand.php +++ b/src/Command/FakeDataCommand.php @@ -1,32 +1,33 @@ -setName('Food Vendor ' . $i); $this->entityManager->persist($vendor); $vendors[] = $vendor; @@ -64,7 +65,7 @@ class FakeDataCommand extends Command { $orders = []; foreach (range(1, $amount) as $i) { - $order = new FoodOrder(); + $order = new FoodOrder; $order->setFoodVendor($vendor); if ($i % 2 === 0) { $order->close(); diff --git a/src/Controller/OrderItemController.php b/src/Controller/OrderItemController.php index 1716839..e9f17c3 100644 --- a/src/Controller/OrderItemController.php +++ b/src/Controller/OrderItemController.php @@ -60,7 +60,8 @@ final class OrderItemController extends AbstractController $entityManager->persist($newOrderItem); $entityManager->flush(); return $this->redirectToRoute('app_food_order_show', [ - 'id' => $orderItem->getFoodOrder()->getId(), + 'id' => $orderItem->getFoodOrder() + ->getId(), ], Response::HTTP_SEE_OTHER); } @@ -80,7 +81,8 @@ final class OrderItemController extends AbstractController $entityManager->flush(); return $this->redirectToRoute('app_food_order_show', [ - 'id' => $orderItem->getFoodOrder()->getId(), + 'id' => $orderItem->getFoodOrder() + ->getId(), ], Response::HTTP_SEE_OTHER); } @@ -102,7 +104,8 @@ final class OrderItemController extends AbstractController $entityManager->remove($orderItem); $entityManager->flush(); return $this->redirectToRoute('app_food_order_show', [ - 'id' => $orderItem->getFoodOrder()->getId(), + 'id' => $orderItem->getFoodOrder() + ->getId(), ], Response::HTTP_SEE_OTHER); } } -- 2.39.5