add orm and tests

This commit is contained in:
lubiana 2024-06-10 20:22:44 +02:00
parent a32bf4ce7d
commit a541338909
No known key found for this signature in database
23 changed files with 1666 additions and 23 deletions

View file

@ -0,0 +1,81 @@
<?php
namespace App\Controller;
use App\Entity\FoodVendor;
use App\Form\FoodVendorType;
use App\Repository\FoodVendorRepository;
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('/food/vendor')]
class FoodVendorController extends AbstractController
{
#[Route('/', name: 'app_food_vendor_index', methods: ['GET'])]
public function index(FoodVendorRepository $foodVendorRepository): Response
{
return $this->render('food_vendor/index.html.twig', [
'food_vendors' => $foodVendorRepository->findAll(),
]);
}
#[Route('/new', name: 'app_food_vendor_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$foodVendor = new FoodVendor();
$form = $this->createForm(FoodVendorType::class, $foodVendor);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($foodVendor);
$entityManager->flush();
return $this->redirectToRoute('app_food_vendor_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('food_vendor/new.html.twig', [
'food_vendor' => $foodVendor,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_food_vendor_show', methods: ['GET'])]
public function show(FoodVendor $foodVendor): Response
{
return $this->render('food_vendor/show.html.twig', [
'food_vendor' => $foodVendor,
]);
}
#[Route('/{id}/edit', name: 'app_food_vendor_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, FoodVendor $foodVendor, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(FoodVendorType::class, $foodVendor);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_food_vendor_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('food_vendor/edit.html.twig', [
'food_vendor' => $foodVendor,
'form' => $form,
]);
}
#[Route('/{id}', name: 'app_food_vendor_delete', methods: ['POST'])]
public function delete(Request $request, FoodVendor $foodVendor, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$foodVendor->getId(), $request->getPayload()->getString('_token'))) {
$entityManager->remove($foodVendor);
$entityManager->flush();
}
return $this->redirectToRoute('app_food_vendor_index', [], Response::HTTP_SEE_OTHER);
}
}

39
src/Entity/FoodVendor.php Normal file
View file

@ -0,0 +1,39 @@
<?php
namespace App\Entity;
use App\Repository\FoodVendorRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Component\Uid\Ulid;
#[ORM\Entity(repositoryClass: FoodVendorRepository::class)]
class FoodVendor
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\Column(type: UlidType::NAME, unique: true)]
#[ORM\CustomIdGenerator(class: UlidGenerator::class)]
private Ulid|null $id = null;
#[ORM\Column(length: 50)]
private ?string $name = null;
public function getId(): ?Ulid
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
}

View file

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

View file

@ -0,0 +1,18 @@
<?php
namespace App\Repository;
use App\Entity\FoodVendor;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<FoodVendor>
*/
class FoodVendorRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, FoodVendor::class);
}
}