<?php
namespace App\Controller\Store\Api;
use App\Controller\Store\BaseStoreController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api", name="api_store")
*/
class SingleStoreController extends BaseStoreController
{
/**
* @Route("/store", name="single")
* @param Request $request
* @return Response
*/
public function single(Request $request): 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'
]);
}
if($request->query->get('id')) {
$store = $this->storeRepository->getOne($request->query->get('id'));
}
if($request->query->get('slug')) {
$store = $this->storeRepository->getOneSlug($request->query->get('slug'));
}
$type = ($request->query->get('type')) ?: null;
$page = ($request->query->get('page')) ?: 1;
$response['body'] = $this->storeService->getFields($store, $site);
$reviews = $this->reviewRepository->listBy([
'store' => $store->getId(),
'limit' => 15,
'fake' => true,
'status' => ['Published'],
'page' => ($type == 'reviews') ? $page : 1,
]);
$reviews_all = $this->reviewRepository->listBy([
'store' => $store->getId(),
'limit' => 15,
'fake' => true,
'page' => ($type == 'reviews') ? $page : 1,
]);
if($reviews != null) {
foreach ($reviews as $review) {
$response['body']['reviews'][] = $this->reviewService->getFields($review);
}
}
$response['body']['count']['reviews'] = $this->reviewRepository->getCount();
$response['body']['count']['reviews_average'] = $this->reviewRepository->getAverage();
$response['body']['count']['reviews_percent'] = ceil($this->reviewRepository->getAverage() * 100 / 5).'%';
$response['body']['total_pages']['reviews'] = ceil($this->reviewRepository->getCount() / 15);
$deals_all = $this->dealRepository->listBy([
'child_mode' => true,
'parent' => true,
'store' => $store->getId(),
'limit' => 15,
'page' => ($type == 'deals') ? $page : 1,
'status' => ['published'],
'site' => $site->getId()
]);
$response['body']['count']['deals_all'] = $this->dealRepository->getCount();
$response['body']['total_pages']['deals_all'] = ceil($this->dealRepository->getCount() / 15);
$deals = $this->dealRepository->listBy([
'child_mode' => true,
'parent' => true,
'store' => $store->getId(),
'limit' => 15,
'page' => ($type == 'deals') ? $page : 1,
'coupon' => 'exclude',
'status' => ['published'],
'site' => $site->getId()
]);
$response['body']['count']['deals'] = $this->dealRepository->getCount();
$response['body']['total_pages']['deals'] = ceil($this->dealRepository->getCount() / 15);
$coupons = $this->dealRepository->listBy([
'child_mode' => true,
'parent' => true,
'store' => $store->getId(),
'limit' => 15,
'page' => ($type == 'coupons') ? $page : 1,
'coupon' => 'view',
'status' => ['published'],
'site' => $site->getId()
]);
$response['body']['count']['coupons'] = $this->dealRepository->getCount();
$response['body']['total_pages']['coupons'] = ceil($this->dealRepository->getCount() / 15);
if($deals != null) {
foreach ($deals as $item) {
$response['body']['deals'][] = $this->dealService->getFields($item);
}
}
if($coupons != null) {
foreach ($coupons as $item) {
$response['body']['coupons'][] = $this->dealService->getFields($item);
}
}
if($deals_all != null) {
foreach ($deals_all as $item) {
$response['body']['deals_all'][] = $this->dealService->getFields($item);
}
}
return $this->json($response);
}
}