Merge branch 'feature/MissionController' into develop

This commit is contained in:
pressatl 2024-12-20 09:09:29 +01:00
commit ef7551d755
9 changed files with 222 additions and 23 deletions

View File

@ -0,0 +1,81 @@
<?php
namespace App\Controller;
use App\Entity\Mission;
use App\Form\MissionType;
use App\Repository\MissionRepository;
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('/mission', name: 'mission')]
final class MissionController extends AbstractController
{
#[Route(name: '_index', methods: ['GET'])]
public function index(MissionRepository $missionRepository): Response
{
return $this->render('mission/index.html.twig', [
'missions' => $missionRepository->findAll(),
]);
}
#[Route('/new', name: '_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
{
$mission = new Mission();
$form = $this->createForm(MissionType::class, $mission);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($mission);
$entityManager->flush();
return $this->redirectToRoute('mission_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('mission/new.html.twig', [
'mission' => $mission,
'form' => $form,
]);
}
#[Route('/{id}', name: '_show', methods: ['GET'])]
public function show(Mission $mission): Response
{
return $this->render('mission/show.html.twig', [
'mission' => $mission,
]);
}
#[Route('/{id}/edit', name: '_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Mission $mission, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(MissionType::class, $mission);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('mission_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('mission/edit.html.twig', [
'mission' => $mission,
'form' => $form,
]);
}
#[Route('/{id}', name: '_delete', methods: ['POST'])]
public function delete(Request $request, Mission $mission, EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$mission->getId(), $request->getPayload()->getString('_token'))) {
$entityManager->remove($mission);
$entityManager->flush();
}
return $this->redirectToRoute('mission_index', [], Response::HTTP_SEE_OTHER);
}
}

25
src/Form/MissionType.php Normal file
View File

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

View File

@ -16,28 +16,28 @@ class MissionRepository extends ServiceEntityRepository
parent::__construct($registry, Mission::class);
}
// /**
// * @return Mission[] Returns an array of Mission objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('m')
// ->andWhere('m.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('m.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
/**
* @return Mission[] Returns an array of Mission objects
*/
public function findByExampleField($value): array
{
return $this->createQueryBuilder('m')
->andWhere('m.exampleField = :val')
->setParameter('val', $value)
->orderBy('m.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
// public function findOneBySomeField($value): ?Mission
// {
// return $this->createQueryBuilder('m')
// ->andWhere('m.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
public function findOneBySomeField($value): ?Mission
{
return $this->createQueryBuilder('m')
->andWhere('m.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
}

View File

@ -0,0 +1,4 @@
<form method="post" action="{{ path('_delete', {'id': mission.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ mission.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,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit Mission{% endblock %}
{% block body %}
<h1>Edit Mission</h1>
{{ include('mission/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('app_mission_index') }}">back to list</a>
{{ include('mission/_delete_form.html.twig') }}
{% endblock %}

View File

@ -0,0 +1,35 @@
{% extends 'base.html.twig' %}
{% block title %}Mission index{% endblock %}
{% block body %}
<h1>Mission index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Label</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for mission in missions %}
<tr>
<td>{{ mission.id }}</td>
<td>{{ mission.label }}</td>
<td>
<a href="{{ path('mission_show', {'id': mission.id}) }}">show</a>
<a href="{{ path('mission_edit', {'id': mission.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="3">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('mission_new') }}">Create new</a>
{% endblock %}

View File

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

View File

@ -0,0 +1,26 @@
{% extends 'base.html.twig' %}
{% block title %}Mission{% endblock %}
{% block body %}
<h1>Mission</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ mission.id }}</td>
</tr>
<tr>
<th>Label</th>
<td>{{ mission.label }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('_index') }}">back to list</a>
<a href="{{ path('_edit', {'id': mission.id}) }}">edit</a>
{{ include('mission/_delete_form.html.twig') }}
{% endblock %}