Added an API endpoint to search for objects by name (closes #337)

This commit is contained in:
Bernd Bestel 2019-08-10 13:07:08 +02:00
parent e6020432c6
commit fa326fdfda
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
4 changed files with 96 additions and 0 deletions

View File

@ -6,3 +6,4 @@
- API improvements
- New endpoint `/stock/shoppinglist/remove-product` to remove a product from a shopping list (thanks @Forceu)
- When adding a product (through `stock/product/{productId}/add` or `stock/product/{productId}/inventory`) with omitted best before date and if the given product has "Default best before days" set, the best before date is calculated based on that (so far always today was used which is still the case when no date is supplied and also the product has no "Default best before days set) (thanks @Forceu)
- New endpoint `/objects/{entity}/search/{searchString}` search for objects by name (contains search)

View File

@ -113,6 +113,25 @@ class GenericEntityApiController extends BaseApiController
}
}
public function SearchObjects(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
if ($this->IsValidEntity($args['entity']) && !$this->IsEntityWithPreventedListing($args['entity']))
{
try
{
return $this->ApiResponse($this->Database->{$args['entity']}()->where('name LIKE ?', '%' . $args['searchString'] . '%'));
}
catch (\PDOException $ex)
{
return $this->GenericErrorResponse($response, 'The given entity has no field "name"');
}
}
else
{
return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed');
}
}
public function GetUserfields(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
try

View File

@ -452,6 +452,81 @@
}
}
},
"/objects/{entity}/search/{searchString}": {
"get": {
"summary": "Returns all objects of the given entity where the field \"name\" contains the search string (so only works for entities which have that field",
"tags": [
"Generic entity interactions"
],
"parameters": [
{
"in": "path",
"name": "entity",
"required": true,
"description": "A valid entity name",
"schema": {
"$ref": "#/components/internalSchemas/ExposedEntity"
}
},
{
"in": "path",
"name": "searchString",
"required": true,
"description": "The search string",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "An entity object",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/components/schemas/Product"
},
{
"$ref": "#/components/schemas/Chore"
},
{
"$ref": "#/components/schemas/Battery"
},
{
"$ref": "#/components/schemas/Location"
},
{
"$ref": "#/components/schemas/QuantityUnit"
},
{
"$ref": "#/components/schemas/ShoppingListItem"
},
{
"$ref": "#/components/schemas/StockEntry"
}
]
}
}
}
}
},
"400": {
"description": "The operation was not successful",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/GenericErrorResponse"
}
}
}
}
}
}
},
"/userfields/{entity}/{objectId}": {
"get": {
"summary": "Returns all userfields with their values of the given object of the given entity",

View File

@ -124,6 +124,7 @@ $app->group('/api', function()
// Generic entity interaction
$this->get('/objects/{entity}', '\Grocy\Controllers\GenericEntityApiController:GetObjects');
$this->get('/objects/{entity}/{objectId}', '\Grocy\Controllers\GenericEntityApiController:GetObject');
$this->get('/objects/{entity}/search/{searchString}', '\Grocy\Controllers\GenericEntityApiController:SearchObjects');
$this->post('/objects/{entity}', '\Grocy\Controllers\GenericEntityApiController:AddObject');
$this->put('/objects/{entity}/{objectId}', '\Grocy\Controllers\GenericEntityApiController:EditObject');
$this->delete('/objects/{entity}/{objectId}', '\Grocy\Controllers\GenericEntityApiController:DeleteObject');