#44: show item name on orderitem editform
All checks were successful
/ ls (pull_request) Successful in 35s
/ ls (push) Successful in 36s
/ ls (release) Successful in 28s

This commit is contained in:
Jonas 2024-08-14 20:39:14 +02:00
parent 25b73e7da5
commit c324973d28
Signed by: lubiana
SSH key fingerprint: SHA256:gkqM8DUX4Blf6P52fycW8ISTd+4eAHH+Uzu9iyc8hAM
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'])] #[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(); $foodOrder = $orderItem->getFoodOrder();
if ($foodOrder->isClosed()) { if ($foodOrder->isClosed()) {
return $this->redirectToRoute('app_food_order_show', [ return $this->redirectToRoute('app_food_order_show', [
'id' => $foodOrder->getId(), 'id' => $foodOrder->getId(),
], Response::HTTP_SEE_OTHER); ], Response::HTTP_SEE_OTHER);
} }
$orderItem->setName($orderItem->getMenuItem()->getName());
$form = $this->createForm(OrderItemType::class, $orderItem); $form = $this->createForm(OrderItemType::class, $orderItem);
$form->setData($orderItem);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { 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(); $entityManager->flush();
return $this->redirectToRoute('app_food_order_show', [ 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 public function buildForm(FormBuilderInterface $builder, array $options): void
{ {
$builder $builder
->add(child: 'name', options: [ ->add('name')
'data' => $options['name'] ?? '',
])
->add('extras') ->add('extras')
->add('createdBy') ->add('createdBy')
; ;

View file

@ -101,6 +101,31 @@ final class OrderItemControllerTest extends DbWebTest
self::assertSame(0, $this->repository->count([])); 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 public function testCopy(): void
{ {
$orderItem = new OrderItem; $orderItem = new OrderItem;
@ -123,7 +148,6 @@ final class OrderItemControllerTest extends DbWebTest
$this->assertSame($orderItem->getName(), $item->getName()); $this->assertSame($orderItem->getName(), $item->getName());
$this->assertSame($orderItem->getExtras(), $item->getExtras()); $this->assertSame($orderItem->getExtras(), $item->getExtras());
} }
} }
#[Override] #[Override]