#44: show item name on orderitem editform #47

Merged
lubiana merged 1 commit from #44_fix-orderitem-edit into main 2024-08-14 18:40:22 +00:00
3 changed files with 49 additions and 6 deletions

View file

@ -90,18 +90,39 @@ final class OrderItemController extends AbstractController
}
#[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,
MenuItemRepository $menuItemRepository,
): Response {
$foodOrder = $orderItem->getFoodOrder();
if ($foodOrder->isClosed()) {
return $this->redirectToRoute('app_food_order_show', [
'id' => $foodOrder->getId(),
], Response::HTTP_SEE_OTHER);
}
$orderItem->setName($orderItem->getMenuItem()->getName());
$form = $this->createForm(OrderItemType::class, $orderItem);
$form->setData($orderItem);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$menuItem = $menuItemRepository->findOneBy([
'name' => $orderItem->getName(),
'foodVendor' => $foodOrder->getFoodVendor(),
]);
if ($menuItem === null) {
$menuItem = new MenuItem;
$menuItem->setName($orderItem->getName());
$menuItem->setFoodVendor($foodOrder->getFoodVendor());
$entityManager->persist($menuItem);
}
$orderItem->setMenuItem($menuItem);
$orderItem->setFoodOrder($foodOrder);
$entityManager->persist($orderItem);
$entityManager->flush();
return $this->redirectToRoute('app_food_order_show', [

View file

@ -14,9 +14,7 @@ final class OrderItemType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add(child: 'name', options: [
'data' => $options['name'] ?? '',
])
->add('name')
->add('extras')
->add('createdBy')
;

View file

@ -101,6 +101,31 @@ final class OrderItemControllerTest extends DbWebTest
self::assertSame(0, $this->repository->count([]));
}
public function testEdit(): void
{
$orderItem = new OrderItem;
$orderItem->setName('Testing');
$orderItem->setExtras('My Extra');
$orderItem->setFoodOrder($this->order);
$orderItem->setMenuItem($this->menuItem);
$this->manager->persist($orderItem);
$this->manager->flush();
$crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $orderItem->getId()));
$nameElem = $crawler->filter('#order_item_name');
$extrasElem = $crawler->filter('#order_item_extras');
$this->assertEquals(
'Testing',
$nameElem->attr('value')
);
$this->assertEquals(
'My Extra',
$extrasElem->attr('value')
);
}
public function testCopy(): void
{
$orderItem = new OrderItem;
@ -123,7 +148,6 @@ final class OrderItemControllerTest extends DbWebTest
$this->assertSame($orderItem->getName(), $item->getName());
$this->assertSame($orderItem->getExtras(), $item->getExtras());
}
}
#[Override]