src/Controller/Api/CoachingStaffController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\CoachingStaff;
  4. use App\Entity\Customer;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class CoachingStaffController extends ApiController
  11. {
  12.     /**
  13.      * @var EntityManagerInterface
  14.      */
  15.     protected $em;
  16.     public function __construct(EntityManagerInterface $emParameterBagInterface $parameterBag)
  17.     {
  18.         parent::__construct($em$parameterBag);
  19.         $this->em $em;
  20.     }
  21.     /**
  22.      * @return JsonResponse
  23.      * 
  24.      * @Route("/coaching-staff", name="api_coaching_staff_get", methods={"GET"})
  25.      */
  26.     public function getCoachingStaff()
  27.     {
  28.         
  29.         /** @var Customer $customer */
  30.         $customer $this->getUser();
  31.         $season $customer->getSeasonActive();
  32.         if (!$season) {
  33.             $response = ['error' => 'No active season found''code' => 404];
  34.             $this->setStatusCode($response["code"]);
  35.             return $this->response($response);
  36.         }
  37.         $coachingStaff $this->em->getRepository(CoachingStaff::class)->findOneBy(['season' => $season]);
  38.         if (!$coachingStaff) {
  39.             $response = ['error' => 'No coaching staff found for the active season''code' => 404];
  40.             $this->setStatusCode($response["code"]);
  41.             return $this->response($response);
  42.         }
  43.         return $this->response([
  44.             'id' => $coachingStaff->getId(),
  45.             'secondCoach' => $coachingStaff->getSecondCoach(),
  46.             'teamDelegate' => $coachingStaff->getTeamDelegate(),
  47.             'equipmentManager' => $coachingStaff->getEquipmentManager(),
  48.             'masseur' => $coachingStaff->getMasseur(),
  49.             'physicalTherapist' => $coachingStaff->getPhysicalTherapist(),
  50.             'teamDoctor' => $coachingStaff->getTeamDoctor(),
  51.         ]);
  52.     }
  53.     /**
  54.      * @param Request $request
  55.      * @return JsonResponse
  56.      * 
  57.      * @Route("/coaching-staff", name="api_coaching_staff_create", methods={"POST"})
  58.      */
  59.     public function postCoachingStaff(Request $request)
  60.     {
  61.         /** @var Customer $customer */
  62.         $customer $this->getUser();
  63.         $season $customer->getSeasonActive();
  64.         if (!$season) {
  65.             return $this->response(['error' => 'No active season found']);
  66.         }
  67.         $data json_decode($request->getContent(), true);
  68.         $coachingStaff $this->em->getRepository(CoachingStaff::class)->findOneBy(['season' => $season]);
  69.         if (!$coachingStaff) {
  70.             $coachingStaff = new CoachingStaff();
  71.             $coachingStaff->setSeason($season);
  72.         }
  73.         $coachingStaff->setSecondCoach($data['secondCoach'] ?? null);
  74.         $coachingStaff->setTeamDelegate($data['teamDelegate'] ?? null);
  75.         $coachingStaff->setEquipmentManager($data['equipmentManager'] ?? null);
  76.         $coachingStaff->setMasseur($data['masseur'] ?? null);
  77.         $coachingStaff->setPhysicalTherapist($data['physicalTherapist'] ?? null);
  78.         $coachingStaff->setTeamDoctor($data['teamDoctor'] ?? null);
  79.         $this->em->persist($coachingStaff);
  80.         $this->em->flush();
  81.         return $this->response(['message' => 'Coaching staff saved successfully']);
  82.     }
  83. }