src\Mobile\NewPatientController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Mobile;
  3. use App\Entity\Branch;
  4. use App\Entity\Patient;
  5. use App\Entity\User;
  6. use Doctrine\ORM\NonUniqueResultException;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Doctrine\Persistence\ObjectManager;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Serializer\Encoder\JsonDecode;
  14. use Symfony\Contracts\HttpClient\HttpClientInterface;
  15. class NewPatientController extends AbstractController {
  16.     private ObjectManager $em;
  17.     public function __construct(ManagerRegistry $managerRegistry)
  18.     {
  19.         $this->em $managerRegistry->getManager();
  20.     }
  21.     /**
  22.      * @Route("/patients", methods={"POST"}, name="newPatient")
  23.      * @param Request $request
  24.      * @return Response
  25.      */
  26.     public function newPatient(Request $request): Response {
  27.         $em $this->em;
  28.         $phone $request->query->get('phone');
  29.         $user $em->getRepository(User::class)->findOneBy([
  30.             'phone' => $phone
  31.         ]);
  32.         $patientJSON $request->getContent();
  33.         $patientArray json_decode($patientJSON);
  34. //        $date = date($patientArray->appointment_date);
  35. //        $date = new \DateTime($patientArray->appointment_date);
  36.         $date = new \DateTime();
  37.         $date->modify('+2 day');
  38. //        $checkPatient = $em->getRepository('App:Patient')->findOneBy([
  39. //            'uniqueId' =>
  40. //        ])
  41. //        $date = new \DateTime();
  42. //       try {
  43. //            $checkPatient = $em->getRepository("App:Patient")->findByDateAndPhoneNumber($patientArray->phone);
  44. //
  45. //
  46. //            if($checkPatient){
  47. //                return new Response('ALREADY AVAILABLE', Response::HTTP_OK, [
  48. //                    'Content-Type' => 'application/json'
  49. //                ]);
  50. //            }
  51. //
  52. //            $checkPatient = null;
  53. //
  54. //            $checkPatient = $em->getRepository("App:Patient")->findByDate3MonthsAndPhoneNumber($patientArray->phone);
  55. //
  56. //            if($checkPatient){
  57. //                return new Response('ALREADY AVAILABLE', Response::HTTP_OK, [
  58. //                    'Content-Type' => 'application/json'
  59. //                ]);
  60. //            }
  61. //
  62. //        } catch (NonUniqueResultException $e) {
  63. //
  64. //        }
  65.         $patient = new Patient();
  66.        // $patient->setPatientId(rand(10,100));
  67.         $patient->setPatientName($patientArray->patient_name);
  68.         $patient->setPhone($patientArray->phone);
  69.         $patient->setLocation($patientArray->location);
  70.         $patient->setReason($patientArray->reason);
  71. //        $patient->setAppointmentDate(new \DateTime());
  72.         $patient->setAppointmentDate($date);
  73.        // $patient->setCreatedAt(new \DateTime());
  74.         $patient->setUser($user);
  75.         $patient->setIsSeen(false);
  76. //        $user = $em->getRepository("App:User")->findOneBy([
  77. //            'id' => 1
  78. //        ]);
  79.         $branch $em->getRepository(Branch::class)->findOneBy([
  80.             'id' => $patientArray->branch
  81.         ]);
  82. //        $branch = $user->getBranch();
  83.         $patient->setBranch($branch);
  84.         $patient->setUser($user);
  85.         // dump($patient);
  86.         $em->persist($patient);
  87.         $em->flush();
  88. //        dump($patient);
  89.         return new Response($patientJSONResponse::HTTP_OK, [
  90.             'Content-Type' => 'application/json'
  91.         ]);
  92.     }
  93. }