diff --git a/src/Command/FakeDataCommand.php b/src/Command/FakeDataCommand.php new file mode 100644 index 0000000..82e1fd6 --- /dev/null +++ b/src/Command/FakeDataCommand.php @@ -0,0 +1,77 @@ +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..e9f17c3 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,76 @@ 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 +92,20 @@ 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();