diff --git a/changelog/67_UNRELEASED_xxxx-xx-xx.md b/changelog/67_UNRELEASED_xxxx-xx-xx.md index e6d7183a..e412d7e1 100644 --- a/changelog/67_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/67_UNRELEASED_xxxx-xx-xx.md @@ -52,4 +52,5 @@ ### API +- Added a new endpoint `/stock/locations/{locationId}/entries` to get all stock entries of a given location (similiar to the already existing endpoint `/stock/products/{productId}/entries`) - Endpoint `/recipes/{recipeId}/consume`: Fixed that consuming partially fulfilled recipes was possible, although an error was already returned in that case (and potentially some of the in-stock ingredients were consumed in fact) diff --git a/controllers/StockApiController.php b/controllers/StockApiController.php index 540a565f..08c8ff5f 100644 --- a/controllers/StockApiController.php +++ b/controllers/StockApiController.php @@ -631,6 +631,11 @@ class StockApiController extends BaseApiController return $this->FilteredApiResponse($response, $this->getStockService()->GetProductStockEntries($args['productId'], false, $allowSubproductSubstitution, true), $request->getQueryParams()); } + public function LocationStockEntries(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) + { + return $this->FilteredApiResponse($response, $this->getStockService()->GetLocationStockEntries($args['locationId']), $request->getQueryParams()); + } + public function ProductStockLocations(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { $allowSubproductSubstitution = false; diff --git a/grocy.openapi.json b/grocy.openapi.json index 2c30a4d0..5bb709b5 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -2792,6 +2792,72 @@ } } }, + "/stock/locations/{locationId}/entries": { + "get": { + "summary": "Returns all stock entries of the given location", + "tags": [ + "Stock" + ], + "parameters": [ + { + "in": "path", + "name": "locationId", + "required": true, + "description": "A valid location id", + "schema": { + "type": "integer" + } + }, + { + "$ref": "#/components/parameters/query" + }, + { + "$ref": "#/components/parameters/order" + }, + { + "$ref": "#/components/parameters/limit" + }, + { + "$ref": "#/components/parameters/offset" + } + ], + "responses": { + "200": { + "description": "An array of StockEntry objects", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/StockEntry" + } + } + } + } + }, + "400": { + "description": "The operation was not successful (possible errors are: Not existing location)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error400" + } + } + } + }, + "500": { + "description": "The operation was not successful (possible errors are invalid field names or conditions in filter parameters provided)", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error500" + } + } + } + } + } + } + }, "/stock/shoppinglist/add-missing-products": { "post": { "summary": "Adds currently missing products (below defined min. stock amount) to the given shopping list", diff --git a/routes.php b/routes.php index 60abf64a..a6ef8bbc 100644 --- a/routes.php +++ b/routes.php @@ -209,6 +209,7 @@ $app->group('/api', function (RouteCollectorProxy $group) { $group->post('/stock/products/by-barcode/{barcode}/transfer', '\Grocy\Controllers\StockApiController:TransferProductByBarcode'); $group->post('/stock/products/by-barcode/{barcode}/inventory', '\Grocy\Controllers\StockApiController:InventoryProductByBarcode'); $group->post('/stock/products/by-barcode/{barcode}/open', '\Grocy\Controllers\StockApiController:OpenProductByBarcode'); + $group->get('/stock/locations/{locationId}/entries', '\Grocy\Controllers\StockApiController:LocationStockEntries'); $group->get('/stock/bookings/{bookingId}', '\Grocy\Controllers\StockApiController:StockBooking'); $group->post('/stock/bookings/{bookingId}/undo', '\Grocy\Controllers\StockApiController:UndoBooking'); $group->get('/stock/transactions/{transactionId}', '\Grocy\Controllers\StockApiController:StockTransactions'); diff --git a/services/StockService.php b/services/StockService.php index 8f8b0d32..b5372f4a 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -820,6 +820,16 @@ class StockService extends BaseService return $result; } + public function GetLocationStockEntries($locationId) + { + if (!$this->LocationExists($locationId)) + { + throw new \Exception('Location does not exist'); + } + + return $this->getDatabase()->stock()->where('location_id', $locationId); + } + public function GetProductStockEntriesForLocation($productId, $locationId, $excludeOpened = false, $allowSubproductSubstitution = false) { $stockEntries = $this->GetProductStockEntries($productId, $excludeOpened, $allowSubproductSubstitution, true);