Skip to content

Commit

Permalink
adds cycles endpoint to pubic api.
Browse files Browse the repository at this point in the history
  • Loading branch information
stopfstedt committed Aug 1, 2020
1 parent 51d0ec4 commit 77f50b6
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
5 changes: 3 additions & 2 deletions config/packages/nelmio_api_doc.yaml
Expand Up @@ -3,14 +3,15 @@ nelmio_api_doc:
documentation:
info:
title: ThronesDB API
version: 1.0.0
version: 1.1.0
description: ThronesDB's public and protected APIs endpoints.
areas:
path_patterns: # an array of regexps
- ^/api/oauth2/deck/
- ^/api/oauth2/decks/
- ^/api/public/card/
- ^/api/public/cards/
- ^/api/public/cycles/
- ^/api/public/decklist/
- ^/api/public/decklists/
- ^/api/public/packs/
- ^/api/public/packs/
81 changes: 81 additions & 0 deletions src/Controller/ApiController.php
Expand Up @@ -4,6 +4,7 @@

use App\Entity\Card;
use App\Entity\CardInterface;
use App\Entity\Cycle;
use App\Entity\Decklist;
use App\Entity\DecklistInterface;
use App\Entity\Pack;
Expand Down Expand Up @@ -113,6 +114,86 @@ public function listPacksAction(Request $request)
return $response;
}

/**
* @Route("/api/public/cycles/", name="api_cycles", methods={"GET"}, options={"i18n" = false})
*
* Get the description of all the cycles as an array of JSON objects.
*
* @Operation(
* tags={"Public"},
* summary="All the Cycles",
* @SWG\Parameter(
* name="jsonp",
* in="query",
* description="JSONP callback",
* required=false,
* @SWG\Schema(type="string")
* ),
* @SWG\Response(
* response="200",
* description="Returned when successful"
* )
* )
*
* @param Request $request
* @return Response
*/
public function listCyclesAction(Request $request)
{
$response = new Response();
$response->setPublic();
$response->setMaxAge($this->container->getParameter('cache_expiration'));
$response->headers->add(array(
'Access-Control-Allow-Origin' => '*',
'Content-Language' => $request->getLocale()
));

$jsonp = $request->query->get('jsonp');

$cycles = $this->getDoctrine()->getRepository(Cycle::class)->findAll();

// check the last-modified-since header
$lastModified = null;
foreach ($cycles as $cycle) {
if (!$lastModified || $lastModified < $cycle->getDateUpdate()) {
$lastModified = $cycle->getDateUpdate();
}
}
$response->setLastModified($lastModified);
if ($response->isNotModified($request)) {
return $response;
}

// build the response
$data = [];
foreach ($cycles as $cycle) {
$packs = array_map(function (PackInterface $pack) {
return $pack->getCode();
}, $cycle->getPacks()->toArray());
$data[] = array(
"name" => $cycle->getName(),
"code" => $cycle->getCode(),
"position" => $cycle->getPosition(),
"url" => $this->get('router')->generate(
'cards_cycle',
array('cycle_code' => $cycle->getCode()),
UrlGeneratorInterface::ABSOLUTE_URL
),
'packs' => $packs
);
}

$content = json_encode($data);
if (isset($jsonp)) {
$content = "$jsonp($content)";
$response->headers->set('Content-Type', 'application/javascript');
} else {
$response->headers->set('Content-Type', 'application/json');
}
$response->setContent($content);
return $response;
}

/**
* Get the description of a card as a JSON object.
*
Expand Down

0 comments on commit 77f50b6

Please sign in to comment.