src/Controller/RoomController.php line 23

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Room;
  4. use App\Form\ActiveTenantType;
  5. use App\Form\RoomType;
  6. use App\Repository\RoomRepository;
  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 RoomController extends AbstractController
  13. {
  14.     public function __construct(
  15.         private ManagerRegistry $managerRegistry,
  16.     ) {
  17.     }
  18.     #[Route('/room'name'room'methods: ['GET'])]
  19.     public function index(): Response
  20.     {
  21.         /** @var RoomRepository $roomRepository */
  22.         $roomRepository $this->managerRegistry->getRepository(Room::class);
  23.         $rooms $roomRepository->findAll();
  24.         $room = new Room();
  25.         $form $this->createForm(RoomType::class, $room);
  26.         return $this->render('room/index.html.twig', [
  27.             'rooms' => $rooms,
  28.             'room_form' => $form->createView(),
  29.         ]);
  30.     }
  31.     #[Route('/room'name'room_add'methods: ['POST'])]
  32.     public function add(Request $request): Response
  33.     {
  34.         $form $this->createForm(RoomType::class);
  35.         $form->handleRequest($request);
  36.         if ($form->isSubmitted() && $form->isValid()) {
  37.             $data $form->getData();
  38.             $em $this->managerRegistry->getManager();
  39.             $em->persist($data);
  40.             $em->flush();
  41.             $this->addFlash(
  42.                 'success',
  43.                 'Zimmer erstellt!'
  44.             );
  45.             return $this->redirectToRoute('room');
  46.         }
  47.         $this->addFlash(
  48.             'warning',
  49.             'Etwas ist schief gelaufen!'
  50.         );
  51.         return $this->redirectToRoute('room');
  52.     }
  53.     #[Route('/room/{room}'name'room_view'methods: ['GET'])]
  54.     public function view(Room $room): Response
  55.     {
  56.         $form $this->createForm(RoomType::class, $room);
  57.         /** @var RoomRepository $roomRepository */
  58.         $roomRepository $this->managerRegistry->getRepository(Room::class);
  59.         $room $roomRepository->findAll();
  60.         return $this->render('room/view.html.twig', [
  61.             'room_form' => $form->createView(),
  62.         ]);
  63.     }
  64.     #[Route('/room/{room}'name'room_patch'methods: ['POST'])]
  65.     public function patch(Room $roomRequest $request): Response
  66.     {
  67.         $form $this->createForm(RoomType::class, $room);
  68.         $form->handleRequest($request);
  69.         if ($form->isSubmitted() && $form->isValid()) {
  70.             $em $this->managerRegistry->getManager();
  71.             $data $form->getData();
  72.             $em->persist($data);
  73.             $em->flush();
  74.             $this->addFlash(
  75.                 'success',
  76.                 'Änderungen gespeichert!'
  77.             );
  78.             return $this->redirectToRoute('room');
  79.         }
  80.         $this->addFlash(
  81.             'warning',
  82.             'Etwas ist schief gelaufen!'
  83.         );
  84.         return $this->redirectToRoute('room');
  85.     }
  86.     #[Route('/room/{room}/add-tenant'name'room_add_tenant'methods: ['POST'])]
  87.     public function addActiveTenant(Request $request): Response
  88.     {
  89.         $form $this->createForm(ActiveTenantType::class);
  90.         $form->handleRequest($request);
  91.         if ($form->isSubmitted() && $form->isValid()) {
  92.             $data $form->getData();
  93.             $em $this->managerRegistry->getManager();
  94.             $em->persist($data);
  95.             $em->flush();
  96.             $this->addFlash(
  97.                 'success',
  98.                 'Mieter zu Zimmer hinzugefügt!'
  99.             );
  100.             return $this->redirectToRoute('room');
  101.         }
  102.         $this->addFlash(
  103.             'warning',
  104.             'Etwas ist schief gelaufen!'
  105.         );
  106.         return $this->redirectToRoute('room');
  107.     }
  108.     #[Route('/room/{room}/delete'name'room_delete'methods: ['GET'])]
  109.     public function delete(Room $room): Response
  110.     {
  111.         $em $this->managerRegistry->getManager();
  112.         $em->remove($room);
  113.         $em->flush();
  114.         $this->addFlash(
  115.             'success',
  116.             'Zimmer gelöscht!'
  117.         );
  118.         return $this->redirectToRoute('room');
  119.     }
  120. }