Working on employee backend
This commit is contained in:
parent
e607897c5b
commit
de2ab661b5
@ -2,17 +2,80 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Employee;
|
||||
use App\Form\EmployeeType;
|
||||
use App\Repository\EmployeeRepository;
|
||||
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;
|
||||
|
||||
class EmployeeController extends AbstractController
|
||||
#[Route('/employee', name: 'employee')]
|
||||
final class EmployeeController extends AbstractController
|
||||
{
|
||||
#[Route('/employee', name: 'app_employee')]
|
||||
public function index(): Response
|
||||
#[Route(name: '_index', methods: ['GET'])]
|
||||
public function index(EmployeeRepository $employeeRepository): Response
|
||||
{
|
||||
return $this->render('employee/index.html.twig', [
|
||||
'controller_name' => 'EmployeeController',
|
||||
'employees' => $employeeRepository->findAll(),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/new', name: '_new', methods: ['GET', 'POST'])]
|
||||
public function new(Request $request, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$employee = new Employee();
|
||||
$form = $this->createForm(EmployeeType::class, $employee);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->persist($employee);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('employee_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('employee/new.html.twig', [
|
||||
'employee' => $employee,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: '_show', methods: ['GET'])]
|
||||
public function show(Employee $employee): Response
|
||||
{
|
||||
return $this->render('employee/show.html.twig', [
|
||||
'employee' => $employee,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}/edit', name: '_edit', methods: ['GET', 'POST'])]
|
||||
public function edit(Request $request, Employee $employee, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
$form = $this->createForm(EmployeeType::class, $employee);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('employee_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
|
||||
return $this->render('employee/edit.html.twig', [
|
||||
'employee' => $employee,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: '_delete', methods: ['POST'])]
|
||||
public function delete(Request $request, Employee $employee, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
if ($this->isCsrfTokenValid('delete'.$employee->getId(), $request->getPayload()->getString('_token'))) {
|
||||
$entityManager->remove($employee);
|
||||
$entityManager->flush();
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('employee_index', [], Response::HTTP_SEE_OTHER);
|
||||
}
|
||||
}
|
||||
|
42
src/Form/EmployeeType.php
Normal file
42
src/Form/EmployeeType.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Employee;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class EmployeeType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('email', EmailType::class, ['label' => 'Email Address'])
|
||||
->add('firstName', TextType::class, ['label' => 'First Name'])
|
||||
->add('lastName', TextType::class, ['label' => 'Last Name'])
|
||||
->add('password', PasswordType::class, ['label' => 'Password'])
|
||||
->add('roles', ChoiceType::class, [
|
||||
'label' => 'Roles (comma-separated)',
|
||||
'required' => false,
|
||||
'choices' => [
|
||||
'User' => 'ROLE_USER',
|
||||
'Admin' => 'ROLE_ADMIN',
|
||||
],
|
||||
'multiple' => true, // Allow multiple selections
|
||||
'expanded' => true, // Render as checkboxes
|
||||
])
|
||||
->add('save', SubmitType::class, ['label' => 'Add Employee']);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Employee::class,
|
||||
]);
|
||||
}
|
||||
}
|
@ -13,6 +13,13 @@
|
||||
{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
|
||||
{% for label, messages in app.flashes %}
|
||||
{% for message in messages %}
|
||||
<div class="alert alert-{{ label }}">{{ message }}</div>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
|
||||
<div class="ml-5 mr-5 ">
|
||||
<nav class="navbar navbar-expand-lg navbar-dark p-3 m-2 rounded hegre-navbar">
|
||||
<a class="navbar-brand " href="#">
|
||||
|
4
templates/employee/_delete_form.html.twig
Normal file
4
templates/employee/_delete_form.html.twig
Normal file
@ -0,0 +1,4 @@
|
||||
<form method="post" action="{{ path('app_employee_delete', {'id': employee.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
|
||||
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ employee.id) }}">
|
||||
<button class="btn">Delete</button>
|
||||
</form>
|
4
templates/employee/_form.html.twig
Normal file
4
templates/employee/_form.html.twig
Normal file
@ -0,0 +1,4 @@
|
||||
{{ form_start(form) }}
|
||||
{{ form_widget(form) }}
|
||||
<button class="btn">{{ button_label|default('Save') }}</button>
|
||||
{{ form_end(form) }}
|
13
templates/employee/edit.html.twig
Normal file
13
templates/employee/edit.html.twig
Normal file
@ -0,0 +1,13 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Edit Employee{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<h1>Edit Employee</h1>
|
||||
|
||||
{{ include('employee/_form.html.twig', {'button_label': 'Update'}) }}
|
||||
|
||||
<a href="{{ path('app_employee_index') }}">back to list</a>
|
||||
|
||||
{{ include('employee/_delete_form.html.twig') }}
|
||||
{% endblock %}
|
@ -1,20 +1,43 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Hello EmployeeController!{% endblock %}
|
||||
{% block title %}Employee index{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<style>
|
||||
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
|
||||
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
|
||||
</style>
|
||||
<h1>Employee index</h1>
|
||||
|
||||
<div class="example-wrapper">
|
||||
<h1>Hello {{ controller_name }}! ✅</h1>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<th>Email</th>
|
||||
<th>FirstName</th>
|
||||
<th>LastName</th>
|
||||
<th>Password</th>
|
||||
<th>Roles</th>
|
||||
<th>actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for employee in employees %}
|
||||
<tr>
|
||||
<td>{{ employee.id }}</td>
|
||||
<td>{{ employee.email }}</td>
|
||||
<td>{{ employee.firstName }}</td>
|
||||
<td>{{ employee.lastName }}</td>
|
||||
<td>{{ employee.password }}</td>
|
||||
<td>{{ employee.roles ? employee.roles|json_encode : '' }}</td>
|
||||
<td>
|
||||
<a href="{{ path('app_employee_show', {'id': employee.id}) }}">show</a>
|
||||
<a href="{{ path('app_employee_edit', {'id': employee.id}) }}">edit</a>
|
||||
</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="7">no records found</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
This friendly message is coming from:
|
||||
<ul>
|
||||
<li>Your controller at <code>/media/astiery/DEV/Boussarie/AP/HegreLand/src/Controller/EmployeeController.php</code></li>
|
||||
<li>Your template at <code>/media/astiery/DEV/Boussarie/AP/HegreLand/templates/employee/index.html.twig</code></li>
|
||||
</ul>
|
||||
</div>
|
||||
<a href="{{ path('app_employee_new') }}">Create new</a>
|
||||
{% endblock %}
|
||||
|
11
templates/employee/new.html.twig
Normal file
11
templates/employee/new.html.twig
Normal file
@ -0,0 +1,11 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}New Employee{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<h1>Create new Employee</h1>
|
||||
|
||||
{{ include('employee/_form.html.twig') }}
|
||||
|
||||
<a href="{{ path('app_employee_index') }}">back to list</a>
|
||||
{% endblock %}
|
42
templates/employee/show.html.twig
Normal file
42
templates/employee/show.html.twig
Normal file
@ -0,0 +1,42 @@
|
||||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block title %}Employee{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<h1>Employee</h1>
|
||||
|
||||
<table class="table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<td>{{ employee.id }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Email</th>
|
||||
<td>{{ employee.email }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>FirstName</th>
|
||||
<td>{{ employee.firstName }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>LastName</th>
|
||||
<td>{{ employee.lastName }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Password</th>
|
||||
<td>{{ employee.password }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Roles</th>
|
||||
<td>{{ employee.roles ? employee.roles|json_encode : '' }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<a href="{{ path('app_employee_index') }}">back to list</a>
|
||||
|
||||
<a href="{{ path('app_employee_edit', {'id': employee.id}) }}">edit</a>
|
||||
|
||||
{{ include('employee/_delete_form.html.twig') }}
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user