src/Controller/ClubApiController.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Club;
  4. use App\Services\ClubService;
  5. use Exception;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Webmozart\Assert\Assert;
  12. /**
  13.  * Class ClubApiController
  14.  * @package App\Controller
  15.  */
  16. class ClubApiController extends AbstractController
  17. {
  18.     private ClubService $clubService;
  19.     /** @var TranslatorInterface */
  20.     private TranslatorInterface $translator;
  21.     public function __construct(ClubService $clubServiceTranslatorInterface $translator)
  22.     {
  23.         $this->clubService $clubService;
  24.         $this->translator $translator;
  25.     }
  26.     /**
  27.      * @Route(path="/api/{version}/club/all", methods={"POST"})
  28.      * @param Request $request
  29.      * @return JsonResponse
  30.      */
  31.     public function getAllClubs(Request $request): JsonResponse
  32.     {
  33.         $response = [
  34.             "success" => false,
  35.         ];
  36.         $partnerId $request->request->get("partnerId""");
  37.         Assert::notEmpty($partnerId$this->translator->trans("partnerId is required"));
  38.         try {
  39.             $clubs $this->clubService->getAll();
  40.             $response = [
  41.                 "success" => true,
  42.                 "data" => [
  43.                     "clubs" => []
  44.                 ],
  45.             ];
  46.             if (!empty($clubs)) {
  47.                 /** @var Club $club */
  48.                 foreach ($clubs as $club) {
  49.                     if (!$club->getAllowInExport()) {
  50.                         continue;
  51.                     }
  52.                     $response["data"]["clubs"][$club->getId()] = [
  53.                         "id" => $club->getId(),
  54.                         "title" => $club->getTitle(),
  55.                         "address" => $club->getAddress(),
  56.                         "withdrawal_limit" => $club->getWithdrawalLimit(),
  57.                         "withdrawal_limit_min" => $club->getWithdrawalLimitMin(),
  58.                         "lat" => $club->getLat(),
  59.                         "lon" => $club->getLon()
  60.                     ];
  61.                 }
  62.             }
  63.         } catch (Exception $e) {
  64.             $response["message"] = $e->getMessage();
  65.         }
  66.         return new JsonResponse($response);
  67.     }
  68. }