Created API endpoints to undo battery charge cycles and chore executions (references #63)

This commit is contained in:
Bernd Bestel 2018-10-27 10:55:30 +02:00
parent 1f3dd58ddf
commit fe83e2fa6f
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
8 changed files with 164 additions and 6 deletions

View File

@ -49,4 +49,17 @@ class BatteriesApiController extends BaseApiController
{
return $this->ApiResponse($this->BatteriesService->GetCurrent());
}
public function UndoChargeCycle(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
try
{
$this->ApiResponse($this->BatteriesService->UndoChargeCycle($args['chargeCycleId']));
return $this->ApiResponse(array('success' => true));
}
catch (\Exception $ex)
{
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
}
}
}

View File

@ -55,4 +55,17 @@ class ChoresApiController extends BaseApiController
{
return $this->ApiResponse($this->ChoresService->GetCurrent());
}
public function UndoChoreExecution(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
try
{
$this->ApiResponse($this->ChoresService->UndoChoreExecution($args['executionId']));
return $this->ApiResponse(array('success' => true));
}
catch (\Exception $ex)
{
return $this->VoidApiActionResponse($response, false, 400, $ex->getMessage());
}
}
}

View File

@ -41,7 +41,7 @@ class ChoresController extends BaseController
public function Analysis(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'choresanalysis', [
'choresLog' => $this->Database->chores_log()->orderBy('tracked_time', 'DESC'),
'choresLog' => $this->Database->chores_log()->where('undone', 0)->orderBy('tracked_time', 'DESC'),
'chores' => $this->Database->chores()->orderBy('name'),
'users' => $this->Database->users()->orderBy('username')
]);

View File

@ -1514,6 +1514,47 @@
}
}
},
"/chores/undo-chore-execution/{executionId}": {
"get": {
"description": "Undoes a chore execution",
"tags": [
"Chores"
],
"parameters": [
{
"in": "path",
"name": "executionId",
"required": true,
"description": "A valid chore execution id",
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "A VoidApiActionResponse object",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/VoidApiActionResponse"
}
}
}
},
"400": {
"description": "A VoidApiActionResponse object (possible errors are: Not existing booking)",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorExampleVoidApiActionResponse"
}
}
}
}
}
}
},
"/batteries/track-charge-cycle/{batteryId}": {
"get": {
"description": "Tracks a charge cycle of the given battery",
@ -1629,6 +1670,47 @@
}
}
},
"/batteries/undo-charge-cycle/{chargeCycleId}": {
"get": {
"description": "Undoes a chore execution",
"tags": [
"Batteries"
],
"parameters": [
{
"in": "path",
"name": "chargeCycleId",
"required": true,
"description": "A valid charge cycle id",
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "A VoidApiActionResponse object",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/VoidApiActionResponse"
}
}
}
},
"400": {
"description": "A VoidApiActionResponse object (possible errors are: Not existing booking)",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorExampleVoidApiActionResponse"
}
}
}
}
}
}
},
"/tasks/get-current": {
"get": {
"description": "Returns all tasks which are not done yet",

View File

@ -6,3 +6,21 @@ SET undone = 0;
ALTER TABLE stock_log
ADD undone_timestamp DATETIME;
ALTER TABLE chores_log
ADD undone TINYINT NOT NULL DEFAULT 0 CHECK(undone IN (0, 1));
UPDATE chores_log
SET undone = 0;
ALTER TABLE chores_log
ADD undone_timestamp DATETIME;
ALTER TABLE battery_charge_cycles
ADD undone TINYINT NOT NULL DEFAULT 0 CHECK(undone IN (0, 1));
UPDATE battery_charge_cycles
SET undone = 0;
ALTER TABLE battery_charge_cycles
ADD undone_timestamp DATETIME;

View File

@ -124,11 +124,13 @@ $app->group('/api', function()
$this->get('/chores/track-chore-execution/{choreId}', '\Grocy\Controllers\ChoresApiController:TrackChoreExecution');
$this->get('/chores/get-chore-details/{choreId}', '\Grocy\Controllers\ChoresApiController:ChoreDetails');
$this->get('/chores/get-current', '\Grocy\Controllers\ChoresApiController:Current');
$this->get('/chores/undo-chore-execution/{executionId}', '\Grocy\Controllers\ChoresApiController:UndoChoreExecution');
// Batteries
$this->get('/batteries/track-charge-cycle/{batteryId}', '\Grocy\Controllers\BatteriesApiController:TrackChargeCycle');
$this->get('/batteries/get-battery-details/{batteryId}', '\Grocy\Controllers\BatteriesApiController:BatteryDetails');
$this->get('/batteries/get-current', '\Grocy\Controllers\BatteriesApiController:Current');
$this->get('/batteries/undo-charge-cycle/{chargeCycleId}', '\Grocy\Controllers\BatteriesApiController:UndoChargeCycle');
// Tasks
$this->get('/tasks/get-current', '\Grocy\Controllers\TasksApiController:Current');

View File

@ -18,8 +18,8 @@ class BatteriesService extends BaseService
}
$battery = $this->Database->batteries($batteryId);
$batteryChargeCylcesCount = $this->Database->battery_charge_cycles()->where('battery_id', $batteryId)->count();
$batteryLastChargedTime = $this->Database->battery_charge_cycles()->where('battery_id', $batteryId)->max('tracked_time');
$batteryChargeCylcesCount = $this->Database->battery_charge_cycles()->where('battery_id = :1 AND undone = 0', $batteryId)->count();
$batteryLastChargedTime = $this->Database->battery_charge_cycles()->where('battery_id = :1 AND undone = 0', $batteryId)->max('tracked_time');
$nextChargeTime = $this->Database->batteries_current()->where('battery_id', $batteryId)->min('next_estimated_charge_time');
return array(
@ -51,4 +51,19 @@ class BatteriesService extends BaseService
$batteryRow = $this->Database->batteries()->where('id = :1', $batteryId)->fetch();
return $batteryRow !== null;
}
public function UndoChargeCycle($chargeCycleId)
{
$logRow = $this->Database->battery_charge_cycles()->where('id = :1 AND undone = 0', $chargeCycleId)->fetch();
if ($logRow == null)
{
throw new \Exception('Charge cycle does not exist or was already undone');
}
// Update log entry
$logRow->update(array(
'undone' => 1,
'undone_timestamp' => date('Y-m-d H:i:s')
));
}
}

View File

@ -21,11 +21,11 @@ class ChoresService extends BaseService
}
$chore = $this->Database->chores($choreId);
$choreTrackedCount = $this->Database->chores_log()->where('chore_id', $choreId)->count();
$choreLastTrackedTime = $this->Database->chores_log()->where('chore_id', $choreId)->max('tracked_time');
$choreTrackedCount = $this->Database->chores_log()->where('chore_id = :1 AND undone = 0', $choreId)->count();
$choreLastTrackedTime = $this->Database->chores_log()->where('chore_id = :1 AND undone = 0', $choreId)->max('tracked_time');
$nextExeuctionTime = $this->Database->chores_current()->where('chore_id', $choreId)->min('next_estimated_execution_time');
$lastChoreLogRow = $this->Database->chores_log()->where('chore_id = :1 AND tracked_time = :2', $choreId, $choreLastTrackedTime)->fetch();
$lastChoreLogRow = $this->Database->chores_log()->where('chore_id = :1 AND tracked_time = :2 AND undone = 0', $choreId, $choreLastTrackedTime)->fetch();
$lastDoneByUser = null;
if ($lastChoreLogRow !== null && !empty($lastChoreLogRow))
{
@ -71,4 +71,19 @@ class ChoresService extends BaseService
$choreRow = $this->Database->chores()->where('id = :1', $choreId)->fetch();
return $choreRow !== null;
}
public function UndoChoreExecution($executionId)
{
$logRow = $this->Database->chores_log()->where('id = :1 AND undone = 0', $executionId)->fetch();
if ($logRow == null)
{
throw new \Exception('Execution does not exist or was already undone');
}
// Update log entry
$logRow->update(array(
'undone' => 1,
'undone_timestamp' => date('Y-m-d H:i:s')
));
}
}