diff --git a/changelog/52_UNRELEASED_2019-xx-xx.md b/changelog/52_UNRELEASED_2019-xx-xx.md index 7f80ea56..eb2a5bf5 100644 --- a/changelog/52_UNRELEASED_2019-xx-xx.md +++ b/changelog/52_UNRELEASED_2019-xx-xx.md @@ -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) diff --git a/controllers/GenericEntityApiController.php b/controllers/GenericEntityApiController.php index 8d95ea77..991a7f58 100644 --- a/controllers/GenericEntityApiController.php +++ b/controllers/GenericEntityApiController.php @@ -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 diff --git a/grocy.openapi.json b/grocy.openapi.json index cb6a200b..56a9633f 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -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", diff --git a/routes.php b/routes.php index c79a716b..3e9aa669 100644 --- a/routes.php +++ b/routes.php @@ -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');