src/Controller/WeatherController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Weather;
  4. use App\Form\WeatherType;
  5. use App\Repository\WeatherRepository;
  6. use App\Services\WeatherService;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. /**
  12.  * @Route("/weather")
  13.  */
  14. class WeatherController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/index", name="weather_index", methods={"GET"})
  18.      */
  19.     public function index(WeatherRepository $weatherRepository): Response
  20.     {
  21.         $today = new \DateTime('now');
  22.         return $this->render('weather/index.html.twig', [
  23.             'weather' => $weatherRepository->findAll(),
  24.             'today' => $today
  25.         ]);
  26.     }
  27.     /**
  28.      * @Route("/new", name="weather_new", methods={"GET","POST"})
  29.      */
  30.     public function new(Request $request): Response
  31.     {
  32.         $weather = new Weather();
  33.         $form $this->createForm(WeatherType::class, $weather);
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()) {
  36.             $entityManager $this->getDoctrine()->getManager();
  37.             $entityManager->persist($weather);
  38.             $entityManager->flush();
  39.             return $this->redirectToRoute('weather_index');
  40.         }
  41.         return $this->render('weather/new.html.twig', [
  42.             'weather' => $weather,
  43.             'form' => $form->createView(),
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/show/{id}", name="weather_show", methods={"GET"})
  48.      */
  49.     public function show(Weather $weather): Response
  50.     {
  51.         return $this->render('weather/show.html.twig', [
  52.             'weather' => $weather,
  53.         ]);
  54.     }
  55.     /**
  56.      * @Route("/edit/{id}", name="weather_edit", methods={"GET","POST"})
  57.      */
  58.     public function edit(Request $requestWeather $weather): Response
  59.     {
  60.         $form $this->createForm(WeatherType::class, $weather);
  61.         $form->handleRequest($request);
  62.         if ($form->isSubmitted() && $form->isValid()) {
  63.             $this->getDoctrine()->getManager()->flush();
  64.             return $this->redirectToRoute('weather_index');
  65.         }
  66.         return $this->render('weather/edit.html.twig', [
  67.             'weather' => $weather,
  68.             'form' => $form->createView(),
  69.         ]);
  70.     }
  71.     /**
  72.      * @Route("/delete/{id}", name="weather_delete", methods={"POST"})
  73.      */
  74.     public function delete(Request $requestWeather $weather): Response
  75.     {
  76.         if ($this->isCsrfTokenValid('delete' $weather->getId(), $request->request->get('_token'))) {
  77.             $entityManager $this->getDoctrine()->getManager();
  78.             $entityManager->remove($weather);
  79.             $entityManager->flush();
  80.         }
  81.         return $this->redirectToRoute('weather_index');
  82.     }
  83.     /**
  84.      * @Route("/weather/delete_all", name="weather_delete_all")
  85.      */
  86.     public function deleteAllWeatherData(WeatherRepository $weatherRepository)
  87.     {
  88.         $weathers $weatherRepository->findAll();
  89.         foreach ($weathers as $weather) {
  90.             $entityManager $this->getDoctrine()->getManager();
  91.             $entityManager->remove($weather);
  92.             $entityManager->flush();
  93.         }
  94.         return $this->redirectToRoute('weather_index');
  95.     }
  96.     /**
  97.      * @Route("/weather/fetch_weather_data", name="fetch_weather_data", methods={"GET"})
  98.      */
  99.     public function weather(WeatherService $weatherServiceRequest $request)
  100.     {
  101.         $referer $request->headers->get('Referer');
  102.         $weatherService->update();
  103.         $weatherService->hourlyUpdate();
  104.         return $this->redirect($referer);
  105.     }
  106.     /**
  107.      * @Route("/weather/fetch_weather_data_hourly", name="fetch_weather_data_hourly", methods={"GET"})
  108.      */
  109.     public function weatherHourly(WeatherService $weatherServiceRequest $request)
  110.     {
  111.         $referer $request->headers->get('Referer');
  112.         $weatherService->hourlyUpdate();
  113.         return $this->redirect($referer);
  114.     }
  115. }