src/Controller/LangchainExerciseController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ExerciseSimpleAnswer;
  4. use App\Repository\ExerciseSimpleAnswerRepository;
  5. use App\Repository\PatientRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. /**
  11.  * @Route("/langchain-exercise")
  12.  */
  13. class LangchainExerciseController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/get-random-exercise-langchain", name="get_random_exercise_langchain", methods={"GET"})
  17.      */
  18.     public function randomExercise(ExerciseSimpleAnswerRepository $exerciseSimpleAnswerRepository): JsonResponse
  19.     {
  20.         $ids $exerciseSimpleAnswerRepository->getAllIds();
  21.     
  22.         if (empty($ids)) {
  23.             return new JsonResponse(['error' => 'No exercises available'], 404);
  24.         }
  25.     
  26.         $randomId $ids[array_rand($ids)];
  27.     
  28.         $exercise $exerciseSimpleAnswerRepository->find($randomId);
  29.     
  30.         if (!$exercise) {
  31.             return new JsonResponse(['error' => 'Exercise not found'], 404);
  32.         }
  33.     
  34.         return new JsonResponse([
  35.             'name' => $exercise->getName(),
  36.             'kpi' => $exercise->getKpi()->getName(),
  37.             'description' => $exercise->getDescription(),
  38.             'question' => $exercise->getQuestion(),
  39.             'answer' => $exercise->getAnswer(),
  40.         ]);
  41.     }
  42.     /**
  43.      * @Route("/exercise-by-kpi", name="exercise_by_kpi", methods={"POST"})
  44.      */
  45.     public function exerciseByKpi(Request $requestExerciseSimpleAnswerRepository $exerciseSimpleAnswerRepository): JsonResponse
  46.     {
  47.         $data json_decode($request->getContent(), true);
  48.         if (!isset($data['kpi']) || empty($data['kpi'])) {
  49.             return new JsonResponse(['error' => 'KPI is required'], 400);
  50.         }
  51.         $kpi $data['kpi'];
  52.         $exercises $exerciseSimpleAnswerRepository->findByKpiName($kpi);
  53.         if (empty($exercises)) {
  54.             return new JsonResponse(['error' => 'No exercises found for the specified KPI'], 404);
  55.         }
  56.         $randomExercise $exercises[array_rand($exercises)];
  57.         return new JsonResponse([
  58.             'name' => $randomExercise->getName(),
  59.             'kpi' => $randomExercise->getKpi()->getName(),
  60.             'description' => $randomExercise->getDescription(),
  61.             'question' => $randomExercise->getQuestion(),
  62.             'answer' => $randomExercise->getAnswer(),
  63.         ]);
  64.     }
  65.     /**
  66.      * @Route("/increase-solved-exercises", name="increase_solved_exercises", methods={"POST"})
  67.      */
  68.     public function increaseSolvedExercises(Request $requestPatientRepository $patientRepository): JsonResponse
  69.     {
  70.         $data json_decode($request->getContent(), true);
  71.         if (!isset($data['patient_id']) || empty($data['patient_id'])) {
  72.             return new JsonResponse(['error' => 'Patient ID is required'], 400);
  73.         }
  74.         if (!isset($data['kpi']) || empty($data['kpi'])) {
  75.             return new JsonResponse(['error' => 'KPI is required'], 400);
  76.         }
  77.         $patientId $data['patient_id'];
  78.         $kpi $data['kpi'];
  79.         $patient $patientRepository->find($patientId);
  80.         if (!$patient) {
  81.             return new JsonResponse(['error' => 'Patient not found'], 404);
  82.         }
  83.         $solvedExercises $patient->getSolvedExercises();
  84.         
  85.         if (!$solvedExercises) {
  86.             $solvedExercises = [
  87.                 'Stimolazione del linguaggio' => 0,
  88.                 'Stimolazione del ragionamento' => 0,
  89.                 'Stimolazione dell\'attenzione' => 0,
  90.                 'Stimolazione dell\'orientamento' => 0
  91.             ];
  92.         } else {
  93.             $solvedExercises json_decode($solvedExercisestrue);
  94.         }
  95.         
  96.         if (isset($solvedExercises[$kpi])) {
  97.             $solvedExercises[$kpi]++;
  98.         } else {
  99.             return new JsonResponse(['error' => 'Invalid KPI'], 400);
  100.         }
  101.         $patient->setSolvedExercises(json_encode($solvedExercises));
  102.         $entityManager $this->getDoctrine()->getManager();
  103.         $entityManager->flush();
  104.         return new JsonResponse(['message' => 'Solved exercises updated successfully']);
  105.     }
  106.     /**
  107.      * @Route("/{id}", name="get_simple_answer_exercise_langchain", methods={"GET"})
  108.      */
  109.     public function exerciseById(ExerciseSimpleAnswer $exerciseSimpleAnswer): JsonResponse
  110.     {
  111.         return new JsonResponse([
  112.             'name' => $exerciseSimpleAnswer->getName(),
  113.             'kpi' => $exerciseSimpleAnswer->getKpi()->getName(),
  114.             'description' => $exerciseSimpleAnswer->getDescription(),
  115.             'question' => $exerciseSimpleAnswer->getQuestion(),
  116.             'answer' => $exerciseSimpleAnswer->getAnswer(),
  117.         ]);
  118.     }
  119. }