Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds cycles endpoint to public api. #515

Merged
merged 1 commit into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions config/packages/nelmio_api_doc.yaml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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