<?php
namespace App\Controller\Api;
use App\Entity\CoachingStaff;
use App\Entity\Customer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class CoachingStaffController extends ApiController
{
/**
* @var EntityManagerInterface
*/
protected $em;
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag)
{
parent::__construct($em, $parameterBag);
$this->em = $em;
}
/**
* @return JsonResponse
*
* @Route("/coaching-staff", name="api_coaching_staff_get", methods={"GET"})
*/
public function getCoachingStaff()
{
/** @var Customer $customer */
$customer = $this->getUser();
$season = $customer->getSeasonActive();
if (!$season) {
$response = ['error' => 'No active season found', 'code' => 404];
$this->setStatusCode($response["code"]);
return $this->response($response);
}
$coachingStaff = $this->em->getRepository(CoachingStaff::class)->findOneBy(['season' => $season]);
if (!$coachingStaff) {
$response = ['error' => 'No coaching staff found for the active season', 'code' => 404];
$this->setStatusCode($response["code"]);
return $this->response($response);
}
return $this->response([
'id' => $coachingStaff->getId(),
'secondCoach' => $coachingStaff->getSecondCoach(),
'teamDelegate' => $coachingStaff->getTeamDelegate(),
'equipmentManager' => $coachingStaff->getEquipmentManager(),
'masseur' => $coachingStaff->getMasseur(),
'physicalTherapist' => $coachingStaff->getPhysicalTherapist(),
'teamDoctor' => $coachingStaff->getTeamDoctor(),
]);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/coaching-staff", name="api_coaching_staff_create", methods={"POST"})
*/
public function postCoachingStaff(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$season = $customer->getSeasonActive();
if (!$season) {
return $this->response(['error' => 'No active season found']);
}
$data = json_decode($request->getContent(), true);
$coachingStaff = $this->em->getRepository(CoachingStaff::class)->findOneBy(['season' => $season]);
if (!$coachingStaff) {
$coachingStaff = new CoachingStaff();
$coachingStaff->setSeason($season);
}
$coachingStaff->setSecondCoach($data['secondCoach'] ?? null);
$coachingStaff->setTeamDelegate($data['teamDelegate'] ?? null);
$coachingStaff->setEquipmentManager($data['equipmentManager'] ?? null);
$coachingStaff->setMasseur($data['masseur'] ?? null);
$coachingStaff->setPhysicalTherapist($data['physicalTherapist'] ?? null);
$coachingStaff->setTeamDoctor($data['teamDoctor'] ?? null);
$this->em->persist($coachingStaff);
$this->em->flush();
return $this->response(['message' => 'Coaching staff saved successfully']);
}
}