<?php
namespace App\Controller\Ads\Api;
use App\Controller\Ads\BaseAdsController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api", name="api_ads")
*/
class IndexAdsController extends BaseAdsController
{
/**
* @Route("/ads", name="index")
* @return JsonResponse
*/
public function __invoke(Request $request): Response
{
$response = [];
if(!$request->query->get('token')) {
return $this->json([
'code' => 400,
'status' => 'error',
'message' => 'Forbidden'
]);
}
$site = $this->siteRepository->getOneToken($request->query->get('token'));
if($site == null) {
return $this->json([
'code' => 400,
'status' => 'error',
'message' => 'Forbidden'
]);
}
$items = $this->adsRepository->listBy([
'site' => $site->getId(),
'order_by' => 'priority',
'type' => $request->query->get('type'),
'disabled' => true
]);
$response['count'] = $this->adsRepository->getCount();
foreach ($items as $item) {
$response['body'][$item->getPosition()][] = $this->adsService->getFields($item);
}
return $this->json($response);
}
}