<?php
namespace App\Controller;
use App\Entity\Club;
use App\Services\ClubService;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webmozart\Assert\Assert;
/**
* Class ClubApiController
* @package App\Controller
*/
class ClubApiController extends AbstractController
{
private ClubService $clubService;
/** @var TranslatorInterface */
private TranslatorInterface $translator;
public function __construct(ClubService $clubService, TranslatorInterface $translator)
{
$this->clubService = $clubService;
$this->translator = $translator;
}
/**
* @Route(path="/api/{version}/club/all", methods={"POST"})
* @param Request $request
* @return JsonResponse
*/
public function getAllClubs(Request $request): JsonResponse
{
$response = [
"success" => false,
];
$partnerId = $request->request->get("partnerId", "");
Assert::notEmpty($partnerId, $this->translator->trans("partnerId is required"));
try {
$clubs = $this->clubService->getAll();
$response = [
"success" => true,
"data" => [
"clubs" => []
],
];
if (!empty($clubs)) {
/** @var Club $club */
foreach ($clubs as $club) {
if (!$club->getAllowInExport()) {
continue;
}
$response["data"]["clubs"][$club->getId()] = [
"id" => $club->getId(),
"title" => $club->getTitle(),
"address" => $club->getAddress(),
"withdrawal_limit" => $club->getWithdrawalLimit(),
"withdrawal_limit_min" => $club->getWithdrawalLimitMin(),
"lat" => $club->getLat(),
"lon" => $club->getLon()
];
}
}
} catch (Exception $e) {
$response["message"] = $e->getMessage();
}
return new JsonResponse($response);
}
}