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