<?php
namespace App\Controller\Api;
use App\Entity\Customer;
use App\Entity\PlayerSportsInjuries;
use App\Services\Api\PlayerSportsInjuriesManager;
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 PlayerSportsInjuriesController extends ApiController
{
/** @var PlayerSportsInjuriesManager $playerSportsInjuriesManager */
protected $playerSportsInjuriesManager;
/**
* @param PlayerSportsInjuriesManager $playerSportsInjuriesManager
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, PlayerSportsInjuriesManager $playerSportsInjuriesManager)
{
parent::__construct($em, $parameterBag);
$this->playerSportsInjuriesManager = $playerSportsInjuriesManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/player_sports_injuries/add", name="api_player_sports_injuries", methods={"POST"})
*/
public function add(Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->playerSportsInjuriesManager->add($data, $request);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/player_sports_injuries/{id}/update", name="api_player_sports_injuries_update", methods={"POST"})
*/
public function update(PlayerSportsInjuries $playerInjury, Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->playerSportsInjuriesManager->update($playerInjury, $data, $request);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/player_sports_injuries/{date}", name="api_player_sports_injuries_me", methods={"POST"})
*/
public function getSharePlayerMe($date, Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$season = $customer->getSeasonActive();
if (!empty($season)) {
$filter = [
[
"prop" => "finish",
"comp" => "=",
"val" => 0
],
[
"prop" => "season",
"comp" => "=",
"val" => $season
],
[
"prop" => "dateStart",
"comp" => ">=",
"val" => \DateTime::createFromFormat("d-m-Y", $date)->format('d-m-Y')
],
];
/** @var PlayerSportsInjuries $playersSportInjuries */
$playersSportInjuries = [];
foreach ($this->em->getRepository(PlayerSportsInjuries::class)->findByOwnCriteria($filter) as $playerSport) {
$playersSportInjuries[] = $playerSport->__toArray();
}
return $this->response($playersSportInjuries);
}
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/player_sports/delete/{id}", name="api_share_player_delete", methods={"POST"})
*/
public function delete(Int $id)
{
$response = $this->playerSportsInjuriesManager->delete($id);
return $this->response($response);
}
protected function getDataFromRequest(Request $request)
{
$data = [
"playerId" => $request->get('playerId'),
];
if ($request->get('note')) {
$data['note'] = $request->get('note');
}
if ($request->get('dateStart')) {
$data["dateStart"] = \DateTime::createFromFormat("d-m-Y", $request->get('dateStart'));
}
if ($request->get('dateEnd')) {
$data['dateEnd'] = \DateTime::createFromFormat("d-m-Y", $request->get('dateEnd'));
}
return $data;
}
}