Skip to content

Commit

Permalink
Merge pull request #29 from RyanNerd/mutli-column-query
Browse files Browse the repository at this point in the history
Support for multiple columns with query endpoint
  • Loading branch information
RyanNerd authored Jun 25, 2019
2 parents 999612a + bc286f5 commit f40829b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 33 deletions.
63 changes: 36 additions & 27 deletions app/Controllers/QueryActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,38 @@ public function __invoke(Request $request, Response $response, array $args): Res
$value = $args['value'];
$models = null;

if ($value === '*' && $this->allowAll) {
$models = $this->model->get()->all();
} else {
$columnName = $parsedRequest['column_name'];
$operator = $parsedRequest['operator'] ?? '=';
$models = $this->model->where($columnName, $operator, $value)->get();
switch ($value) {
// SELECT *
case '*':
{
if ($this->allowAll) {
$models = $this->model->get()->all();
}
break;
}

// SELECT * WHERE __columnName=value AND __columnName=value [...]
case '_':
{
$model = $this->model;
foreach ($parsedRequest as $item => $value) {
if ($item{0} === '_') {
$columnName = substr($item, 1);
$model = $model->where($columnName, '=', $value);
}
}
$models = $model->get();

break;
}

// SELECT * WHERE `column_name` `operator` `value`
default:
{
$columnName = $parsedRequest['column_name'];
$operator = $parsedRequest['operator'] ?? '=';
$models = $this->model->where($columnName, $operator, $value)->get();
}
}

if ($models !== null) {
Expand All @@ -59,35 +85,18 @@ public function __invoke(Request $request, Response $response, array $args): Res
$responseBody = $responseBody
->setData($dataTables)
->setStatus(200);
return $responseBody();
} else {
$responseBody = $responseBody
->setData(null)
->setStatus(404);
}
}

// Load the model with the given id (PK)
$model = $this->model->find($args['id']);

// If the record is not found then 404 error, otherwise status is 200.
if ($model === null) {
$data = null;
$status = 404;
} else {
// Remove any protected fields from the response
$data = $model->toArray();
$this->sanitize($data, $model::FIELDS);

$status = 200;
$responseBody = $responseBody
->setData(null)
->setStatus(400)
->setMessage('invalid request');
}

// Set the status and data of the ResponseBody
$responseBody = $responseBody
->setData($data)
->setStatus($status);

// Return the response as JSON
return $responseBody();
}
}
34 changes: 28 additions & 6 deletions app/Controllers/QueryValidatorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Willow\Middleware\ResponseBody;
use Slim\Routing\Route;
use Willow\Middleware\ResponseBody;

abstract class QueryValidatorBase
{
Expand All @@ -26,14 +26,36 @@ public function __invoke(Request $request, RequestHandler $handler): ResponseInt
$route = $request->getAttribute('route');
$value = $route->getArgument('value');

if ($value !== '*') {
if (!array_key_exists('column_name', $parsedRequest)) {
$responseBody->registerParam('required', 'column_name', 'string');
} else {
switch ($value)
{
case '*':
break;

case '_':
$columnCount = 0;
foreach ($parsedRequest as $item => $value) {
if ($item{0} === '_') {
$columnName = substr($item, 1);
if (!array_key_exists($columnName, $this->modelFields)) {
$responseBody->registerParam('invalid', $columnName, null);
} else {
$columnCount++;
}
}
}

// This option requires at least one __ColumnName=value
if ($columnCount === 0) {
$responseBody->registerParam('required', '__column', 'string');
}

break;

default:
if (!array_key_exists($parsedRequest['column_name'], $this->modelFields)) {
$responseBody->registerParam('invalid', 'column_name', 'string');
}
}
break;
}

// Are there any missing or required request data points?
Expand Down

0 comments on commit f40829b

Please sign in to comment.