src/Controller/BookingController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use App\Repository\BookingRepository;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Mime\Email;
  9. use Symfony\Component\Mailer\Mailer;
  10. use Symfony\Component\Mailer\Transport;
  11. use Symfony\Component\Mailer\MailerInterface;
  12. class BookingController extends AbstractController
  13. {
  14.     private $bookingRepository;
  15.     public  function __construct(BookingRepository $bookingRepository)
  16.     {
  17.         $this->bookingRepository $bookingRepository;
  18.     }
  19.     /**
  20.      * @Route("/services", name="app_services", methods={"GET"})
  21.      */
  22.     public function services(): Response
  23.     {
  24.         $services $this->bookingRepository->fetchServices();
  25.         //dd($bookings);
  26.         return $this->json($services);
  27.     }
  28.     /**
  29.      * @Route("/appointments/{id}", name="app_appointments", methods={"GET"})
  30.      * the id is for customer id and it checks the active future appointments and return null if there is no future appointments
  31.      */
  32.     public function appointments(int $id): Response
  33.     {
  34.         $appointments $this->bookingRepository->activeAppointments($id);
  35.         //dd($bookings);
  36.         return $this->json($appointments);
  37.     }
  38.     /**
  39.      * @Route("/allappointments/", name="all_appointments", methods={"GET"})
  40.      * lists all the appointments
  41.      */
  42.     public function allAppointments() : Response 
  43.         $appointments $this->bookingRepository->allActiveAppointments();
  44.         //dd($bookings);
  45.         return $this->json($appointments);
  46.     }
  47.     /**
  48.      * @Route("/freeweekdays/", name="freeweekdays", methods={"GET"})
  49.      * returns weekday_off 3 and 7 for Wednesday and Sunday day offs
  50.      */
  51.     public function freeWeekdays() : Response 
  52.         $appointments $this->bookingRepository->freeWeekdays();
  53.         //dd($bookings);
  54.         return $this->json($appointments);
  55.     }
  56.     /**
  57.      * @Route("/workingtime/", name="workingtime", methods={"GET"})
  58.      * lists all the user id, weekday, startime, endtime, breakstart and breakend
  59.      */
  60.     public function workingTime() : Response 
  61.         $appointments $this->bookingRepository->fetchWorkingTime();
  62.         //dd($bookings);
  63.         return $this->json($appointments);
  64.     }
  65.     /**
  66.      * @Route("/workinghours/", name="workinghours", methods={"GET"})
  67.      * lists only one starttime and endtime
  68.      */
  69.     public function workingHours() : Response 
  70.         $appointments $this->bookingRepository->fetchWorkingHours();
  71.         //dd($bookings);
  72.         return $this->json($appointments);
  73.     }
  74.     /**
  75.      * @Route("/daysoff/", name="daysoff", methods={"GET"})
  76.      * it returns null since there is no day off mentioned
  77.      */
  78.     public function barbersDaysoff() : Response 
  79.         $appointments $this->bookingRepository->barbersDaysoff();
  80.         //dd($bookings);
  81.         return $this->json($appointments);
  82.     }
  83.     /**
  84.      * @Route("/historyappointments/{id}", name="app_historyappointments", methods={"GET"})
  85.      * lists all the details for the history of appointements for the specified customer
  86.      */
  87.     public function historyAppointements(int $id): Response
  88.     {
  89.         $historyappointments $this->bookingRepository->historyAppointments($id);
  90.         //dd($bookings);
  91.         return $this->json($historyappointments);
  92.     }
  93.     /**
  94.      * @Route("/bookappointments", name="app_bookappointments", methods={"POST"})
  95.      * Does the booking
  96.      */
  97.     public function bookAppointments(Request $request ): Response{
  98.         $request_data json_decode($request->getContent(), true);
  99.         $check $this->bookingRepository->checkfree($request_data["bookingStart"], $request_data["bookingEnd"], $request_data["providerId"]);
  100.         if(empty($check)){
  101.             $result $this->bookingRepository->bookAppointments($request_data["bookingStart"], $request_data["bookingEnd"], $request_data["serviceId"], $request_data["providerId"], $request_data["servicePrice"], $request_data["customerId"]);
  102.             $customerId $request_data["customerId"];
  103.             $email $this->bookingRepository->fetchEmail($customerId);
  104.             $transport Transport::fromDsn('smtp://localhost');
  105.             $mailer = new Mailer($transport);
  106.             $email = (new Email())
  107.                 ->from('info@geneva-barbers.ch')
  108.                 ->to($email[0]["email"])
  109.                 //->cc('cc@example.com')
  110.                 //->bcc('bcc@example.com')
  111.                 //->replyTo('fabien@example.com')
  112.                 //->priority(Email::PRIORITY_HIGH)
  113.                 ->subject('Geneva Barbers - Rendez-Vous Confirmation')
  114.                 ->text('Geneva Barbers - Rendez-Vous Confirmation')
  115.                 ->html("<h1>Votre Rendez-Vous chez Geneva Barbers</h1><p>Début de rendez-vous:".$request_data["bookingStart"]."</p><p>Fin de rendez-vous:".$request_data["bookingEnd"]."</p>");
  116.             $mailer->send($email);
  117.             
  118.             return $this->json(['Success' => 'Appointment Successfully Booked!']);
  119.         } else {
  120.             return $this->json(['Success' => 'APPOINTEMENT NOT BOOKED, PLEASE CHOOSE ANOTHER TIME!']);
  121.         }
  122.         
  123.     }
  124. }