Skip to content

Commit 320a215

Browse files
committed
added batch process
1 parent 265c197 commit 320a215

File tree

6 files changed

+103
-116
lines changed

6 files changed

+103
-116
lines changed

Controller/AbstractCollectionController.php

Lines changed: 0 additions & 107 deletions
This file was deleted.

Controller/BatchController.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,34 @@
1111

1212
namespace ONGR\ApiBundle\Controller;
1313

14-
use ONGR\ApiBundle\Service\Crud;
15-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14+
use Symfony\Component\HttpFoundation\Request;
1615
use Symfony\Component\HttpFoundation\Response;
1716

1817
/**
1918
* Batch controller
2019
*/
21-
class BatchController extends Controller
20+
class BatchController extends AbstractRestController
2221
{
22+
/**
23+
* Main action to process batch call.
24+
*
25+
* @param Request $request
26+
* @return Response
27+
*/
28+
public function processAction(Request $request)
29+
{
30+
$crud = $this->getCrudService();
31+
$repository = $this->getRequestRepository($request);
32+
$documents = $this->get('ongr_api.request_serializer')->deserializeRequest($request);
33+
34+
try {
35+
foreach ($documents as $document) {
36+
$crud->create($repository, $document);
37+
}
38+
$crud->commit($repository);
39+
return $this->renderRest($request, '', Response::HTTP_NO_CONTENT);
40+
} catch (\Exception $e) {
41+
return $this->renderError($request, $e->getMessage(), Response::HTTP_BAD_REQUEST);
42+
}
43+
}
2344
}

DependencyInjection/Configuration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ public function getEndpointNode()
138138
)
139139
->end()
140140

141+
->booleanNode('batch')
142+
->defaultTrue()
143+
->info(
144+
'If set to true user can sent documents in batch\'s.'
145+
)
146+
->end()
147+
141148
->end()
142149
->end();
143150

Routing/ElasticsearchLoader.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ private function processRestRoute(
7878
'_version' => $version,
7979
'repository' => $endpoint['repository'],
8080
];
81-
$requirements = [];
81+
82+
$pattern = $version.'/'.sprintf('%s/{documentId}', strtolower($document));
83+
84+
if ($endpoint['batch']) {
85+
$defaults['_controller'] = 'ONGRApiBundle:Batch:Process';
86+
$batchPattern = $version.'/'.sprintf('%s', strtolower($document)) . '/_batch';
87+
$name = strtolower(sprintf('ongr_api_%s_%s_%s', $version, $document, Request::METHOD_POST));
88+
$collection->add($name.'_batch', new Route($batchPattern, $defaults, [], [], "", [], [Request::METHOD_POST]));
89+
}
8290

8391
foreach ($endpoint['methods'] as $method) {
8492

@@ -90,7 +98,6 @@ private function processRestRoute(
9098
$collection->add($name.'_wi', new Route($postPattern, $defaults, [], [], "", [], [$method]));
9199
}
92100

93-
$pattern = $version.'/'.sprintf('%s/{documentId}', strtolower($document));
94101
$collection->add($name, new Route($pattern, $defaults, [], [], "", [], [$method]));
95102

96103
if ($endpoint['variants']) {

Service/RequestSerializer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use JMS\Serializer\SerializerInterface;
1515
use Symfony\Component\HttpFoundation\Request;
16-
use Symfony\Component\HttpFoundation\RequestStack;
1716

1817
/**
1918
* This class provides all data for deserialization and serialization of API requests.
@@ -66,21 +65,22 @@ public function serializeRequest(Request $request, $data)
6665
*/
6766
public function deserializeRequest(Request $request)
6867
{
69-
$type = 'array';
7068
$format = $this->checkAcceptHeader($request);
7169

7270
try {
73-
return $this->serializer->deserialize($request->getContent(), $type, $format);
71+
return $this->serializer->deserialize($request->getContent(), 'array', $format);
7472
} catch (\Exception $e) {
7573
throw new \RuntimeException(
76-
'Could not deserialize content to object of \'' . $type . '\' type and \'' . $format . '\' format.'
74+
'Could not deserialize content from the request.'
7775
);
7876
}
7977
}
8078

8179
/**
8280
* Returns acceptance type based on given request.
8381
*
82+
* @param Request $request
83+
*
8484
* @return string
8585
*/
8686
public function checkAcceptHeader(Request $request)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ApiBundle\Tests\Functional\Controller;
13+
14+
use ONGR\ElasticsearchBundle\Result\Result;
15+
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
16+
use Symfony\Component\HttpFoundation\Request;
17+
18+
class BatchControllerTest extends AbstractControllerTestCase
19+
{
20+
private $data = [
21+
[
22+
'manufacturer' => 'diesel',
23+
],
24+
[
25+
'manufacturer' => 'levis',
26+
],
27+
[
28+
'manufacturer' => 'armani',
29+
],
30+
];
31+
32+
/**
33+
* Test batch API by sending data batch.
34+
*/
35+
public function testBatchRequest()
36+
{
37+
$this->getManager();
38+
$data = json_encode($this->data);
39+
40+
$response = $this->sendApiRequest(
41+
Request::METHOD_POST,
42+
'/api/v3/jeans/_batch',
43+
$data
44+
);
45+
46+
$repo = $this->getManager()->getRepository('TestBundle:Jeans');
47+
$search = $repo->createSearch()->addQuery(new MatchAllQuery());
48+
$results = $repo->execute($search, Result::RESULTS_ARRAY);
49+
50+
// $resultsFromApi = [];
51+
// foreach ($results as $result) {
52+
// $resultsFromApi[] = $result['manufacturer'];
53+
// }
54+
//
55+
// $originalData = ['diesel']
56+
57+
$this->assertEquals(asort($this->data), asort($results));
58+
}
59+
}

0 commit comments

Comments
 (0)