Skip to content
Snippets Groups Projects
Verified Commit 4a3986c6 authored by Damien's avatar Damien
Browse files

FEAT #16063 TIME 3:15 WIP Tiles update delete and update positions

parent 4d4b1002
No related branches found
No related tags found
No related merge requests found
...@@ -528,6 +528,9 @@ $app->post('/templates/{id}/mergeEmail', \Template\controllers\TemplateControlle ...@@ -528,6 +528,9 @@ $app->post('/templates/{id}/mergeEmail', \Template\controllers\TemplateControlle
//Tiles //Tiles
$app->get('/tiles', \Home\controllers\TileController::class . ':get'); $app->get('/tiles', \Home\controllers\TileController::class . ':get');
$app->post('/tiles', \Home\controllers\TileController::class . ':create'); $app->post('/tiles', \Home\controllers\TileController::class . ':create');
$app->put('/tiles/{id}', \Home\controllers\TileController::class . ':update');
$app->delete('/tiles/{id}', \Home\controllers\TileController::class . ':delete');
$app->put('/tilesPositions', \Home\controllers\TileController::class . ':updatePositions');
//Users //Users
$app->put('/users/export', \User\controllers\UserController::class . ':getExport'); $app->put('/users/export', \User\controllers\UserController::class . ':getExport');
......
...@@ -33,6 +33,10 @@ class TileController ...@@ -33,6 +33,10 @@ class TileController
'data' => [$GLOBALS['id']] 'data' => [$GLOBALS['id']]
]); ]);
foreach ($tiles as $key => $tile) {
$tiles['parameters'] = json_decode($tile['parameters'], true);
}
return $response->withJson(['tiles' => $tiles]); return $response->withJson(['tiles' => $tiles]);
} }
...@@ -69,7 +73,7 @@ class TileController ...@@ -69,7 +73,7 @@ class TileController
HistoryController::add([ HistoryController::add([
'tableName' => 'tiles', 'tableName' => 'tiles',
'recordId' => $GLOBALS['id'], 'recordId' => $id,
'eventType' => 'ADD', 'eventType' => 'ADD',
'eventId' => 'tileCreation', 'eventId' => 'tileCreation',
'info' => 'tile creation' 'info' => 'tile creation'
...@@ -77,4 +81,101 @@ class TileController ...@@ -77,4 +81,101 @@ class TileController
return $response->withJson(['id' => $id]); return $response->withJson(['id' => $id]);
} }
public function update(Request $request, Response $response, array $args)
{
$tile = TileModel::getById(['select' => ['user_id'], 'id' => $args['id']]);
if (empty($tile) || $tile['user_id'] != $GLOBALS['id']) {
return $response->withStatus(400)->withJson(['errors' => 'Tile out of perimeter']);
}
$body = $request->getParsedBody();
if (empty($body)) {
return $response->withStatus(400)->withJson(['errors' => 'Body is empty']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['type'] ?? null) || !in_array($body['type'], TileController::TYPES)) {
return $response->withStatus(400)->withJson(['errors' => 'Body type is empty, not a string or not valid']);
} elseif (!Validator::stringType()->notEmpty()->validate($body['view'] ?? null) || !in_array($body['view'], TileController::VIEWS)) {
return $response->withStatus(400)->withJson(['errors' => 'Body view is empty, not a string or not valid']);
}
TileModel::update([
'set' => [
'type' => $body['type'],
'view' => $body['view'],
'parameters' => empty($body['parameters']) ? '{}' : json_encode($body['parameters'])
],
'where' => ['id = ?'],
'data' => [$args['id']]
]);
HistoryController::add([
'tableName' => 'tiles',
'recordId' => $args['id'],
'eventType' => 'UP',
'eventId' => 'tileModification',
'info' => 'tile modification'
]);
return $response->withStatus(204);
}
public function delete(Request $request, Response $response, array $args)
{
$tile = TileModel::getById(['select' => ['user_id'], 'id' => $args['id']]);
if (empty($tile) || $tile['user_id'] != $GLOBALS['id']) {
return $response->withStatus(400)->withJson(['errors' => 'Tile out of perimeter']);
}
TileModel::delete([
'where' => ['id = ?'],
'data' => [$args['id']]
]);
HistoryController::add([
'tableName' => 'tiles',
'recordId' => $args['id'],
'eventType' => 'DEL',
'eventId' => 'tileSuppression',
'info' => 'tile suppression'
]);
return $response->withStatus(204);
}
public function updatePositions(Request $request, Response $response)
{
$body = $request->getParsedBody();
if (empty($body)) {
return $response->withStatus(400)->withJson(['errors' => 'Body is empty']);
} elseif (!Validator::arrayType()->notEmpty()->validate($body['tiles'] ?? null)) {
return $response->withStatus(400)->withJson(['errors' => 'Body tiles is empty not not an array']);
}
$userTiles = TileModel::get(['select' => ['id'], 'where' => ['user_id = ?'], 'data' => [$GLOBALS['id']]]);
if (count($userTiles) != count($body['tiles'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body tiles do not match user tiles']);
}
$allTiles = array_column($userTiles, 'id');
foreach ($body['tiles'] as $tile) {
if (!in_array($tile['id'], $allTiles)) {
return $response->withStatus(400)->withJson(['errors' => 'Tiles out of perimeter']);
}
}
foreach ($body['tiles'] as $key => $tile) {
if ($key != $tile['position']) {
TileModel::update([
'set' => [
'position' => $key,
],
'where' => ['id = ?'],
'data' => [$tile['id']]
]);
}
}
return $response->withStatus(204);
}
} }
...@@ -59,7 +59,7 @@ class TileModel ...@@ -59,7 +59,7 @@ class TileModel
public static function create(array $args) public static function create(array $args)
{ {
ValidatorModel::notEmpty($args, ['user_id', 'type', 'view', 'position']); ValidatorModel::notEmpty($args, ['user_id', 'type', 'view']);
ValidatorModel::stringType($args, ['type', 'view', 'parameters']); ValidatorModel::stringType($args, ['type', 'view', 'parameters']);
ValidatorModel::intVal($args, ['user_id', 'position']); ValidatorModel::intVal($args, ['user_id', 'position']);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment