1
0
Fork 0
mirror of synced 2024-06-26 18:20:43 +12:00

Create range filter for numeric attributes

This commit is contained in:
kodumbeats 2021-08-27 16:27:48 -04:00
parent 6c5ac312fa
commit fb80088286
4 changed files with 31 additions and 21 deletions

View file

@ -214,7 +214,7 @@ $collections = [
'required' => false,
'default' => new stdClass,
'array' => false,
'filters' => ['json'],
'filters' => ['json', 'range'],
],
[
'$id' => 'filters',

View file

@ -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']);

View file

@ -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;
}
);

View file

@ -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());