<?php
namespace App\Controller\Api;
use App\Entity\Player;
use App\Entity\PlayerWeight;
use App\Services\Api\PlayerWeightManager;
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 PlayerWeightController extends ApiController
{
/** @var PlayerWeightManager $playerWeightManager */
protected $playerWeightManager;
/**
* @param PlayerWeightManager $playerWeightManager
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, PlayerWeightManager $playerWeightManager)
{
parent::__construct($em, $parameterBag);
$this->playerWeightManager = $playerWeightManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/player_weight/{player}/add", name="api_player_weight_add", methods={"POST"})
*/
public function add(Request $request, Player $player)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->playerWeightManager->add($data, $player);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/player_weight/{playerWeight}/update", name="api_player_weight_update", methods={"POST"})
*/
public function update(Request $request, PlayerWeight $playerWeight)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->playerWeightManager->update($data, $playerWeight);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/player_weight/{weight}/delete", name="api_player_weight_delete", methods={"POST"})
*/
public function delete(PlayerWeight $weight)
{
$response = $this->playerWeightManager->delete($weight);
return $this->response($response);
}
protected function getDataFromRequest(Request $request)
{
$data = [
"weight" => $request->get('weight'),
"date" => $request->get('date'),
"time" => $request->get('time'),
];
return $data;
}
}