From 0bbd2d9880c2706c2dafcd886e82cd3c8bdfc0ae Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sun, 30 Sep 2018 10:47:56 +0200 Subject: [PATCH] Prepare user settings API (references #74 and #71) --- controllers/UsersApiController.php | 28 +++++++++ grocy.openapi.json | 99 ++++++++++++++++++++++++++++++ migrations/0039.sql | 10 +++ routes.php | 4 ++ services/UsersService.php | 34 ++++++++++ 5 files changed, 175 insertions(+) create mode 100644 migrations/0039.sql diff --git a/controllers/UsersApiController.php b/controllers/UsersApiController.php index 4b1b10e0..9afa7475 100644 --- a/controllers/UsersApiController.php +++ b/controllers/UsersApiController.php @@ -68,4 +68,32 @@ class UsersApiController extends BaseApiController return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage()); } } + + public function GetUserSetting(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) + { + try + { + $value = $this->UsersService->GetUserSetting(GROCY_USER_ID, $args['settingKey']); + return $this->ApiResponse(array('value' => $value)); + } + catch (\Exception $ex) + { + return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage()); + } + } + + public function SetUserSetting(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) + { + try + { + $requestBody = $request->getParsedBody(); + + $value = $this->UsersService->SetUserSetting(GROCY_USER_ID, $args['settingKey'], $requestBody['value']); + return $this->ApiResponse(array('success' => true)); + } + catch (\Exception $ex) + { + return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage()); + } + } } diff --git a/grocy.openapi.json b/grocy.openapi.json index fcb0883f..aa6cff46 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -617,6 +617,97 @@ } } }, + "/user/settings/{settingKey}": { + "get": { + "description": "Gets the given setting of the currently logged on user", + "tags": [ + "User settings" + ], + "parameters": [ + { + "in": "path", + "name": "settingKey", + "required": true, + "description": "The key of the user setting", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "A UserSetting object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UserSetting" + } + } + } + }, + "400": { + "description": "A VoidApiActionResponse object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorExampleVoidApiActionResponse" + } + } + } + } + } + }, + "post": { + "description": "Sets the given setting of the currently logged on user", + "tags": [ + "User settings" + ], + "requestBody": { + "description": "A valid UserSetting object", + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/UserSetting" + } + } + } + }, + "parameters": [ + { + "in": "path", + "name": "settingKey", + "required": true, + "description": "The key of the user setting", + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "A VoidApiActionResponse object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/VoidApiActionResponse" + } + } + } + }, + "400": { + "description": "A VoidApiActionResponse object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorExampleVoidApiActionResponse" + } + } + } + } + } + } + }, "/stock/add-product/{productId}/{amount}": { "get": { "description": "Adds the the given amount of the given product to stock", @@ -2098,6 +2189,14 @@ "format": "date-time" } } + }, + "UserSetting": { + "type": "object", + "properties": { + "value": { + "type": "string" + } + } } }, "examples": { diff --git a/migrations/0039.sql b/migrations/0039.sql new file mode 100644 index 00000000..7f8e9c80 --- /dev/null +++ b/migrations/0039.sql @@ -0,0 +1,10 @@ +CREATE TABLE user_settings ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + user_id INTEGER NOT NULL, + key TEXT NOT NULL, + value TEXT, + row_created_timestamp DATETIME DEFAULT (datetime('now', 'localtime')), + row_updated_timestamp DATETIME DEFAULT (datetime('now', 'localtime')), + + UNIQUE(user_id, key) +); diff --git a/routes.php b/routes.php index 57814751..28794694 100644 --- a/routes.php +++ b/routes.php @@ -91,6 +91,10 @@ $app->group('/api', function() $this->post('/users/edit/{userId}', '\Grocy\Controllers\UsersApiController:EditUser'); $this->get('/users/delete/{userId}', '\Grocy\Controllers\UsersApiController:DeleteUser'); + // User + $this->get('/user/settings/{settingKey}', '\Grocy\Controllers\UsersApiController:GetUserSetting'); + $this->post('/user/settings/{settingKey}', '\Grocy\Controllers\UsersApiController:SetUserSetting'); + // Stock $this->get('/stock/add-product/{productId}/{amount}', '\Grocy\Controllers\StockApiController:AddProduct'); $this->get('/stock/consume-product/{productId}/{amount}', '\Grocy\Controllers\StockApiController:ConsumeProduct'); diff --git a/services/UsersService.php b/services/UsersService.php index 1bc3f74b..1200f53a 100644 --- a/services/UsersService.php +++ b/services/UsersService.php @@ -50,6 +50,40 @@ class UsersService extends BaseService return $returnUsers; } + public function GetUserSetting($userId, $settingKey) + { + $settingRow = $this->Database->user_settings()->where('user_id = :1 AND key = :2', $userId, $settingKey)->fetch(); + if ($settingRow !== null) + { + return $settingRow->value; + } + else + { + return null; + } + } + + public function SetUserSetting($userId, $settingKey, $settingValue) + { + $settingRow = $this->Database->user_settings()->where('user_id = :1 AND key = :2', $userId, $settingKey)->fetch(); + if ($settingRow !== null) + { + $settingRow->update(array( + 'value' => $settingValue, + 'row_updated_timestamp' => date('Y-m-d H:i:s') + )); + } + else + { + $settingRow = $this->Database->user_settings()->createRow(array( + 'user_id' => $userId, + 'key' => $settingKey, + 'value' => $settingValue + )); + $settingRow->save(); + } + } + private function UserExists($userId) { $userRow = $this->Database->users()->where('id = :1', $userId)->fetch();