#42: allow updates to menuitems
All checks were successful
/ ls (pull_request) Successful in 37s
/ ls (push) Successful in 39s
/ ls (release) Successful in 25s

This commit is contained in:
Jonas 2024-07-29 13:04:57 +02:00
parent 0068654885
commit 674adcba60
Signed by: lubiana
SSH key fingerprint: SHA256:gkqM8DUX4Blf6P52fycW8ISTd+4eAHH+Uzu9iyc8hAM
14 changed files with 258 additions and 7 deletions

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240815151510 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE menu_item ADD COLUMN deleted_at DATETIME DEFAULT NULL');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TEMPORARY TABLE __temp__menu_item AS SELECT id, name, food_vendor_id FROM menu_item');
$this->addSql('DROP TABLE menu_item');
$this->addSql('CREATE TABLE menu_item (id BLOB NOT NULL, name VARCHAR(255) NOT NULL, food_vendor_id BLOB NOT NULL, PRIMARY KEY(id), CONSTRAINT FK_D754D5506EF983E8 FOREIGN KEY (food_vendor_id) REFERENCES food_vendor (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('INSERT INTO menu_item (id, name, food_vendor_id) SELECT id, name, food_vendor_id FROM __temp__menu_item');
$this->addSql('DROP TABLE __temp__menu_item');
$this->addSql('CREATE INDEX IDX_D754D5506EF983E8 ON menu_item (food_vendor_id)');
}
}

View file

@ -0,0 +1,53 @@
<?php declare(strict_types=1);
namespace App\Controller;
use App\Entity\MenuItem;
use App\Form\MenuItemType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/menu/item')]
final class MenuItemController extends AbstractController
{
#[Route('/{id}', name: 'app_menu_item_show', methods: ['GET'])]
public function show(MenuItem $menuItem): Response
{
return $this->render('menu_item/show.html.twig', [
'menu_item' => $menuItem,
]);
}
#[Route('/{id}/edit', name: 'app_menu_item_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, MenuItem $menuItem, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(MenuItemType::class, $menuItem);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_menu_item_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('menu_item/edit.html.twig', [
'menu_item' => $menuItem,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_menu_item_delete', methods: ['POST'])]
public function delete(Request $request, MenuItem $menuItem, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete' . $menuItem->getId(), $request->getPayload()->getString('_token'))) {
$menuItem->delete();
$entityManager->persist($menuItem);
$entityManager->flush();
}
return $this->redirectToRoute('app_menu_item_index', [], Response::HTTP_SEE_OTHER);
}
}

View file

@ -88,10 +88,15 @@ class FoodVendor
/** /**
* @return Collection<int, MenuItem> * @return Collection<int, MenuItem>
*/ */
public function getMenuItems(): Collection public function getMenuItems(bool $withDeleted = false): Collection
{ {
if ($withDeleted) {
return $this->menuItems; return $this->menuItems;
} }
return $this->menuItems->filter(
static fn(MenuItem $item): bool => $item->isDeleted() === false
);
}
public function addMenuItem(MenuItem $menuItem): static public function addMenuItem(MenuItem $menuItem): static
{ {

View file

@ -3,6 +3,7 @@
namespace App\Entity; namespace App\Entity;
use App\Repository\MenuItemRepository; use App\Repository\MenuItemRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator; use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
use Symfony\Bridge\Doctrine\Types\UlidType; use Symfony\Bridge\Doctrine\Types\UlidType;
@ -24,6 +25,9 @@ class MenuItem
#[ORM\JoinColumn(nullable: false)] #[ORM\JoinColumn(nullable: false)]
private FoodVendor|null $foodVendor = null; private FoodVendor|null $foodVendor = null;
#[ORM\Column(nullable: true)]
private DateTimeImmutable|null $deletedAt = null;
public function getId(): Ulid|null public function getId(): Ulid|null
{ {
return $this->id; return $this->id;
@ -52,4 +56,27 @@ class MenuItem
return $this; return $this;
} }
public function isDeleted(): bool
{
return $this->getDeletedAt() instanceof DateTimeImmutable;
}
public function delete(): static
{
$this->setDeletedAt();
return $this;
}
public function getDeletedAt(): DateTimeImmutable|null
{
return $this->deletedAt;
}
public function setDeletedAt(DateTimeImmutable|null $deletedAt = new DateTimeImmutable): static
{
$this->deletedAt = $deletedAt;
return $this;
}
} }

28
src/Form/MenuItemType.php Normal file
View file

@ -0,0 +1,28 @@
<?php declare(strict_types=1);
namespace App\Form;
use App\Entity\MenuItem;
use Override;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
final class MenuItemType extends AbstractType
{
#[Override]
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name')
;
}
#[Override]
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => MenuItem::class,
]);
}
}

View file

@ -5,7 +5,14 @@
<title>{% block title %}Welcome!{% endblock %}</title> <title>{% block title %}Welcome!{% endblock %}</title>
<link rel="icon" type="image/svg+xml" <link rel="icon" type="image/svg+xml"
href="{{ favicon }}" /> href="{{ favicon }}" />
{% set currentDate = "now"|date("d") %}
{% if currentDate % 3 == 0 %}
<link rel="stylesheet" href="/static/css/new.min.css"> <link rel="stylesheet" href="/static/css/new.min.css">
{% elseif currentDate % 3 == 1 %}
<link rel="stylesheet" href="/static/css/simple.min.css">
{% else %}
<link rel="stylesheet" href="/static/css/water.min.css">
{% endif %}
<style> <style>
label{ label{
display: block; display: block;

View file

@ -16,9 +16,13 @@
<section> <section>
<h2>known menuitems</h2> <h2>known menuitems</h2>
<ul>
{% for item in food_vendor.menuItems %} {% for item in food_vendor.menuItems %}
<code>{{ item.name }}</code> <li>
<a href="{{ path('app_menu_item_show', {'id': item.id}) }}">{{ item.name }}</a>
</li>
{% endfor %} {% endfor %}
</ul>
</section> </section>

View file

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

View file

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

View file

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}Edit MenuItem{% endblock %}
{% block body %}
<h1>Edit MenuItem</h1>
{{ include('menu_item/_form.html.twig', {'button_label': 'Update'}) }}
{{ include('menu_item/_delete_form.html.twig') }}
{% endblock %}

View file

@ -0,0 +1,35 @@
{% extends 'base.html.twig' %}
{% block title %}MenuItem index{% endblock %}
{% block body %}
<h1>MenuItem index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for menu_item in menu_items %}
<tr>
<td>{{ menu_item.id }}</td>
<td>{{ menu_item.name }}</td>
<td>
<a href="{{ path('app_menu_item_show', {'id': menu_item.id}) }}">show</a>
<a href="{{ path('app_menu_item_edit', {'id': menu_item.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="3">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('app_menu_item_new') }}">Create new</a>
{% endblock %}

View file

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New MenuItem{% endblock %}
{% block body %}
<h1>Create new MenuItem</h1>
{{ include('menu_item/_form.html.twig') }}
<a href="{{ path('app_menu_item_index') }}">back to list</a>
{% endblock %}

View file

@ -0,0 +1,26 @@
{% extends 'base.html.twig' %}
{% block title %}MenuItem{% endblock %}
{% block body %}
<h1>MenuItem</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ menu_item.id }}</td>
</tr>
<tr>
<th>Name</th>
<td>{{ menu_item.name }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('app_food_vendor_show', { 'id': menu_item.foodVendor.id}) }}">back to list</a>
<a href="{{ path('app_menu_item_edit', {'id': menu_item.id}) }}">edit</a>
{{ include('menu_item/_delete_form.html.twig') }}
{% endblock %}

View file

@ -95,7 +95,7 @@ final class FoodVendorControllerTest extends DbWebTest
->last(); ->last();
$this->assertSame('My Title', $nameNode->text()); $this->assertSame('My Title', $nameNode->text());
$itemNodes = $crawler->filter('code'); $itemNodes = $crawler->filter('li');
$this->assertCount(4, $itemNodes); $this->assertCount(4, $itemNodes);
} }