<?php
namespace App\Controller\Api;
use App\Entity\Strategy;
use App\Services\Api\StrategyManager;
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 StrategyController extends ApiController
{
/** @var StrategyManager $strategyManager */
protected $strategyManager;
/**
* @param StrategyManager $strategyManager
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, StrategyManager $strategyManager)
{
parent::__construct($em, $parameterBag);
$this->strategyManager = $strategyManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/strategy/add", name="api_strategy_add", methods={"POST"})
*/
public function add(Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$prefixFile = $this->getParameter("app.path.strategies");
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$urlImage = $baseurl . $prefixFile . "/";
$response = $this->strategyManager->add($data, $urlImage);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/strategy/update/{strategy}", name="api_strategy_update", methods={"POST"})
*/
public function update(Request $request, Strategy $strategy)
{
$request = $this->transformJsonBody($request);
$prefixFile = $this->getParameter("app.path.strategies");
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$urlImage = $baseurl . $prefixFile . "/";
$data = $this->getDataUpdateFromRequest($request);
$response = $this->strategyManager->update($data, $strategy, $urlImage);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/strategy/delete/{strategy}", name="api_strategy_delete", methods={"POST"})
*/
public function delete(Strategy $strategy)
{
$response = $this->strategyManager->delete($strategy);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/strategy/list", name="api_strategy_list", methods={"POST"})
*/
public function getList(Request $request)
{
$prefixFile = $this->getParameter("app.path.strategies");
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$urlImage = $baseurl . $prefixFile . "/";
$response = $this->strategyManager->list($urlImage);
return $this->response($response);
}
protected function getDataFromRequest(Request $request)
{
$data = [
"title" => $request->get('title'),
"description" => $request->get('description'),
"image" => $request->files->get('image'),
];
return $data;
}
protected function getDataUpdateFromRequest(Request $request)
{
$data = [
"title" => $request->get('title'),
"description" => $request->get('description'),
"image" => $request->files->get('image'),
];
return $data;
}
}