Skip to content

Commit

Permalink
Merge pull request #46 from thelia-modules/feature/order
Browse files Browse the repository at this point in the history
add ordering to blockgroup GET
  • Loading branch information
lopes-vincent committed May 20, 2021
2 parents a326dac + 7b2d3c2 commit 3727d63
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions Controller/BlockGroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace TheliaBlocks\Controller;

use OpenApi\Annotations as OA;
use OpenApi\Controller\Admin\BaseAdminOpenApiController;
use OpenApi\Model\Api\ModelFactory;
use OpenApi\Annotations as OA;
use OpenApi\Service\OpenApiService;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\Routing\Annotation\Route;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\HttpFoundation\Request;
use TheliaBlocks\Model\BlockGroup;
use TheliaBlocks\Model\BlockGroupQuery;
use TheliaBlocks\Model\BlockGroupI18n;
use TheliaBlocks\Model\BlockGroupI18nQuery;
use TheliaBlocks\Model\BlockGroupQuery;
use TheliaBlocks\Model\ItemBlockGroup;
use TheliaBlocks\Model\ItemBlockGroupQuery;

Expand Down Expand Up @@ -76,6 +77,7 @@ public function createBlockGroup(
}

$blockGroup->clearItemBlockGroups();

return OpenApiService::jsonResponse(
$modelFactory->buildModel('BlockGroup', $blockGroup)
);
Expand Down Expand Up @@ -145,7 +147,7 @@ public function getBlockGroup(
}

if ($request->get('visible') !== null) {
$visible = (boolean)json_decode(strtolower($request->get('visible')));
$visible = (bool) json_decode(strtolower($request->get('visible')));
$blockGroupQuery->filterByVisible($visible);
}

Expand Down Expand Up @@ -240,7 +242,7 @@ public function getBlockGroups(
}

if (null !== $itemType = $request->get('itemType')) {
$itemBlockGroupQuery =$blockGroupQuery->useItemBlockGroupQuery()
$itemBlockGroupQuery = $blockGroupQuery->useItemBlockGroupQuery()
->filterByItemType($itemType);

if (null !== $itemId = $request->get('itemId')) {
Expand All @@ -251,20 +253,31 @@ public function getBlockGroups(
}

if ($request->get('visible') !== null) {
$visible = (boolean)json_decode(strtolower($request->get('visible')));
$visible = (bool) json_decode(strtolower($request->get('visible')));
$blockGroupQuery->filterByVisible($visible);
}

$order = $request->get('order');

switch ($order) {
case 'id':
$blockGroupQuery->orderById(Criteria::ASC);
break;
case 'id_reverse':
$blockGroupQuery->orderById(Criteria::DESC);
break;
default:
$blockGroupQuery->orderById(Criteria::DESC);
}

$propelTheliaBlocks = $blockGroupQuery->find();

if (empty($propelTheliaBlocks)) {
return OpenApiService::jsonResponse([], 404);
}

$theliaBlocks = array_map(
function ($propelBlockGroup) use ($modelFactory, $request) {
return $modelFactory->buildModel('BlockGroup', $propelBlockGroup, $request->get('locale'));
},
fn ($propelBlockGroup) => $modelFactory->buildModel('BlockGroup', $propelBlockGroup, $request->get('locale')),
iterator_to_array($propelTheliaBlocks)
);

Expand Down Expand Up @@ -364,7 +377,7 @@ public function deleteBlockGroup(

$blockGroup->delete();

return new JsonResponse("Success", 204);
return new JsonResponse('Success', 204);
}

/**
Expand Down Expand Up @@ -409,7 +422,7 @@ public function duplicateBlockGroup(

$blockGroupI18ns = BlockGroupI18nQuery::create()->filterById($blockGroupId)->find();

array_map(function (BlockGroupI18n $blockI18n) use ($newBlockId) {
array_map(function (BlockGroupI18n $blockI18n) use ($newBlockId): void {
$newBlockI18n = $blockI18n->copy();
$newBlockI18n->setId($newBlockId)->save();
}, iterator_to_array($blockGroupI18ns));
Expand Down

0 comments on commit 3727d63

Please sign in to comment.