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 179 additions and 113 deletions

View file

@ -0,0 +1,77 @@
<?php declare(strict_types=1);
namespace App\Command;
use App\Entity\FoodOrder;
use App\Entity\FoodVendor;
use Doctrine\ORM\EntityManagerInterface;
use Override;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function range;
use function sprintf;
#[AsCommand(
name: 'app:fake-data',
description: 'Add a short description for your command',
)]
final class FakeDataCommand extends Command
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
) {
parent::__construct();
}
#[Override]
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
{
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;
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
#[Route('/new/{foodOrder}', name: 'app_order_item_new', methods: ['GET', 'POST'])]
public function new(Request $request, FoodOrder $foodOrder, EntityManagerInterface $entityManager): Response
{
return $this->render('order_item/index.html.twig', [
'order_items' => $orderItemRepository->findAll(),
]);
if ($foodOrder->isClosed()) {
return $this->redirectToRoute('app_food_order_show', [
'id' => $foodOrder->getId(),
], Response::HTTP_SEE_OTHER);
}
#[Route('/new', name: 'app_order_item_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$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'))) {
$foodOrder = $orderItem->getFoodOrder();
if ($foodOrder->isClosed()) {
return $this->redirectToRoute('app_food_order_show', [
'id' => $foodOrder->getId(),
], Response::HTTP_SEE_OTHER);
}
$entityManager->remove($orderItem);
$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);
}
}

View file

@ -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();
}
}

View file

@ -21,11 +21,39 @@
</tr>
</tbody>
</table>
<a class="button" href="{{ path('app_food_order_index') }}">back to list</a>
{% if(food_order.isClosed) %}
<a class="button" href="{{ path('app_food_order_open', {'id': food_order.id}) }}">reopen</a>
{% else %}
<a class="button" href="{{ path('app_food_order_close', {'id': food_order.id}) }}">close</a>
{% 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 %}

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'}) }}
<a href="{{ path('app_order_item_index') }}">back to list</a>
{{ include('order_item/_delete_form.html.twig') }}
<a href="{{ path('app_food_order_show', {'id': order_item.foodOrder.id}) }}">back to list</a>
{% 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') }}
<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 %}

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/';
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();