src/Controller/TenantController.php line 24

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Tenant;
  4. use App\Form\TenantType;
  5. use App\Repository\TenantRepository;
  6. use App\Service\TenantService;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class TenantController extends AbstractController
  13. {
  14.     public function __construct(
  15.         private ManagerRegistry $managerRegistry,
  16.         private TenantService $tenantService,
  17.     ) {
  18.     }
  19.     #[Route('/tenant'name'tenant'methods: ['GET'])]
  20.     public function index(): Response
  21.     {
  22.         /** @var TenantRepository $tenantRepository */
  23.         $tenantRepository $this->managerRegistry->getRepository(Tenant::class);
  24.         $tenants $tenantRepository->findAll();
  25.         $tenant = new Tenant();
  26.         $form $this->createForm(TenantType::class, $tenant);
  27.         return $this->render('tenant/index.html.twig', [
  28.             'tenants' => $tenants,
  29.             'tenant_form' => $form->createView(),
  30.         ]);
  31.     }
  32.     #[Route('/tenant'name'tenant_add'methods: ['POST'])]
  33.     public function add(Request $request): Response
  34.     {
  35.         $form $this->createForm(tenantType::class);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             $data $form->getData();
  39.             $em $this->managerRegistry->getManager();
  40.             $em->persist($data);
  41.             $em->flush();
  42.             $this->addFlash(
  43.                 'success',
  44.                 'Mieter erstellt!'
  45.             );
  46.             return $this->redirectToRoute('tenant');
  47.         }
  48.         $this->addFlash(
  49.             'warning',
  50.             'Etwas ist schief gelaufen!'
  51.         );
  52.         return $this->redirectToRoute('tenant');
  53.     }
  54.     #[Route('/tenant/{tenant}'name'tenant_view'methods: ['GET'])]
  55.     public function view(Tenant $tenant): Response
  56.     {
  57.         $form $this->createForm(TenantType::class, $tenant);
  58.         /** @var TenantRepository $tenantRepository */
  59.         $tenantRepository $this->managerRegistry->getRepository(Tenant::class);
  60.         $tenant $tenantRepository->findAll();
  61.         return $this->render('tenant/view.html.twig', [
  62.             'tenant_form' => $form->createView(),
  63.         ]);
  64.     }
  65.     #[Route('/tenant/{tenant}'name'tenant_patch'methods: ['POST'])]
  66.     public function patch(Tenant $tenantRequest $request): Response
  67.     {
  68.         $form $this->createForm(TenantType::class, $tenant);
  69.         $form->handleRequest($request);
  70.         if ($form->isSubmitted() && $form->isValid()) {
  71.             $em $this->managerRegistry->getManager();
  72.             $data $form->getData();
  73.             $data $this->tenantService->setStateTenantForRoom($tenant);
  74.             $em->persist($data);
  75.             $em->flush();
  76.             $this->addFlash(
  77.                 'success',
  78.                 'Ă„nderungen gespeichert!'
  79.             );
  80.             return $this->redirectToRoute('tenant');
  81.         }
  82.         $this->addFlash(
  83.             'warning',
  84.             'Etwas ist schief gelaufen!'
  85.         );
  86.         return $this->redirectToRoute('tenant');
  87.     }
  88.     #[Route('/tenant/{tenant}/delete'name'tenant_delete'methods: ['GET'])]
  89.     public function delete(Tenant $tenant): Response
  90.     {
  91.         $em $this->managerRegistry->getManager();
  92.         $em->remove($tenant);
  93.         $em->flush();
  94.         $this->addFlash(
  95.             'success',
  96.             'Mieter gelöscht!'
  97.         );
  98.         return $this->redirectToRoute('tenant');
  99.     }
  100. }