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 ...@@ -25,6 +25,8 @@ class APIPromosController extends AbstractController
*/ */
public function promos(Request $request) public function promos(Request $request)
{ {
// TODO: DEPRECATION
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$results = $em->getRepository(Promo::class)->findBy(array('isVisible' => true), array('id' => 'DESC'), 2); $results = $em->getRepository(Promo::class)->findBy(array('isVisible' => true), array('id' => 'DESC'), 2);
...@@ -35,19 +37,31 @@ class APIPromosController extends AbstractController ...@@ -35,19 +37,31 @@ class APIPromosController extends AbstractController
return $response; return $response;
} else { } else {
foreach($results as $result) { foreach($results as $result) {
$oneResult = []; $data[] = $result->getJSON();
}
$oneResult['id'] = $result->getId();
$oneResult['title'] = $result->getTitle(); $response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 200, 'data' => $data]);
$oneResult['type'] = $result->getType(); return $response;
$oneResult['textColor'] = $result->getTextColor(); }
$oneResult['color'] = $result->getColor(); }
$oneResult['button']['type'] = $result->getButtonType();
$oneResult['button']['data'] = $result->getButtonData(); /**
$oneResult['isVisible'] = $result->getIsVisible(); * @Route("/api/activePromos", name="api.activePromos")
$oneResult['image_path'] = $baseUrl."/uploads/promo/".$result->getImagePath(); * @Route("/api/activePromos/")
*/
$data[] = $oneResult; 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]); $response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 200, 'data' => $data]);
......
...@@ -156,4 +156,20 @@ class Promo ...@@ -156,4 +156,20 @@ class Promo
return $this; 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