Prepare user settings API (references #74 and #71)

This commit is contained in:
Bernd Bestel 2018-09-30 10:47:56 +02:00
parent b81316bd60
commit 0bbd2d9880
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
5 changed files with 175 additions and 0 deletions

View File

@ -68,4 +68,32 @@ class UsersApiController extends BaseApiController
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage()); 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());
}
}
} }

View File

@ -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}": { "/stock/add-product/{productId}/{amount}": {
"get": { "get": {
"description": "Adds the the given amount of the given product to stock", "description": "Adds the the given amount of the given product to stock",
@ -2098,6 +2189,14 @@
"format": "date-time" "format": "date-time"
} }
} }
},
"UserSetting": {
"type": "object",
"properties": {
"value": {
"type": "string"
}
}
} }
}, },
"examples": { "examples": {

10
migrations/0039.sql Normal file
View File

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

View File

@ -91,6 +91,10 @@ $app->group('/api', function()
$this->post('/users/edit/{userId}', '\Grocy\Controllers\UsersApiController:EditUser'); $this->post('/users/edit/{userId}', '\Grocy\Controllers\UsersApiController:EditUser');
$this->get('/users/delete/{userId}', '\Grocy\Controllers\UsersApiController:DeleteUser'); $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 // Stock
$this->get('/stock/add-product/{productId}/{amount}', '\Grocy\Controllers\StockApiController:AddProduct'); $this->get('/stock/add-product/{productId}/{amount}', '\Grocy\Controllers\StockApiController:AddProduct');
$this->get('/stock/consume-product/{productId}/{amount}', '\Grocy\Controllers\StockApiController:ConsumeProduct'); $this->get('/stock/consume-product/{productId}/{amount}', '\Grocy\Controllers\StockApiController:ConsumeProduct');

View File

@ -50,6 +50,40 @@ class UsersService extends BaseService
return $returnUsers; 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) private function UserExists($userId)
{ {
$userRow = $this->Database->users()->where('id = :1', $userId)->fetch(); $userRow = $this->Database->users()->where('id = :1', $userId)->fetch();