add order item stuff #5

Merged
lubiana merged 2 commits from order_items into main 2024-06-14 16:57:42 +00:00
11 changed files with 175 additions and 113 deletions
Showing only changes of commit 814dc4a41b - Show all commits

View file

@ -0,0 +1,76 @@
<?php
namespace App\Command;
use App\Entity\FoodOrder;
use App\Entity\FoodVendor;
use Doctrine\Common\Collections\Order;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:fake-data',
description: 'Add a short description for your command',
)]
class FakeDataCommand extends Command
{
public function __construct(
private EntityManagerInterface $entityManager,
)
{
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$arg1 = $input->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;
}
}

View file

@ -18,7 +18,7 @@ final class FoodOrderController extends AbstractController
public function index(FoodOrderRepository $foodOrderRepository): Response public function index(FoodOrderRepository $foodOrderRepository): Response
{ {
return $this->render('food_order/index.html.twig', [ return $this->render('food_order/index.html.twig', [
'food_orders' => $foodOrderRepository->findAll(), 'food_orders' => $foodOrderRepository->findLatestEntries(),
]); ]);
} }

View file

@ -2,9 +2,9 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\FoodOrder;
use App\Entity\OrderItem; use App\Entity\OrderItem;
use App\Form\OrderItemType; use App\Form\OrderItemType;
use App\Repository\OrderItemRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -14,52 +14,74 @@ use Symfony\Component\Routing\Attribute\Route;
#[Route('/order/item')] #[Route('/order/item')]
final class OrderItemController extends AbstractController final class OrderItemController extends AbstractController
{ {
#[Route('/', name: 'app_order_item_index', methods: ['GET'])] #[Route('/new/{foodOrder}', name: 'app_order_item_new', methods: ['GET', 'POST'])]
public function index(OrderItemRepository $orderItemRepository): Response public function new(Request $request, FoodOrder $foodOrder, EntityManagerInterface $entityManager): 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
{ {
if ($foodOrder->isClosed()) {
return $this->redirectToRoute('app_food_order_show', [
'id' => $foodOrder->getId(),
], Response::HTTP_SEE_OTHER);
}
$orderItem = new OrderItem; $orderItem = new OrderItem;
$form = $this->createForm(OrderItemType::class, $orderItem); $form = $this->createForm(OrderItemType::class, $orderItem);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$orderItem->setFoodOrder($foodOrder);
$entityManager->persist($orderItem); $entityManager->persist($orderItem);
$entityManager->flush(); $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', [ return $this->render('order_item/new.html.twig', [
'order_item' => $orderItem, 'order_item' => $orderItem,
'food_order' => $foodOrder,
'form' => $form, 'form' => $form,
]); ]);
} }
#[Route('/{id}', name: 'app_order_item_show', methods: ['GET'])] #[Route('/{id}/copy', name: 'app_order_item_copy', methods: ['GET'])]
public function show(OrderItem $orderItem): Response public function copy(OrderItem $orderItem, EntityManagerInterface $entityManager): Response
{ {
return $this->render('order_item/show.html.twig', [ $foodOrder = $orderItem->getFoodOrder();
'order_item' => $orderItem, 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'])] #[Route('/{id}/edit', name: 'app_order_item_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, OrderItem $orderItem, EntityManagerInterface $entityManager): Response 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 = $this->createForm(OrderItemType::class, $orderItem);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush(); $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', [ 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'])] #[Route('/{id}', name: 'app_order_item_delete')]
public function delete(Request $request, OrderItem $orderItem, EntityManagerInterface $entityManager): Response public function delete(OrderItem $orderItem, EntityManagerInterface $entityManager): Response
{ {
if ($this->isCsrfTokenValid('delete' . $orderItem->getId(), $request->getPayload()->getString('_token'))) { $foodOrder = $orderItem->getFoodOrder();
$entityManager->remove($orderItem); if ($foodOrder->isClosed()) {
$entityManager->flush(); return $this->redirectToRoute('app_food_order_show', [
'id' => $foodOrder->getId(),
], Response::HTTP_SEE_OTHER);
} }
$entityManager->remove($orderItem);
return $this->redirectToRoute('app_order_item_index', [], Response::HTTP_SEE_OTHER); $entityManager->flush();
return $this->redirectToRoute('app_food_order_show', [
'id' => $orderItem->getFoodOrder()->getId(),
], Response::HTTP_SEE_OTHER);
} }
} }

View file

@ -23,4 +23,19 @@ final class FoodOrderRepository extends ServiceEntityRepository
$this->getEntityManager() $this->getEntityManager()
->flush(); ->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();
}
} }

View file

@ -21,11 +21,39 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<a class="button" href="{{ path('app_food_order_index') }}">back to list</a> <a class="button" href="{{ path('app_food_order_index') }}">back to list</a>
{% if(food_order.isClosed) %} {% if(food_order.isClosed) %}
<a class="button" href="{{ path('app_food_order_open', {'id': food_order.id}) }}">reopen</a> <a class="button" href="{{ path('app_food_order_open', {'id': food_order.id}) }}">reopen</a>
{% else %} {% else %}
<a class="button" href="{{ path('app_food_order_close', {'id': food_order.id}) }}">close</a> <a class="button" href="{{ path('app_food_order_close', {'id': food_order.id}) }}">close</a>
{% endif %} {% endif %}
<h2>Items</h2>
<table class="table">
<thead>
<tr>
<th>name</th>
<th>extras</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for item in food_order.orderItems %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.extras }}</td>
<td>
{% if(food_order.isClosed) %}
{% else %}
<a href="{{ path('app_order_item_edit', {'id': item.id}) }}">edit</a>
<a href="{{ path('app_order_item_copy', {'id': item.id}) }}">copy</a>
<a href="{{ path('app_order_item_delete', {'id': item.id}) }}">remove</a>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<a class="button" href="{{ path('app_order_item_new', {'foodOrder': food_order.id}) }}">New Item</a>
{% endblock %} {% endblock %}

View file

@ -1,4 +0,0 @@
<form method="post" action="{{ path('app_order_item_delete', {'id': order_item.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ order_item.id) }}">
<button class="btn">Delete</button>
</form>

View file

@ -7,7 +7,5 @@
{{ include('order_item/_form.html.twig', {'button_label': 'Update'}) }} {{ include('order_item/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_order_item_index') }}">back to list</a> <a href="{{ path('app_food_order_show', {'id': order_item.foodOrder.id}) }}">back to list</a>
{{ include('order_item/_delete_form.html.twig') }}
{% endblock %} {% endblock %}

View file

@ -1,37 +0,0 @@
{% extends 'base.html.twig' %}
{% block title %}OrderItem index{% endblock %}
{% block body %}
<h1>OrderItem index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Extras</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for order_item in order_items %}
<tr>
<td>{{ order_item.id }}</td>
<td>{{ order_item.name }}</td>
<td>{{ order_item.extras }}</td>
<td>
<a href="{{ path('app_order_item_show', {'id': order_item.id}) }}">show</a>
<a href="{{ path('app_order_item_edit', {'id': order_item.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="4">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('app_order_item_new') }}">Create new</a>
{% endblock %}

View file

@ -7,5 +7,5 @@
{{ include('order_item/_form.html.twig') }} {{ include('order_item/_form.html.twig') }}
<a href="{{ path('app_order_item_index') }}">back to list</a> <a class="button" href="{{ path('app_food_order_show', { 'id': food_order.id}) }}">back to list</a>
{% endblock %} {% endblock %}

View file

@ -1,30 +0,0 @@
{% extends 'base.html.twig' %}
{% block title %}OrderItem{% endblock %}
{% block body %}
<h1>OrderItem</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ order_item.id }}</td>
</tr>
<tr>
<th>Name</th>
<td>{{ order_item.name }}</td>
</tr>
<tr>
<th>Extras</th>
<td>{{ order_item.extras }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('app_order_item_index') }}">back to list</a>
<a href="{{ path('app_order_item_edit', {'id': order_item.id}) }}">edit</a>
{{ include('order_item/_delete_form.html.twig') }}
{% endblock %}

View file

@ -12,17 +12,6 @@ final class OrderItemControllerTest extends DbWebTest
{ {
private string $path = '/order/item/'; 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 public function testNew(): void
{ {
$this->markTestIncomplete(); $this->markTestIncomplete();