Create range filter for numeric attributes
This commit is contained in:
parent
6c5ac312fa
commit
fb80088286
|
@ -214,7 +214,7 @@ $collections = [
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'default' => new stdClass,
|
'default' => new stdClass,
|
||||||
'array' => false,
|
'array' => false,
|
||||||
'filters' => ['json'],
|
'filters' => ['json', 'range'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'$id' => 'filters',
|
'$id' => 'filters',
|
||||||
|
|
|
@ -800,7 +800,9 @@ App::get('/v1/database/collections/:collectionId/attributes')
|
||||||
throw new Exception('Collection not found', 404);
|
throw new Exception('Collection not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$attributes = $collection->getAttribute('attributes', []);
|
$attributes = $dbForInternal->find('attributes', [
|
||||||
|
new Query('collectionId', Query::TYPE_EQUAL, [$collection->getId()])
|
||||||
|
], 100);
|
||||||
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
'sum' => \count($attributes),
|
'sum' => \count($attributes),
|
||||||
|
@ -833,18 +835,9 @@ App::get('/v1/database/collections/:collectionId/attributes/:attributeId')
|
||||||
throw new Exception('Collection not found', 404);
|
throw new Exception('Collection not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search for matching attribute in collection
|
$attribute = $dbForInternal->getDocument('attributes', $attributeId);
|
||||||
$attribute = null;
|
|
||||||
$attributes = $collection->getAttribute('attributes'); /** @var Document[] $attributes */
|
|
||||||
|
|
||||||
foreach ($attributes as $a) {
|
if ($attribute === false) {
|
||||||
if ($a->getId() === $attributeId) {
|
|
||||||
$attribute = $a;
|
|
||||||
break; // stop once the attribute is found
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (\is_null($attribute)) {
|
|
||||||
throw new Exception('Attribute not found', 404);
|
throw new Exception('Attribute not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -867,9 +860,7 @@ App::get('/v1/database/collections/:collectionId/attributes/:attributeId')
|
||||||
};
|
};
|
||||||
|
|
||||||
// Format response
|
// Format response
|
||||||
$default = \json_decode($attribute->getAttribute('default', []), true)['value'] ?? null;
|
// TODO@kodumbeats test if this is necessary with range filter
|
||||||
$attribute->setAttribute('default', $default);
|
|
||||||
|
|
||||||
if ($model === Response::MODEL_ATTRIBUTE_INTEGER || $model === Response::MODEL_ATTRIBUTE_FLOAT)
|
if ($model === Response::MODEL_ATTRIBUTE_INTEGER || $model === Response::MODEL_ATTRIBUTE_FLOAT)
|
||||||
{
|
{
|
||||||
$attribute->setAttribute('min', $formatOptions['min']);
|
$attribute->setAttribute('min', $formatOptions['min']);
|
||||||
|
|
26
app/init.php
26
app/init.php
|
@ -186,10 +186,32 @@ DatabaseOld::addFilter('encrypt',
|
||||||
*/
|
*/
|
||||||
Database::addFilter('defaultValue',
|
Database::addFilter('defaultValue',
|
||||||
function($value) {
|
function($value) {
|
||||||
return \json_encode(['value' => $value]);
|
return json_encode(['value' => $value]);
|
||||||
},
|
},
|
||||||
function($value) {
|
function($value) {
|
||||||
return \json_decode($value, true)['value'];
|
return json_decode($value, true)['value'];
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Database::addFilter('range',
|
||||||
|
function($value, Document $attribute) {
|
||||||
|
if ($attribute->isSet('min')) {
|
||||||
|
$attribute->removeAttribute('min');
|
||||||
|
}
|
||||||
|
if ($attribute->isSet('max')) {
|
||||||
|
$attribute->removeAttribute('max');
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
},
|
||||||
|
function($value, Document $attribute) {
|
||||||
|
$formatOptions = json_decode($attribute->getAttribute('formatOptions', []), true);
|
||||||
|
if (isset($formatOptions['min']) || isset($formatOptions['max'])) {
|
||||||
|
$attribute
|
||||||
|
->setAttribute('min', $formatOptions['min'])
|
||||||
|
->setAttribute('max', $formatOptions['max'])
|
||||||
|
;
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -88,9 +88,6 @@ class DatabaseV1 extends Worker
|
||||||
if(!$dbForExternal->createAttribute($collectionId, $key, $type, $size, $required, $default, $signed, $array, $format, $formatOptions, $filters)) {
|
if(!$dbForExternal->createAttribute($collectionId, $key, $type, $size, $required, $default, $signed, $array, $format, $formatOptions, $filters)) {
|
||||||
throw new Exception('Failed to create Attribute');
|
throw new Exception('Failed to create Attribute');
|
||||||
}
|
}
|
||||||
if (!\is_null($default)) {
|
|
||||||
$attribute->setAttribute('default', json_encode(['value' => $default]));
|
|
||||||
}
|
|
||||||
$dbForInternal->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available'));
|
$dbForInternal->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available'));
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
Console::error($th->getMessage());
|
Console::error($th->getMessage());
|
||||||
|
|
Loading…
Reference in a new issue