From 545f52b66aaa7974852c637eb02d885a0dbb7c18 Mon Sep 17 00:00:00 2001 From: "florian.azizian" <florian.azizian@maarch.org> Date: Tue, 23 Jul 2019 22:30:20 +0100 Subject: [PATCH] FEAT #11292 TIME 3:30 CRUD folders --- rest/index.php | 7 + sql/develop.sql | 1 + .../folder/controllers/FolderController.php | 147 ++++++++++++++++++ src/app/folder/models/FolderModel.php | 21 +++ src/app/folder/models/FolderModelAbstract.php | 98 ++++++++++++ 5 files changed, 274 insertions(+) create mode 100755 src/app/folder/controllers/FolderController.php create mode 100755 src/app/folder/models/FolderModel.php create mode 100755 src/app/folder/models/FolderModelAbstract.php diff --git a/rest/index.php b/rest/index.php index 063ce6763bc..395c1cbd18c 100755 --- a/rest/index.php +++ b/rest/index.php @@ -184,6 +184,13 @@ $app->put('/groups/{id}/indexing', \Group\controllers\GroupController::class . ' $app->put('/groups/{id}/services/{serviceId}', \Group\controllers\GroupController::class . ':updateService'); $app->put('/groups/{id}/reassign/{newGroupId}', \Group\controllers\GroupController::class . ':reassignUsers'); +//Folders +$app->get('/folders', \Folder\controllers\FolderController::class . ':get'); +$app->post('/folders', \Folder\controllers\FolderController::class . ':create'); +$app->get('/folders/{id}', \Folder\controllers\FolderController::class . ':getById'); +$app->put('/folders/{id}', \Folder\controllers\FolderController::class . ':update'); +$app->delete('/folders/{id}', \Folder\controllers\FolderController::class . ':delete'); + //Histories $app->get('/histories', \History\controllers\HistoryController::class . ':get'); $app->get('/histories/users/{userSerialId}', \History\controllers\HistoryController::class . ':getByUserId'); diff --git a/sql/develop.sql b/sql/develop.sql index f5d1c1b6c37..a3677bab89a 100755 --- a/sql/develop.sql +++ b/sql/develop.sql @@ -66,6 +66,7 @@ DELETE FROM actions WHERE action_page = 'view' OR component = 'viewDoc'; /* FOLDERS */ ALTER TABLE folders RENAME TO folder_tmp; +ALTER TABLE folder_tmp RENAME CONSTRAINT folders_pkey to folders_tmp_pkey; CREATE TABLE folders ( id serial NOT NULL, diff --git a/src/app/folder/controllers/FolderController.php b/src/app/folder/controllers/FolderController.php new file mode 100755 index 00000000000..4382120cde3 --- /dev/null +++ b/src/app/folder/controllers/FolderController.php @@ -0,0 +1,147 @@ +<?php + +/** + * Copyright Maarch since 2008 under licence GPLv3. + * See LICENCE.txt file at the root folder for more details. + * This file is part of Maarch software. + */ + +/** + * @brief Folder Controller + * + * @author dev@maarch.org + */ + +namespace Folder\controllers; + +use Folder\models\FolderModel; +use History\controllers\HistoryController; +use Respect\Validation\Validator; +use Slim\Http\Request; +use Slim\Http\Response; +use User\models\UserModel; + +class FolderController +{ + public function get(Request $request, Response $response) + { + $folders = FolderModel::get(); + return $response->withJson(['folders' => $folders]); + } + + public function getById(Request $request, Response $response, array $aArgs) + { + if (!Validator::numeric()->notEmpty()->validate($aArgs['id'])) { + return $response->withStatus(400)->withJson(['errors' => 'Query id is empty or not an integer']); + } + + // Check rights + + $folder = FolderModel::getById(['id' => $aArgs['id']]); + if (empty($folder)) { + return $response->withStatus(400)->withJson(['errors' => 'Folder not found']); + } + + return $response->withJson(['folder' => $folder]); + } + + public function create(Request $request, Response $response) + { + $data = $request->getParams(); + + if (!Validator::stringType()->notEmpty()->validate($data['label'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']); + } + if (!empty($data['parent_id']) && !Validator::intval()->validate($data['parent_id'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body parent_id is not a numeric']); + } + if (!Validator::boolVal()->notEmpty()->validate($data['public'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body public is empty or not a boolean']); + } + + $currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]); + + // Check rights parent_id + + $id = FolderModel::create([ + 'label' => $data['label'], + 'public' => true, + 'sharing' => $data['sharing'], + 'user_id' => $currentUser['id'], + 'parent_id' => $data['parent_id'] + ]); + HistoryController::add([ + 'tableName' => 'folders', + 'recordId' => $id, + 'eventType' => 'ADD', + 'info' => _FOLDER_CREATION . " : {$id}", + 'moduleId' => 'folder', + 'eventId' => 'folderCreation', + ]); + + return $response->withJson(['folder' => $id]); + } + + public function update(Request $request, Response $response, array $aArgs) + { + $data = $request->getParams(); + + if (!Validator::numeric()->notEmpty()->validate($aArgs['id'])) { + return $response->withStatus(400)->withJson(['errors' => 'Query id is empty or not an integer']); + } + if (!Validator::stringType()->notEmpty()->validate($data['label'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']); + } + if (!empty($data['parent_id']) &&!Validator::intval()->validate($data['parent_id'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body parent_id is not a numeric']); + } + if (!Validator::boolVal()->notEmpty()->validate($data['public'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body public is empty or not a boolean']); + } + + // Check rights + // Check rights parent_id + + FolderModel::update([ + 'set' => [ + 'label' => $data['label'], + 'public' => empty($data['public']) ? 'false' : 'true', + 'sharing' => $data['sharing'], + 'parent_id' => $data['parent_id'] + ], + 'where' => ['id = ?'], + 'data' => [$aArgs['id']] + ]); + HistoryController::add([ + 'tableName' => 'folders', + 'recordId' => $aArgs['id'], + 'eventType' => 'UP', + 'info' => _FOLDER_MODIFICATION . " : {$aArgs['id']}", + 'moduleId' => 'folder', + 'eventId' => 'folderModification', + ]); + + return $response->withStatus(200); + } + + public function delete(Request $request, Response $response, array $aArgs) + { + if (!Validator::numeric()->notEmpty()->validate($aArgs['id'])) { + return $response->withStatus(400)->withJson(['errors' => 'Query id is empty or not an integer']); + } + + // Check rights + + FolderModel::delete(['id' => $aArgs['id']]); + HistoryController::add([ + 'tableName' => 'folder', + 'recordId' => $aArgs['id'], + 'eventType' => 'DEL', + 'info' => _BASKET_SUPPRESSION . " : {$aArgs['id']}", + 'moduleId' => 'folder', + 'eventId' => 'folderSuppression', + ]); + + return $response->withStatus(200); + } +} diff --git a/src/app/folder/models/FolderModel.php b/src/app/folder/models/FolderModel.php new file mode 100755 index 00000000000..6c53d63c946 --- /dev/null +++ b/src/app/folder/models/FolderModel.php @@ -0,0 +1,21 @@ +<?php + +/** +* Copyright Maarch since 2008 under licence GPLv3. +* See LICENCE.txt file at the root folder for more details. +* This file is part of Maarch software. +* +*/ + +/** +* @brief Folder Model +* @author dev@maarch.org +* @ingroup core +*/ + +namespace Folder\models; + +class FolderModel extends FolderModelAbstract +{ + // Do your stuff in this class +} diff --git a/src/app/folder/models/FolderModelAbstract.php b/src/app/folder/models/FolderModelAbstract.php new file mode 100755 index 00000000000..140ce27dc1e --- /dev/null +++ b/src/app/folder/models/FolderModelAbstract.php @@ -0,0 +1,98 @@ +<?php +/** +* Copyright Maarch since 2008 under licence GPLv3. +* See LICENCE.txt file at the root folder for more details. +* This file is part of Maarch software. + +* @brief FolderModelAbstract +* @author dev <dev@maarch.org> +* @ingroup core +*/ + +namespace Folder\models; + +use SrcCore\models\ValidatorModel; +use SrcCore\models\DatabaseModel; + +class FolderModelAbstract +{ + public static function get(array $aArgs = []) + { + ValidatorModel::arrayType($aArgs, ['select']); + + $folderType = DatabaseModel::select([ + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], + 'table' => ['folders'] + ]); + + return $folderType; + } + + public static function getById(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id']); + ValidatorModel::intVal($aArgs, ['id']); + + $aGroups = DatabaseModel::select([ + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], + 'table' => ['folders'], + 'where' => ['id = ?'], + 'data' => [$aArgs['id']] + ]); + + return $aGroups[0]; + } + + public static function create(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['user_id', 'label']); + ValidatorModel::stringType($aArgs, ['label']); + ValidatorModel::intVal($aArgs, ['user_id', 'parent_id']); + ValidatorModel::boolType($aArgs, ['public']); + + $nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'folders_id_seq']); + + DatabaseModel::insert([ + 'table' => 'folders', + 'columnsValues' => [ + 'id' => $nextSequenceId, + 'label' => $aArgs['label'], + 'public' => empty($aArgs['public']) ? 'false' : 'true', + 'sharing' => $aArgs['sharing'], + 'user_id' => $aArgs['user_id'], + 'parent_id' => $aArgs['parent_id'] + ] + ]); + + return $nextSequenceId; + } + + public static function update(array $args) + { + ValidatorModel::notEmpty($args, ['where']); + ValidatorModel::arrayType($args, ['set', 'where', 'data']); + + DatabaseModel::update([ + 'table' => 'folders', + 'set' => empty($args['set']) ? [] : $args['set'], + 'where' => $args['where'], + 'data' => empty($args['data']) ? [] : $args['data'] + ]); + + return true; + } + + public static function delete(array $aArgs) + { + ValidatorModel::notEmpty($aArgs, ['id']); + ValidatorModel::intVal($aArgs, ['id']); + + DatabaseModel::delete([ + 'table' => 'folders', + 'where' => ['id = ?'], + 'data' => [$aArgs['id']] + ]); + + return true; + } +} -- GitLab