<?php
namespace App\Controller\Api;
use App\Entity\Convocatoria;
use App\Entity\Customer;
use App\Entity\Player;
use App\Entity\Document;
use App\Entity\SeasonPlayer;
use App\Entity\TrainingAsist;
use App\Helper\UploadedBase64File;
use App\Repository\ConvocatoriaRepository;
use App\Repository\TrainingAsistRepository;
use App\Repository\PlayerRepository;
use App\Services\Api\DocumentManager;
use App\Services\Api\CustomerManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Psr\Log\LoggerInterface;
class DocumentController extends ApiController
{
/** @var CustomerManager $customerManager */
protected $customerManager;
/**
* @param CustomerManager $customerManager
*/
public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, CustomerManager $customerManager)
{
parent::__construct($em, $parameterBag);
$this->customerManager = $customerManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/document/add", name="api_document_add", methods={"POST"})
*/
public function addDocument(Request $request)
{
$request = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($request);
$response = $this->customerManager->addDocument($data, $request);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/document/delete/{id}", name="api_document_delete", methods={"POST"})
*/
public function deleteDocument($id)
{
$response = $this->customerManager->deleteDocument($id);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/document/list", name="api_document_list", methods={"POST"})
*/
public function listDocument(Request $request, LoggerInterface $logger)
{
$prefixFile = $this->getParameter("app.path.document_images");
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$documents = [];
/** @var Customer $customer */
/** @var Document $document */
$customer = $this->getUser();
$logger->info('CUS ' . $customer . " " . $customer->getId());
$filter = [
"customer" => $customer
];
foreach ($this->em->getRepository(Document::class)->findBy($filter, ["id" => "DESC"]) as $document) {
$documents[] = [
"id" => $document->getId(),
"image" => !empty($document->getImage()) ? $baseurl . $prefixFile . "/" . $document->getImage() : "",
];
}
return $this->response($documents);
}
protected function getDataFromRequest(Request $request)
{
$data = [];
$image = $request->get('image');
if (!empty($image))
$data["image"] = new UploadedBase64File($image, $request->get('imageFilename'));
return $data;
}
}