diff --git a/server/App/Controller/AuthController.php b/server/App/Controller/AuthController.php index e06acfc..5a3478b 100644 --- a/server/App/Controller/AuthController.php +++ b/server/App/Controller/AuthController.php @@ -7,8 +7,7 @@ class AuthController { public function signup() { - $uname = $_GET["username"]; - $password = $_GET["password"]; + list($uname, $password) = get_expected_vars($_GET, ["username", "password"]); $uname = sanitize_input($uname); $auth = new Auth(); @@ -29,8 +28,7 @@ } public function signin() { - $uname = $_GET["username"]; - $password = $_GET["password"]; + list($uname, $password) = get_expected_vars($_GET, ["username", "password"]); $uname = sanitize_input($uname); $auth = new Auth(); @@ -53,7 +51,9 @@ } public function signout() { - $token = sanitize_input($_GET["token"]); + list($token) = get_expected_vars($_GET, ["token"]); + $token = sanitize_input($token); + $auth = new Auth(); if(!$auth -> signout($token)) { diff --git a/server/App/Controller/GroupsController.php b/server/App/Controller/GroupsController.php index 350a814..4c19d79 100644 --- a/server/App/Controller/GroupsController.php +++ b/server/App/Controller/GroupsController.php @@ -2,9 +2,45 @@ namespace App\Controller; +use \App\Models\Groups; +use \App\Entity\Session; +use \App\Entity\Group; + class GroupsController { public function create() { + list($title) = get_expected_vars($_GET, ["title"]); + $title = sanitize_input($title); + $session = get_current_session(); + + $groups = new Groups(); + $group = $groups -> create_group( + $session -> user() -> get_id(), + $title, + "user" + ); + + if(!$group) { + return error_response("create_group", "Something wrong"); + } + + return success_response([ + "group_id" => $group -> get_id(), + ]); + } + + public function get_list() { + $session = get_current_session(); + + $groups = $session -> user() -> get_groups(); + + $groups = array_map(function($group){ + return $group -> to_array(); + }, $groups); + + return success_response([ + "groups" => $groups + ]); } public function remove() { @@ -12,10 +48,43 @@ } public function change_position() { + $session = get_current_session(); + list($group_id, $position) = get_expected_vars($_GET, ["group_id", "position"]); - } + $position = intval($position); + $group_id = intval($group_id); + + $groups = new Groups(); + if(!$groups -> change_group_position($group_id, $position)) { + return error_response("group_change_position", "Something wrong"); + } + + return success_response(); + } public function change_title() { + $session = get_current_session(); + list($group_id, $title) = get_expected_vars($_GET, ["group_id", "title"]); + + $group_id = intval($group_id); + $new_title = sanitize_input($title); + + $group = new Group(); + if(!$group -> init_by_id($group_id)) { + return error_response("change_group_title", "Wrong group_id"); + } + + if($group -> user_id != $session -> user_id) { + return error_response("change_group_title", "Access denied"); + } + + $group -> title = $new_title; + + if(!$group -> update()) { + return error_response("change_group_title", "Something wrong"); + } + + return success_response(); } } \ No newline at end of file diff --git "a/server/App/Entity/Group\nGroup.php" "b/server/App/Entity/Group\nGroup.php" deleted file mode 100644 index 689447d..0000000 --- "a/server/App/Entity/Group\nGroup.php" +++ /dev/null @@ -1,21 +0,0 @@ - set_fields([ - "user_id", "title", "type", "position", "update_at", "create_at" - ]); - } - - public function change_position(int $new_pos) :bool { - - } -} \ No newline at end of file diff --git a/server/App/Entity/Group.php b/server/App/Entity/Group.php new file mode 100644 index 0000000..689447d --- /dev/null +++ b/server/App/Entity/Group.php @@ -0,0 +1,21 @@ + set_fields([ + "user_id", "title", "type", "position", "update_at", "create_at" + ]); + } + + public function change_position(int $new_pos) :bool { + + } +} \ No newline at end of file diff --git a/server/App/Entity/Session.php b/server/App/Entity/Session.php index eb1d23a..40b2dd4 100755 --- a/server/App/Entity/Session.php +++ b/server/App/Entity/Session.php @@ -20,11 +20,20 @@ } public function force_activity() { - $this -> last_activity = get_create_at_timestamp(); + $this -> last_activity = get_formatted_timestamp(); $this -> update(); } - public function user() { + public function user() :User { + if(!isset($this -> user_instance)) { + if(!$this -> user_id) { + return throw new \Exception("Error of creating User instance. User_id is undefined"); + } + + $this -> user_instance = new User(); + $this -> user_instance -> init_by_id($this -> user_id); + } + return $this -> user_instance; } @@ -38,8 +47,8 @@ $this -> user_id = $this -> user() -> get_id(); $this -> state = "active"; $this -> token = gen_token($this -> user() -> get_id()); - $this -> last_activity = get_create_at_timestamp(); - $this -> create_at = get_create_at_timestamp(); + $this -> last_activity = get_formatted_timestamp(); + $this -> create_at = get_formatted_timestamp(); $this -> create_new(); diff --git a/server/App/Entity/User.php b/server/App/Entity/User.php index 629ecc3..af35e81 100755 --- a/server/App/Entity/User.php +++ b/server/App/Entity/User.php @@ -2,6 +2,8 @@ namespace App\Entity; +use \App\Models\Groups; + class User { protected static $tablename = "user"; @@ -14,4 +16,8 @@ "name", "passhash", "create_at" ]); } + + public function get_groups() :Array { + return (new Groups()) -> get_groups_by_user_id($this -> get_id()); + } } \ No newline at end of file diff --git a/server/App/Models/Auth.php b/server/App/Models/Auth.php index e879b88..97db90b 100755 --- a/server/App/Models/Auth.php +++ b/server/App/Models/Auth.php @@ -22,7 +22,7 @@ $user = new User(); $user -> name = $uname; $user -> passhash = $passhash; - $user -> create_at = get_create_at_timestamp(); + $user -> create_at = get_formatted_timestamp(); return $user -> create_new() ? $user : false; } diff --git a/server/App/Models/Groups.php b/server/App/Models/Groups.php index 085321b..5b8f249 100644 --- a/server/App/Models/Groups.php +++ b/server/App/Models/Groups.php @@ -5,11 +5,90 @@ use \App\Entity\Group; class Groups { - public function create_group(int $user_id, String $title, String $type) :Group { + public function total_group_by_user(int $user_id) :int { + $tablename = Group::get_tablename(); + $sql = "SELECT COUNT(*) FROM `{$tablename}` WHERE `user_id`='{$user_id}'"; + $result = app() -> db() -> query($sql); + if(!$result) { + return throw new \Exception("Error of db query"); + } + + $d = $result -> fetch_assoc(); + return $d["COUNT(*)"]; + } + + public function create_group(int $user_id, String $title, String $type) :Group { + $group = new Group(); + + $group -> user_id = $user_id; + $group -> title = $title; + $group -> type = $type; + $group -> position = $this -> total_group_by_user($user_id) + 1; + $group -> update_at = get_formatted_timestamp(); + $group -> create_at = get_formatted_timestamp(); + + $group -> create_new(); + + return $group; } public function get_groups_by_user_id(int $user_id) :Array { + $tablename = Group::get_tablename(); + $sql = "SELECT * FROM `{$tablename}` WHERE `user_id`='{$user_id}' ORDER BY `position` ASC"; + $results = app() -> db() -> query($sql); + if(!$results) { + return throw new \Exception("Error of query to database"); + } + + $groups = []; + + while($row = $results -> fetch_assoc()) { + $groups[] = Group::instance_from_data($row); + } + + return $groups; + } + + 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(); + + list($selected_group) = array_values(array_filter($groups, function($group) use($group_id) { + return $group -> get_id() == $group_id; + })); + + $pos_min = min($selected_group -> position, $new_position); + $pos_max = max($selected_group -> position, $new_position); + $current_position = $selected_group->position; + + foreach($groups as $i => $group) { + if($group -> position < $pos_min or $group -> position > $pos_max) { + continue; + } + + if($group -> get_id() == $group_id) { + $group -> position = $new_position; + continue; + } + + if ($new_position > $current_position) { + if ($group->position > $current_position && $group->position <= $new_position) { + $group->position--; + } + } else { + if ($group->position < $current_position && $group->position >= $new_position) { + $group->position++; + } + } + } + + 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 4a5f06c..1e0f138 100755 --- a/server/App/routes_map.php +++ b/server/App/routes_map.php @@ -3,25 +3,49 @@ namespace App; use \App\Controller\AuthController; +use \App\Controller\GroupsController; function init_auth($router) { $router -> linking("GET", "signup", function(){ $auth_controller = new AuthController(); - $auth_controller -> signup(); + return $auth_controller -> signup(); }); $router -> linking("GET", "signin", function(){ $auth_controller = new AuthController(); - $auth_controller -> signin(); + return $auth_controller -> signin(); }); $router -> linking("GET", "signout", function(){ $auth_controller = new AuthController(); - $auth_controller -> signout(); + return $auth_controller -> signout(); + }); +} + +function init_groups($router) { + $router -> linking("GET", "groups-create", function(){ + $groups_controller = new GroupsController(); + return $groups_controller -> create(); + }); + + $router -> linking("GET", "groups-list", function(){ + $groups_controller = new GroupsController(); + return $groups_controller -> get_list(); + }); + + $router -> linking("GET", "groups-change-title", function(){ + $groups_controller = new GroupsController(); + return $groups_controller -> change_title(); + }); + + $router -> linking("GET", "groups-change-position", function(){ + $groups_controller = new GroupsController(); + return $groups_controller -> change_position(); }); } function init_routes_map($router) { init_auth($router); + init_groups($router); } \ No newline at end of file diff --git a/server/libs/DataContain.php b/server/libs/DataContain.php index f031d9a..829e26c 100755 --- a/server/libs/DataContain.php +++ b/server/libs/DataContain.php @@ -29,4 +29,14 @@ $this -> changed_fields[] = $field_name; } } + + public static function instance_from_data(Array $data) { + $instance = new static(); + $instance -> init_from_data($data); + return $instance; + } + + public function to_array() :Array { + return $this -> data; + } } \ No newline at end of file diff --git a/server/libs/DataStorage.php b/server/libs/DataStorage.php index 55f25da..c66e734 100755 --- a/server/libs/DataStorage.php +++ b/server/libs/DataStorage.php @@ -56,6 +56,10 @@ return true; } + if(in_array("update_at", $this -> data_fields)) { + $this -> update_at = get_formatted_timestamp(); + } + $fields = []; foreach($this -> changed_fields as $field_name) { @@ -73,6 +77,15 @@ return $this -> init_by_field("id", $id); } + public function init_from_data(Array $data) { + if(!isset($data["id"]) or !$data["id"]) { + return throw new \Exception("Invalid id."); + } + + $this -> data = $data; + $this -> id = $data["id"]; + } + public function remove() :bool { !$this -> id and throw new \Exception("Tried removing of uninizialized object"); diff --git a/server/libs/utils.php b/server/libs/utils.php index 0b9ebbc..666aea6 100755 --- a/server/libs/utils.php +++ b/server/libs/utils.php @@ -23,7 +23,7 @@ return htmlspecialchars(strip_tags(trim($var)), ENT_QUOTES, 'UTF-8'); } -function get_create_at_timestamp() :String { +function get_formatted_timestamp() :String { return date("Y-m-d H:i:s"); } @@ -49,6 +49,34 @@ ]; } +function force_error_response(String $class, String $errtext) { + die(response_json(error_response($class, $errtext))); +} +function get_current_session() { + $token = sanitize_input($_GET["token"]); + $session = new \App\Entity\Session(); + + if(!$session -> init_by_token($token)) { + force_error_response("Auth", "Invalid token"); + } + + return $session; +} + +function get_expected_vars(Array $vars_scope, Array $vars_list) :Array { + $result = []; + + foreach($vars_list as $var_name) { + if(!isset($vars_scope[$var_name])) { + $expected_vars = implode(", ", $vars_list); + force_error_response("Router", "Controller expects ({$expected_vars})"); + } + + $result[] = $vars_scope[$var_name]; + } + + return $result; +}