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

wip: basic combo searching functionality #4

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php

namespace App\JsonApi\V1\Extensions\SearchByComponents;

use App\Models\Device;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use JsonApiPhp\JsonApi as Structure;
use Psr\Http\Message\ResponseInterface;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Exception\BadRequestException;
use Tobyz\JsonApiServer\Exception\MethodNotAllowedException;
use Tobyz\JsonApiServer\Extension\Extension;
use Tobyz\JsonApiServer\Serializer;

use function Tobyz\JsonApiServer\json_api_response;

class SearchByComponentsExtension extends Extension
{
public function uri(): string
{
return 'http://mobilecombos.com/ext/search-by-components';
}

public function queryToSQL($query)
{
$addSlashes = str_replace('?', "'?'", $query->toSql());

$sql = str_replace('%', '#', $addSlashes);

$sql = str_replace('?', '%s', $sql);

$sql = vsprintf($sql, $query->getBindings());

$sql = str_replace('#', '%', $sql);

return $sql;
}

public function handle(Context $context): ?ResponseInterface
{
if (Arr::has(['/search-by-components'], $context->getPath())) {
return null;
}

$request = $context->getRequest();

if ($request->getMethod() !== 'POST') {
throw new MethodNotAllowedException();
}

$body = $context->getBody();
$searchQuery = $body['query'] ?? null;

if (!isset($searchQuery)) {
throw new BadRequestException('Missing combo search query');
}

$serializer = new Serializer($context);

$query = Device::query();

$query = $this->buildQuery($searchQuery, $query);

// $query = $query->with([
// 'deviceFirmwares',
// 'deviceFirmwares.capabilitySets',
// 'deviceFirmwares.capabilitySets.combos',
// 'deviceFirmwares.capabilitySets.combos.nrComponents',
// 'deviceFirmwares.capabilitySets.combos.lteComponents'
// ]);

dd($this->queryToSQL($query));

$results = $query->get();

foreach ($results as $device) {
$serializer->add(
$context->getApi()->getResourceType('devices'),
$device,
[
'deviceFirmwares' => [
'capabilitySets' => [
'combos' => [
'nrComponents' => [],
'lteComponents' => [],
],
],
],
]
);
}

[$primary, $included] = $serializer->serialize();

return json_api_response(new Structure\CompoundDocument(
new Structure\ResourceCollection(...$primary),
new Structure\Included(...$included),
// new Structure\Link\SelfLink($this->buildUrl($context->getRequest())),
));
}

protected function hashQueryComponent(array $component): string
{
return md5(serialize($component));
}

protected function buildQuery(array $search, Builder $query): Builder
{
$searchWithQuantity = [];

foreach ($search as $componentQuery) {
$hash = $this->hashQueryComponent($componentQuery);

if (!isset($searchWithQuantity[$hash])) {
$searchWithQuantity[$hash] = [
'quantity' => 1,
'query' => $componentQuery,
];
} else {
$searchWithQuantity[$hash]['quantity']++;
}
}

foreach ($searchWithQuantity as $componentQuery) {
$relation = 'deviceFirmwares.capabilitySets.combos.'.
(strtolower(Arr::get($componentQuery, 'query.type')) === 'nr' ? 'nrComponents'
: 'lteComponents');

$query = $query->whereHas($relation, function (Builder $query) use ($componentQuery) {
$bands = Arr::get($componentQuery, 'query.bands');
$dlClass = Arr::get($componentQuery, 'query.dlClass');
$ulClass = Arr::get($componentQuery, 'query.ulClass');
$mimo = Arr::get($componentQuery, 'query.mimo');
$ul_mimo = Arr::get($componentQuery, 'query.ulMimo');
$dl_modulation = Arr::get($componentQuery, 'query.dlModulation');
$ul_modulation = Arr::get($componentQuery, 'query.ulModulation');

if (isset($bands)) {
if (is_array($bands)) {
$query->whereIn('band', $bands);
} else {
$query->where('band', $bands);
}
}

if (isset($dlClass)) {
if (is_array($dlClass)) {
$query->whereIn('dl_class', $dlClass);
} else {
$query->where('dl_class', $dlClass);
}
}

if (isset($ulClass)) {
if (is_array($ulClass)) {
$query->whereIn('ul_class', $ulClass);
} else {
$query->where('ul_class', $ulClass);
}
}

if (isset($mimo)) {
if (is_array($mimo)) {
$query->whereIn('mimo', $mimo);
} else {
$query->where('mimo', $mimo);
}
}

if (isset($ul_mimo)) {
if (is_array($ul_mimo)) {
$query->whereIn('ul_mimo', $ul_mimo);
} else {
$query->where('ul_mimo', $ul_mimo);
}
}

if (isset($dl_modulation)) {
if (is_array($dl_modulation)) {
$query->whereIn('dl_modulation', $dl_modulation);
} else {
$query->where('dl_modulation', $dl_modulation);
}
}

if (isset($ul_modulation)) {
if (is_array($ul_modulation)) {
$query->whereIn('ul_modulation', $ul_modulation);
} else {
$query->where('ul_modulation', $ul_modulation);
}
}

return $query;
}, '=', $componentQuery['quantity']);
}

return $query;
}
}
2 changes: 2 additions & 0 deletions app/JsonApi/V1/JsonApiServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\JsonApi\V1;

use App\JsonApi\V1\Extensions\SearchByComponents\SearchByComponentsExtension;
use Illuminate\Support\Facades\Config;
use Psr\Http\Message\ServerRequestInterface;
use Tobyz\JsonApiServer\ErrorProviderInterface;
Expand All @@ -28,6 +29,7 @@ public function requestHandler(ServerRequestInterface $request): \Psr\Http\Messa
$this->addResources();

$this->server->extension(new Atomic());
$this->server->extension(new SearchByComponentsExtension());
}

if (!$request->hasHeader('Accept')) {
Expand Down