Skip to content

Commit 4f24a44

Browse files
author
Sebastian Luberriaga
committed
-with-simpler-cget && apidoc
1 parent 3b4a374 commit 4f24a44

File tree

8 files changed

+107
-17
lines changed

8 files changed

+107
-17
lines changed

Lube/GeneratorBundle/Command/GenerateAPICommand.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ protected function configure()
2626
new InputOption('entity' , '', InputOption::VALUE_REQUIRED, 'Target class for API Generation'),
2727
new InputOption('bundle' , '', InputOption::VALUE_REQUIRED, 'Target bundle For API Controller'),
2828
new InputOption('with-update' , '', InputOption::VALUE_NONE, 'Should we add POST/PUT Operations'),
29+
new InputOption('with-simple-cget' , '', InputOption::VALUE_NONE, 'Should we replace cGet operation for a simpler one (no filters)'),
2930
new InputOption('role' , '', InputOption::VALUE_NONE, 'API ROLE ACCESS')
3031
))
3132
->setHelp(<<<EOT
3233
This command <info>api:generate</info> creates basic CRUD operations and RESTfull endpoints for your model.
3334
3435
Using --with-update allows the creation for update/remove operations.
36+
Using --with-simple-cget replaces the cget operation for a simpler one (no filters, order).
3537
36-
<info>php app/console api:generate --entity=AcmeBlogBundle:Post --bundle=AcmeBlogBundle --with-update --role="ROLE_ADMIN"</info>
38+
<info>php app/console api:generate --entity=AcmeBlogBundle:Post --bundle=AcmeBlogBundle --with-update --role="ROLE_ADMIN" --with-simple-cget</info>
3739
EOT
3840
)
3941
->setName('api:generate')
@@ -118,7 +120,18 @@ protected function interact(InputInterface $input, OutputInterface $output)
118120
));
119121

120122
//Actions
121-
if ($input->hasArgument('with-update') && $input->getArgument('with-update') != '')
123+
if ($input->hasArgument('with-simple-cget'))
124+
{
125+
$input->setOption('with-simple-cget', $input->getArgument('with-simple-cget'));
126+
}
127+
else
128+
{
129+
$question = new ConfirmationQuestion('Do you wish your cGet action to have filters and order? <info>[yes]</info> ', true);
130+
131+
$input->setOption('with-simple-cget', $helper->ask($input, $output, $question));
132+
}
133+
134+
if ($input->hasArgument('with-update'))
122135
{
123136
$input->setOption('with-update', $input->getArgument('with-update'));
124137
}
@@ -204,7 +217,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
204217
$Entity['Rol'] = $input->getOption('role');
205218
$Entity['Name'] = $EntityName;
206219
$Entity['Metadata'] = $EntityMetadata;
207-
$Entity['Actions'] = $input->getOption('with-update') ? array('cget', 'get', 'save', 'remove', 'update') : array('cget', 'get');
220+
221+
if ($input->getOption('with-update')) {
222+
array_push($Entity['Actions'], 'save', 'remove', 'update');
223+
}
224+
225+
if ($input->getOption('with-simple-cget')) {
226+
array_push($Entity['Actions'], 'cget_simple');
227+
} else {
228+
array_push($Entity['Actions'], 'cget');
229+
}
230+
231+
array_push($Entity['Actions'], 'get');
232+
208233

209234
$this->renderFile('controller.php.twig',
210235
$BundlePath . '/Controller/' . $EntityName . 'Controller.php',

Lube/GeneratorBundle/Resources/templates/actions/cget.php.twig

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@
33
{% endif %}
44
* @Route("")
55
* @Method({"GET"})
6-
* @ApiDoc(description="Returns a collection of {{ Entity['Name'] }}")
6+
* @ApiDoc(description="Devuelve una lista de {{ Entity['Name'] }} filtrada/ordenada",
7+
* section="{{ Entity['Name'] }}",
8+
* resource=true,
9+
* parameters={
10+
* {"name"="filter_by", "dataType"="json", "required"=false, "description"="Filtros para listar {{ Entity['Name'] }}s"},
11+
* {"name"="order_by", "dataType"="json", "required"=false, "description"="Filtros para ordenar {{ Entity['Name'] }}s"},
12+
* parameters={
13+
* {"name"="offset", "dataType"="integer", "required"=false, "description"="Offset para paginacion"},
14+
* {"name"="limit", "dataType"="integer", "required"=false, "description"="Limit para paginacion"},
15+
* },
16+
* tags={
17+
* "a revisar: Dev" = "#5bc0de"
18+
* }
19+
* )
720
*/
821
public function cgetAction(Request $request)
922
{
@@ -27,7 +40,7 @@
2740

2841
foreach ($errors as $error)
2942
{
30-
$errorMessages[] = (string)$error;
43+
$errorMessages[$error->getProperty()] = $error->getViolation();
3144
}
3245

3346
return new JsonResponse($errorMessages, 400);
@@ -36,7 +49,7 @@
3649

3750
${{ Entity['Name'] | lower }}s = $this->em->getRepository('{{ Bundle['Name'] }}:{{ Entity['Name'] }}')->findBy($filterArray, $orderArray, $limit, $offset);
3851

39-
$Response['data'] = json_decode($this->serializer->serialize(${{ Entity['Name'] | lower }}s, 'json'));
52+
$Response['data'] = $this->utils->serializeByGroup(${{ Entity['Name'] | lower }}s, ['basic'], false);
4053
$Response['total'] = count($this->em->getRepository('{{ Bundle['Name'] }}:{{ Entity['Name'] }}')->findBy($filterArray));
4154

4255
return new JsonResponse($Response);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
{% if Entity['Rol'] %} * @PreAuthorize("hasRole('{{ Entity['Rol'] }}')")
3+
{% endif %}
4+
* @Route("")
5+
* @Method({"GET"})
6+
* @ApiDoc(description="Devuelve una lista de {{ Entity['Name'] }}",
7+
* section="{{ Entity['Name'] }}",
8+
* resource=true,
9+
* parameters={
10+
* {"name"="offset", "dataType"="integer", "required"=false, "description"="Offset para paginacion"},
11+
* {"name"="limit", "dataType"="integer", "required"=false, "description"="Limit para paginacion"},
12+
* },
13+
* tags={
14+
* "a revisar: Dev" = "#5bc0de"
15+
* }
16+
* )
17+
*/
18+
public function cgetAction(Request $request)
19+
{
20+
$offset = $request->query->get('offset', null);
21+
$limit = $request->query->get('limit', null);
22+
23+
${{ Entity['Name'] | lower }}s = $this->em->getRepository('{{ Bundle['Name'] }}:{{ Entity['Name'] }}')->findBy([], [], $limit, $offset);
24+
25+
$Response['data'] = $this->utils->serializeByGroup(${{ Entity['Name'] | lower }}s, ['basic'], false);
26+
$Response['total'] = count($this->em->getRepository('{{ Bundle['Name'] }}:{{ Entity['Name'] }}')->findAll());
27+
28+
return new JsonResponse($Response);
29+
}

Lube/GeneratorBundle/Resources/templates/actions/get.php.twig

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
* @Method({"GET"})
66
* @ParamConverter("{{ Entity['Name'] | lower }}", class="{{ Bundle['Name'] }}:{{ Entity['Name'] }}")
77
* @ApiDoc(
8-
* description="Returns a {{ Entity['Name'] }}",
8+
* description="Retorna un {{ Entity['Name'] }}",
9+
* resource=true,
10+
* section="{{ Entity['Name'] }}",
11+
* tags={
12+
* "a revisar: Dev" = "#5bc0de"
13+
* },
914
* requirements={
1015
* {
1116
* "name"="id",
1217
* "dataType"="integer",
1318
* "requirement"="\d+",
14-
* "description"="Id of the {{ Entity['Name'] }}"
19+
* "description"="Id del {{ Entity['Name'] }}"
1520
* }
1621
* }
1722
* )
1823
*/
1924
public function getAction(${{ Entity['Name'] | lower }})
2025
{
21-
return new JsonResponse(json_decode($this->serializer->serialize(${{ Entity['Name'] | lower }}, 'json')));
26+
return new JsonResponse($this->utils->serializeByGroup(${{ Entity['Name'] | lower }}, ['detailed'], false));
2227
}

Lube/GeneratorBundle/Resources/templates/actions/remove.php.twig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
* @Method({"DELETE"})
66
* @ParamConverter("{{ Entity['Name'] | lower }}", class="{{ Bundle['Name'] }}:{{ Entity['Name'] }}")
77
* @ApiDoc(
8-
* description="Deletes a {{ Entity['Name'] }}"
8+
* description="Remueve un {{ Entity['Name'] }}"
9+
* section="{{ Entity['Name'] }}",
10+
* resource=true,
11+
* tags={
12+
* "a revisar: Dev" = "#5bc0de"
13+
* }
914
* )
1015
*/
1116
public function removeAction(Request $request, ${{ Entity['Name'] | lower }})

Lube/GeneratorBundle/Resources/templates/actions/save.php.twig

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
* @Route(" ")
55
* @Method({"POST"})
66
* @ApiDoc(
7-
* description="Creates a {{ Entity['Name'] }}"
7+
* description="Crea un {{ Entity['Name'] }}"
8+
* section="{{ Entity['Name'] }}",
9+
* resource=true,
10+
* tags={
11+
* "a revisar: Dev" = "#5bc0de"
12+
* }
813
* )
914
*/
1015
public function saveAction(Request $request)
@@ -20,7 +25,7 @@
2025

2126
foreach ($errors as $error)
2227
{
23-
$errorMessages[] = (string)$error;
28+
$errorMessages[$error->getProperty()] = $error->getViolation();
2429
}
2530

2631
return new JsonResponse($errorMessages, 400);
@@ -33,7 +38,7 @@
3338
{
3439
foreach ($errors->getIterator() as $error)
3540
{
36-
$errorMessages[] = $error->getMessage();
41+
$errorMessages[$error->getProperty()] = $error->getViolation();
3742
}
3843

3944
return new JsonResponse($errorMessages, 400);
@@ -43,5 +48,5 @@
4348
$this->em->persist(${{ Entity['Name'] | lower}});
4449
$this->em->flush();
4550

46-
return new JsonResponse(json_decode($this->serializer->serialize(${{ Entity['Name'] | lower }}, 'json')));
51+
return new JsonResponse($this->utils->serializeByGroup(${{ Entity['Name'] | lower }}, ['detailed'], false));
4752
}

Lube/GeneratorBundle/Resources/templates/actions/update.php.twig

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
* @Method({"POST"})
66
* @ParamConverter("{{ Entity['Name'] | lower }}", class="{{ Bundle['Name'] }}:{{ Entity['Name'] }}")
77
* @ApiDoc(
8-
* description="Updates a {{ Entity['Name'] }}"
8+
* description="Actualiza un {{ Entity['Name'] }}"
9+
* section="{{ Entity['Name'] }}",
10+
* resource=true,
11+
* tags={
12+
* "a revisar: Dev" = "#5bc0de"
13+
* }
914
* )
1015
*/
1116
public function updateAction(Request $request, ${{ Entity['Name'] | lower }})
@@ -37,7 +42,7 @@
3742
{
3843
foreach ($errors->getIterator() as $error)
3944
{
40-
$errorMessages[] = $error->getMessage();
45+
$errorMessages[$error->getProperty()] = $error->getViolation();
4146
}
4247

4348
return new JsonResponse($errorMessages, 400);
@@ -48,5 +53,5 @@
4853
$this->em->merge(${{ Entity['Name'] | lower}});
4954
$this->em->flush();
5055

51-
return new JsonResponse(json_decode($this->serializer->serialize(${{ Entity['Name'] | lower }}, 'json')));
56+
return new JsonResponse($this->utils->serializeByGroup(${{ Entity['Name'] | lower }}, ['detailed'], false));
5257
}

Lube/GeneratorBundle/Resources/templates/controller.php.twig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class {{ Entity['Name'] }}Controller extends Controller
3131
/** @Inject("jms_serializer") */
3232
private $serializer;
3333
34+
/** @Inject("tiarg.utils") */
35+
private $utils;
36+
3437
{% for action in Entity['Actions'] %}
3538
3639
{% include 'actions/' ~ action ~ '.php.twig' %}

0 commit comments

Comments
 (0)