Commit 8a886d0e authored by Laura Heimann's avatar Laura Heimann

minimized promo code, added activePromos api endpoint

parent 3143cb7e
......@@ -25,6 +25,8 @@ class APIPromosController extends AbstractController
*/
public function promos(Request $request)
{
// TODO: DEPRECATION
$em = $this->getDoctrine()->getManager();
$results = $em->getRepository(Promo::class)->findBy(array('isVisible' => true), array('id' => 'DESC'), 2);
......@@ -35,19 +37,31 @@ class APIPromosController extends AbstractController
return $response;
} else {
foreach($results as $result) {
$oneResult = [];
$oneResult['id'] = $result->getId();
$oneResult['title'] = $result->getTitle();
$oneResult['type'] = $result->getType();
$oneResult['textColor'] = $result->getTextColor();
$oneResult['color'] = $result->getColor();
$oneResult['button']['type'] = $result->getButtonType();
$oneResult['button']['data'] = $result->getButtonData();
$oneResult['isVisible'] = $result->getIsVisible();
$oneResult['image_path'] = $baseUrl."/uploads/promo/".$result->getImagePath();
$data[] = $oneResult;
$data[] = $result->getJSON();
}
$response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 200, 'data' => $data]);
return $response;
}
}
/**
* @Route("/api/activePromos", name="api.activePromos")
* @Route("/api/activePromos/")
*/
public function activePromos(Request $request)
{
$em = $this->getDoctrine()->getManager();
$results = $em->getRepository(Promo::class)->findBy(array('isVisible' => true), array('id' => 'DESC'));
$baseUrl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
if(!$results) {
$response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 404, 'data' => []]);
return $response;
} else {
foreach($results as $result) {
$data[] = $result->getJSON();
}
$response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 200, 'data' => $data]);
......
......@@ -156,4 +156,20 @@ class Promo
return $this;
}
public function getJSON() {
return array(
'id' => $this->id,
'title' => $this->title,
'type' => $this->type,
'textColor' => $this->textColor,
'color' => $this->color,
'button' => array(
'type' => $this->buttonType,
'data' => $this->buttonData
),
'isVisible' => $this->isVisible,
'image_path' => $_ENV['ASSET_BASE_URL']."/".$_ENV['ASSET_PROMO_FOLDER']."/".$this->imagePath,
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment