diff --git a/server/App/Controller/GroupsController.php b/server/App/Controller/GroupsController.php index 4c19d79..21bba4f 100644 --- a/server/App/Controller/GroupsController.php +++ b/server/App/Controller/GroupsController.php @@ -44,7 +44,23 @@ } public function remove() { + $session = get_current_session(); + list($group_id) = get_expected_vars($_GET, ["group_id"]); + $group_id = intval($group_id); + $group = new Group(); + if(!$group -> init_by_id($group_id)) { + return error_response("group_remove", "Invalid group_id"); + } + + if(!$group -> remove()) { + return error_response("group_remove", "Something wrong"); + } + + $groups_model = new Groups(); + $groups_model -> normalizing_groups_position($session -> user_id); + + return success_response(); } public function change_position() { diff --git a/server/App/Models/Groups.php b/server/App/Models/Groups.php index 5b8f249..f69911f 100644 --- a/server/App/Models/Groups.php +++ b/server/App/Models/Groups.php @@ -52,7 +52,6 @@ } public function change_group_position(int $group_id, int $new_position) :bool { - $tablename = Group::get_tablename(); $session = get_current_session(); $groups = $session -> user() -> get_groups(); @@ -75,12 +74,12 @@ } if ($new_position > $current_position) { - if ($group->position > $current_position && $group->position <= $new_position) { - $group->position--; + if ($group -> position > $current_position && $group -> position <= $new_position) { + $group -> position--; } } else { - if ($group->position < $current_position && $group->position >= $new_position) { - $group->position++; + if ($group -> position < $current_position && $group -> position >= $new_position) { + $group -> position++; } } } @@ -91,4 +90,19 @@ return true; } + + public function normalizing_groups_position(int $user_id) :bool { + $session = get_current_session(); + $groups = $session -> user() -> get_groups(); + + foreach ($groups as $i => $group) { + $group -> position = $i + 1; + } + + array_map(function($group){ + $group -> update(); + }, $groups); + + return true; + } } \ No newline at end of file diff --git a/server/App/routes_map.php b/server/App/routes_map.php index 1e0f138..27463f9 100755 --- a/server/App/routes_map.php +++ b/server/App/routes_map.php @@ -43,6 +43,11 @@ $groups_controller = new GroupsController(); return $groups_controller -> change_position(); }); + + $router -> linking("GET", "groups-remove", function(){ + $groups_controller = new GroupsController(); + return $groups_controller -> remove(); + }); } function init_routes_map($router) {