From 22a431da071066bb3412841b5c6b9aa49cb25a94 Mon Sep 17 00:00:00 2001 From: RundelhausCode Date: Mon, 19 Apr 2021 09:15:15 +0200 Subject: [PATCH] new database --- Backend/Controllers/Admin/AdminLogin.php | 14 ++-- Backend/Controllers/Admin/AdminSignup.php | 12 +-- .../Controllers/FileHandler/FileHandler.php | 22 +++--- .../Controllers/FileHandler/GetGameData.php | 19 +++-- .../Controllers/FileHandler/updateFiles.php | 61 +++++++++------- Backend/Controllers/FileHandler/upload.php | 73 +++++++++++-------- Backend/Controllers/GameJam/GetGameJam.php | 9 ++- Backend/Controllers/GameJam/NewGameJam.php | 20 ++--- Backend/Controllers/GameJam/UpdateGameJam.php | 21 +++--- Backend/Controllers/Group/GetGroup.php | 12 +-- Backend/Controllers/Group/Group.php | 20 ++--- Backend/Controllers/Group/Login.php | 61 ++++++++-------- Backend/Controllers/Group/ResetPassword.php | 20 ++--- Backend/Controllers/Group/SigningUp.php | 23 +++--- Backend/Controllers/Group/UpdateGruop.php | 25 +++---- Backend/Controllers/Group/UpdatePassword.php | 20 ++--- Backend/Controllers/Polls/1vote.php | 19 ++--- Backend/Controllers/Polls/321vote.php | 10 +-- Backend/Controllers/Polls/GetVote.php | 8 +- Backend/Controllers/Polls/GetWinner.php | 14 ++-- Backend/Controllers/Polls/Vote.php | 9 ++- .../Registration/GetRegistration.php | 1 + .../Registration/NewRegistration.php | 1 + .../Registration/UpdateRegistration.php | 1 + Backend/Controllers/keyWord/GetKeyWinner.php | 19 +++-- Backend/Controllers/keyWord/NewKeyWord.php | 33 ++++++--- Backend/Database/AdminUser.php | 5 +- Backend/Database/GameData.php | 5 +- Backend/Database/GameJam.php | 5 +- Backend/Database/Group.php | 8 +- Backend/Database/KeyWord.php | 7 +- Backend/Database/Password.php | 5 +- Backend/Database/Registration.php | 17 +++++ Backend/Database/Vote.php | 7 +- Backend/Database/databaseMigration.php | 1 + Backend/Models/AdminUser.php | 3 +- Backend/Models/GameData.php | 14 +++- Backend/Models/GameJam.php | 16 ++-- Backend/Models/Group.php | 22 +++--- Backend/Models/KeyWord.php | 13 +++- Backend/Models/Password.php | 11 ++- Backend/Models/Registration.php | 35 +++++++++ Backend/Models/Vote.php | 4 +- 43 files changed, 423 insertions(+), 302 deletions(-) create mode 100644 Backend/Controllers/Registration/GetRegistration.php create mode 100644 Backend/Controllers/Registration/NewRegistration.php create mode 100644 Backend/Controllers/Registration/UpdateRegistration.php create mode 100644 Backend/Database/Registration.php create mode 100644 Backend/Models/Registration.php diff --git a/Backend/Controllers/Admin/AdminLogin.php b/Backend/Controllers/Admin/AdminLogin.php index f7ec881..af3c392 100644 --- a/Backend/Controllers/Admin/AdminLogin.php +++ b/Backend/Controllers/Admin/AdminLogin.php @@ -7,31 +7,31 @@ use Backend\Models\AdminUser; //Start the php session session_start(); -if(isset($_POST['aLogin'])){ +if (isset($_POST['aLogin'])) { $userName = $_POST["userName"]; $password = $_POST["password"]; - $user = AdminUser::firstWhere('user_name', $userName ); - if($user){ + $user = AdminUser::firstWhere('user_name', $userName); + if ($user) { $hashedPassword = $user->password; - if(password_verify($password, $hashedPassword )){ + if (password_verify($password, $hashedPassword)) { $_SESSION['userName'] = $userName; $_SESSION['admin'] = true; $_SESSION['success'] = "You are now logged in"; http_response_code(200); makeAdminLogin($userName); - }else{ + } else { session_destroy(); http_response_code(401); echo json_encode(["message" => "wrong password"]); } - }else{ + } else { session_destroy(); http_response_code(401); echo json_encode(["message" => "admin don't exist"]); } -}else{ +} else { http_response_code(400); } \ No newline at end of file diff --git a/Backend/Controllers/Admin/AdminSignup.php b/Backend/Controllers/Admin/AdminSignup.php index f63eab9..efb4f5b 100644 --- a/Backend/Controllers/Admin/AdminSignup.php +++ b/Backend/Controllers/Admin/AdminSignup.php @@ -4,20 +4,20 @@ require_once "Admin.php"; use Backend\Models\AdminUser; -if (isAdmin()){ - if (isset($_POST["newAdmin"])){ +if (isAdmin()) { + if (isset($_POST["newAdmin"])) { $admin = new AdminUser(); $admin->user_name = $_POST["newUsername"]; $admin->password = $_POST["newPassword"]; - if ($admin->save()){ + if ($admin->save()) { http_response_code(201); - }else{ + } else { http_response_code(500); } - }else{ + } else { http_response_code(400); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "is not admin"]); } \ No newline at end of file diff --git a/Backend/Controllers/FileHandler/FileHandler.php b/Backend/Controllers/FileHandler/FileHandler.php index e31cb14..ce424a3 100644 --- a/Backend/Controllers/FileHandler/FileHandler.php +++ b/Backend/Controllers/FileHandler/FileHandler.php @@ -13,15 +13,15 @@ function ZipFileHandler(string $gameFileName, string $gameFileTmp): string $headerType = mime_content_type($gameFileTmp); $headerTypeMatch = array('application/zip'); - if(in_array($headerType,$headerTypeMatch)){ - $gameFileNewName = uniqid("", true). "." . $fileActualExtGame; - if(empty($gameFileName)){ + if (in_array($headerType, $headerTypeMatch)) { + $gameFileNewName = uniqid("", true) . "." . $fileActualExtGame; + if (empty($gameFileName)) { http_response_code(400); exit(); } - rename($gameFileTmp,"../../Games/".$gameFileNewName); + rename($gameFileTmp, "../../Games/" . $gameFileNewName); return $gameFileNewName; - }else{ + } else { http_response_code(400); echo json_encode(["message" => "Wrong file type gameFile"]); exit(); @@ -39,17 +39,17 @@ function imagesFileHandler(string $thumbnailFileName, string $thumbnailFileTmp): $fileActualExtThumb = strtolower(end($fileExtThumb)); $headerType = mime_content_type($thumbnailFileTmp); - $headerTypeMatch = array('image/png', 'image/jpeg','image/gif', 'image/svg+xml',); + $headerTypeMatch = array('image/png', 'image/jpeg', 'image/gif', 'image/svg+xml',); - if(in_array($headerType,$headerTypeMatch)){ - $thumbnailFileNewName = uniqid("", true). "." . $fileActualExtThumb; - if(empty($thumbnailFileName)){ + if (in_array($headerType, $headerTypeMatch)) { + $thumbnailFileNewName = uniqid("", true) . "." . $fileActualExtThumb; + if (empty($thumbnailFileName)) { http_response_code(400); exit(); } - rename($thumbnailFileTmp,"../../../Frontend/images/".$thumbnailFileNewName); + rename($thumbnailFileTmp, "../../../Frontend/images/" . $thumbnailFileNewName); return $thumbnailFileNewName; - }else{ + } else { http_response_code(400); echo json_encode(["message" => "Wrong file type thumbnailFile"]); exit(); diff --git a/Backend/Controllers/FileHandler/GetGameData.php b/Backend/Controllers/FileHandler/GetGameData.php index b58f35c..e9c2f3d 100644 --- a/Backend/Controllers/FileHandler/GetGameData.php +++ b/Backend/Controllers/FileHandler/GetGameData.php @@ -1,16 +1,21 @@ GameData(); -}elseif (isset($_GET['gameJamId'])){ - $gameDataIds = Group::where("game_jam_id",$_GET['gameJamId'])->pluck("game_data_id")->toArray(); +if (isset($_GET['gameDataId'])) { + $openGameDataStream = GameData::find($_GET['gameDataId']); +} elseif (isset($_GET['registrationId'])) { + $openGameDataStream = Registration::find($_GET['registrationId']); +} elseif (isset($_GET['groupId'])) { + $registrationIds = Registration::where("group_id", $_GET['groupId'])->pluck("game_data_id")->toArray(); + $openGameDataStream = GameData::whereIn("id", $registrationIds)->get(); +} elseif (isset($_GET['gameJamId'])) { + $gameDataIds = Group::where("game_jam_id", $_GET['gameJamId'])->pluck("game_data_id")->toArray(); $openGameDataStream = GameData::whereIn("id", $gameDataIds)->get(); -}else{ +} else { $openGameDataStream = GameData::all(); } diff --git a/Backend/Controllers/FileHandler/updateFiles.php b/Backend/Controllers/FileHandler/updateFiles.php index b903190..1ee2f62 100644 --- a/Backend/Controllers/FileHandler/updateFiles.php +++ b/Backend/Controllers/FileHandler/updateFiles.php @@ -2,15 +2,31 @@ require_once "../../../bootstrap.php"; require_once('../Group/Group.php'); require_once('FileHandler.php'); -use Backend\Models\GameData; -if(isLogin()){ - if(isset($_POST['submitUpdate'])){ +use Backend\Models\GameData; +use Backend\Models\Registration; + +if (isLogin()) { + if (isset($_POST['submitUpdate'])) { + //Get the game data + $gameData = GameData::find($_POST['gameDataId']); + if ($gameData === null) { + http_response_code(400); + echo json_encode(["message" => "game data not found"]); + exit(); + } + //Get the group + $group = groupViaToken($_SESSION['token']); + if (!in_array($gameData->id, Registration::where("group_id", $group->id)->pluck("game_data_id")->toArray())) { + http_response_code(401); + exit(); + } + $gameFile = $_FILES['gameFile']; $desc = $_POST['description']; $title = $_POST['gameTitle']; $thumbnail = $_FILES['thumbnailFile']; - $isWebBased = isset( $_POST['isWebBased']); + $isWebBased = isset($_POST['isWebBased']); $gameFileName = $_FILES['gameFile']['name'];//Game name $gameFileTmp = $_FILES['gameFile']['tmp_name'];//Tmp location of the file @@ -18,34 +34,29 @@ if(isLogin()){ $thumbnailFileName = $_FILES['thumbnailFile']['name'];//Game name $thumbnailFileTmp = $_FILES['thumbnailFile']['tmp_name'];//Tmp location of the file - $thumbnailFileError =$_FILES['thumbnailFile']['error'];//File error + $thumbnailFileError = $_FILES['thumbnailFile']['error'];//File error - - //Get the group - $group = groupViaToken($_SESSION['token']); - //Get the game data, from the group - $gameData = GameData::find($group->game_data_id); $gameData->game_name = $title; - if(isset($gameData) && $gameFileError === 0){ - unlink("../../Games/".$gameData->game_link); - $gameData->game_link = ZipFileHandler($gameFileName,$gameFileTmp); + if (isset($gameData) && $gameFileError === 0) { + unlink("../../Games/" . $gameData->game_link); + $gameData->game_link = ZipFileHandler($gameFileName, $gameFileTmp); } $gameData->description = $desc; - if(isset($thumbnail) && $thumbnailFileError === 0){ - unlink("../../../Frontend/images/".$gameData->img); - $gameData->img = imagesFileHandler($thumbnailFileName,$thumbnailFileTmp); + if (isset($thumbnail) && $thumbnailFileError === 0) { + unlink("../../../Frontend/images/" . $gameData->img); + $gameData->img = imagesFileHandler($thumbnailFileName, $thumbnailFileTmp); } $gameData->is_web_Based = $isWebBased; - if(!$gameData->save()){ - http_response_code(500); - }else{ - http_response_code(201); + if (!$gameData->save()) { + http_response_code(500); + } else { + http_response_code(201); } - }else{ - http_response_code(400); + } else { + http_response_code(400); } -}else{ - http_response_code(401); - echo json_encode(["message" => "is not login"]); +} else { + http_response_code(401); + echo json_encode(["message" => "is not login"]); } \ No newline at end of file diff --git a/Backend/Controllers/FileHandler/upload.php b/Backend/Controllers/FileHandler/upload.php index c6cad0c..d95e075 100644 --- a/Backend/Controllers/FileHandler/upload.php +++ b/Backend/Controllers/FileHandler/upload.php @@ -2,8 +2,10 @@ require_once "../../../bootstrap.php"; require_once('../Group/Group.php'); require_once('FileHandler.php'); + use Backend\Models\GameData; use Backend\Models\GameJam; +use Backend\Models\Registration; use Backend\Models\Group; @@ -12,26 +14,37 @@ date_default_timezone_set("Europe/Copenhagen"); $isImages = false; -if(isLogin()){ +if (isLogin()) { + if (isset($_POST['submitUpload'])) { + $group = groupViaToken($_SESSION['token']); + $gameJam = GameJam::find($_POST['gameJamId']); + if ($gameJam === null) { + http_response_code(400); + echo json_encode(["message" => "gameJam not found"]); + exit(); + } + $registration = Registration::where('game_jam_id', $gameJam->id)->where("group_id", $group->id)->frist(); + if ($registration === null) { + http_response_code(401); + echo json_encode(["message" => "group not registered for that game jam"]); + exit(); + } - $group = groupViaToken($_SESSION['token']); - $gameJam = GameJam::find($group->game_jam_id); - $gameJamStartTime = strtotime($gameJam->start_time); - $gameJamEndTime = strtotime($gameJam->end_time); - $date = date('Y/m/d H:i:s', time()); + $gameJamStartTime = strtotime($gameJam->start_time); + $gameJamEndTime = strtotime($gameJam->end_time); + $date = date('Y/m/d H:i:s', time()); - $currentTime = strtotime($date); + $currentTime = strtotime($date); - if($gameJamStartTime <= $currentTime && $gameJamEndTime >= $currentTime){ - if(!isset($group->game_data_id)){ - if(isset($_POST['submitUpload'])){ + if ($gameJamStartTime <= $currentTime && $gameJamEndTime >= $currentTime) { + if (!isset($registration->game_data_id)) { //Get the data from the user form $gameFile = $_FILES['gameFile']; $desc = $_POST['description']; $title = $_POST['gameTitle']; $thumbnail = $_FILES['thumbnailFile']; - $isWebBased = isset( $_POST['isWebBased']); + $isWebBased = isset($_POST['isWebBased']); $gameFileName = $_FILES['gameFile']['name'];//Game name $gameFileTmp = $_FILES['gameFile']['tmp_name'];//Tmp location of the file @@ -39,40 +52,40 @@ if(isLogin()){ $thumbnailFileName = $_FILES['thumbnailFile']['name'];//Game name $thumbnailFileTmp = $_FILES['thumbnailFile']['tmp_name'];//Tmp location of the file - $thumbnailFileError =$_FILES['thumbnailFile']['error'];//File error + $thumbnailFileError = $_FILES['thumbnailFile']['error'];//File error - if($gameFileError === 0){ + if ($gameFileError === 0) { $gameData = new GameData(); $gameData->game_name = $title; - $gameData->game_link = ZipFileHandler($gameFileName,$gameFileTmp); + $gameData->game_link = ZipFileHandler($gameFileName, $gameFileTmp); $gameData->description = $desc; - if(isset($thumbnail) && $thumbnailFileError === 0){ - $gameData->img = imagesFileHandler($thumbnailFileName,$thumbnailFileTmp); + if (isset($thumbnail) && $thumbnailFileError === 0) { + $gameData->img = imagesFileHandler($thumbnailFileName, $thumbnailFileTmp); } $gameData->is_web_Based = $isWebBased; $gameData->save(); - $group->gameData()->associate($gameData); - if(! $group->save()){ - http_response_code(500); - }else{ - http_response_code(201); - } + $registration->gameData()->associate($gameData); + if (!$registration->save()) { + http_response_code(500); + } else { + http_response_code(201); + } - }else{ - http_response_code(500); + } else { + http_response_code(500); } - }else{ + } else { http_response_code(400); + echo json_encode(["message" => "file already uploaded "]); } - }else{ + } else { http_response_code(400); - echo json_encode(["message" => "Can only upload one file"]); + echo json_encode(["message" => "Can only upload when the game jam has started"]); } - }else{ + } else { http_response_code(400); - echo json_encode(["message" => "Can only upload when the game jam has started"]); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "is not login"]); } \ No newline at end of file diff --git a/Backend/Controllers/GameJam/GetGameJam.php b/Backend/Controllers/GameJam/GetGameJam.php index 6746672..b7453a3 100644 --- a/Backend/Controllers/GameJam/GetGameJam.php +++ b/Backend/Controllers/GameJam/GetGameJam.php @@ -4,13 +4,14 @@ header("Access-Control-Allow-Credentials: true"); header("Access-Control-Max-Age: 1000"); header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding"); header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE"); -require_once (realpath(dirname(__FILE__) ."/../../../bootstrap.php")); +require_once(realpath(dirname(__FILE__) . "/../../../bootstrap.php")); + use Backend\Models\GameJam; -if(isset($_GET['gameJamId'])){ - $dbValue = GameJam::find($_GET['gameJamId']); -}else{ +if (isset($_GET['gameJamId'])) { + $dbValue = GameJam::find($_GET['gameJamId']); +} else { $dbValue = GameJam::all(); } diff --git a/Backend/Controllers/GameJam/NewGameJam.php b/Backend/Controllers/GameJam/NewGameJam.php index f5bdfab..ff34686 100644 --- a/Backend/Controllers/GameJam/NewGameJam.php +++ b/Backend/Controllers/GameJam/NewGameJam.php @@ -1,34 +1,34 @@ name = $_POST["gameJamName"]; - $gameJam->start_time = $_POST["startDate"]."T".$_POST["startTime"]; - $gameJam->end_time = $_POST["endDate"]."T".$_POST["endTime"]; + $gameJam->start_time = $_POST["startDate"] . "T" . $_POST["startTime"]; + $gameJam->end_time = $_POST["endDate"] . "T" . $_POST["endTime"]; if (!empty($_POST['keyWord'])) { $gameJam->key_word = $_POST['keyWord']; } $gameJam->description = $_POST["description"]; - if($gameJam->save()){ + if ($gameJam->save()) { http_response_code(201); - } - else{ + } else { http_response_code(500); } - }else{ + } else { http_response_code(400); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "is not admin"]); } \ No newline at end of file diff --git a/Backend/Controllers/GameJam/UpdateGameJam.php b/Backend/Controllers/GameJam/UpdateGameJam.php index 6fb62ce..643d0cd 100644 --- a/Backend/Controllers/GameJam/UpdateGameJam.php +++ b/Backend/Controllers/GameJam/UpdateGameJam.php @@ -1,39 +1,40 @@ name = $_POST['gameJamName']; - $gameJam->start_time = $_POST["startDate"]."T".$_POST["startTime"]; + $gameJam->start_time = $_POST["startDate"] . "T" . $_POST["startTime"]; - $gameJam->end_time = $_POST["endDate"]."T".$_POST["endTime"]; + $gameJam->end_time = $_POST["endDate"] . "T" . $_POST["endTime"]; if (!empty($_POST['keyWord'])) { $gameJam->key_word = $_POST['keyWord']; - }else{ + } else { $gameJam->key_word = null; } $gameJam->description = $_POST['description']; if ($gameJam->save()) { http_response_code(201); - }else{ + } else { http_response_code(500); } - }else{ + } else { http_response_code(400); echo json_encode(["message" => "game jam not found"]); } - }else{ + } else { http_response_code(400); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "is not admin"]); } diff --git a/Backend/Controllers/Group/GetGroup.php b/Backend/Controllers/Group/GetGroup.php index e4dee59..1f5ae47 100644 --- a/Backend/Controllers/Group/GetGroup.php +++ b/Backend/Controllers/Group/GetGroup.php @@ -1,14 +1,14 @@ get(); -} -else{ +} elseif (isset($_GET["gameJameId"])) { + $registrationIds = Registration::where("game_jam_id", $_GET["gameJameId"])->pluck("game_data_id")->toArray(); + $groups = Group::whereIn("id", $registrationIds)->get(); +} else { $groups = Group::all(); } diff --git a/Backend/Controllers/Group/Group.php b/Backend/Controllers/Group/Group.php index 2519cdf..ba1cfed 100644 --- a/Backend/Controllers/Group/Group.php +++ b/Backend/Controllers/Group/Group.php @@ -1,8 +1,9 @@ group_id); } return null; @@ -34,15 +35,14 @@ function isLogin(): bool */ function passwordValidate(string $password1): bool { - $uppercase = preg_match('@[A-Z]@', $password1); - $lowercase = preg_match('@[a-z]@', $password1); - $number = preg_match('@[0-9]@', $password1); - $specialChars = preg_match('@[^\w]@', $password1); + $uppercase = preg_match('@[A-Z]@', $password1); + $lowercase = preg_match('@[a-z]@', $password1); + $number = preg_match('@[0-9]@', $password1); + $specialChars = preg_match('@[^\w]@', $password1); - if(!$uppercase || !$lowercase || !$number || !$specialChars || (strlen($password1) >= 8 && strlen($password1) <= 255) ) { - return true; - } - else return false; + if (!$uppercase || !$lowercase || !$number || !$specialChars || (strlen($password1) >= 8 && strlen($password1) <= 255)) { + return true; + } else return false; } function makeLogin(string $groupName, int $groupId) diff --git a/Backend/Controllers/Group/Login.php b/Backend/Controllers/Group/Login.php index f78693d..1068587 100644 --- a/Backend/Controllers/Group/Login.php +++ b/Backend/Controllers/Group/Login.php @@ -9,44 +9,43 @@ use Illuminate\Support\Str; //Start the php session -if(isset($_POST['login'])){ +if (isset($_POST['login'])) { $groupName = $_POST["groupName"]; $password = $_POST["password"]; - $group = Group::firstWhere('group_name', $groupName ); - if($group){ - $hashedPassword = $group->password->password; - if(password_verify($password, $hashedPassword )){ - $token = Str::random(100); - $groupPassword = Password::firstWhere('group_id', $group->id); - $groupPassword->remember_token = $token; - if($groupPassword->save()){ - $_SESSION['token'] = $token; - $_SESSION['success'] = "You are now logged in"; - makeLogin($groupName,$group->id); - //header('location: ../../../Frontend/index.php?login=success'); - //exit(); - http_response_code(200); - } - else{ - session_destroy(); - http_response_code(500); - } + $group = Group::firstWhere('group_name', $groupName); + if ($group) { + $hashedPassword = $group->password->password; + if (password_verify($password, $hashedPassword)) { + $token = Str::random(100); + $groupPassword = Password::firstWhere('group_id', $group->id); + $groupPassword->remember_token = $token; + if ($groupPassword->save()) { + $_SESSION['token'] = $token; + $_SESSION['success'] = "You are now logged in"; + makeLogin($groupName, $group->id); + //header('location: ../../../Frontend/index.php?login=success'); + //exit(); + http_response_code(200); + } else { + session_destroy(); + http_response_code(500); + } - }else{ - session_destroy(); - http_response_code(401); - echo json_encode(["message" => "Wrong password"]); - } + } else { + session_destroy(); + http_response_code(401); + echo json_encode(["message" => "Wrong password"]); + } - }else{ - session_destroy(); - http_response_code(401); - echo json_encode(["message" => "group was not found"]); - } + } else { + session_destroy(); + http_response_code(401); + echo json_encode(["message" => "group was not found"]); + } -}else{ +} else { http_response_code(400); } diff --git a/Backend/Controllers/Group/ResetPassword.php b/Backend/Controllers/Group/ResetPassword.php index 3fa3e8e..21a2647 100644 --- a/Backend/Controllers/Group/ResetPassword.php +++ b/Backend/Controllers/Group/ResetPassword.php @@ -5,26 +5,26 @@ require_once "../Admin/Admin.php"; use \Backend\Models\Password; //session_start(); -if(isAdmin()){ - if(isset($_POST['restPassword'])){ - $password = Password::firstWhere("group_id",$_POST['groupId']); - if($password){ - $password->password = password_hash($_POST['newPassword'], PASSWORD_DEFAULT); +if (isAdmin()) { + if (isset($_POST['restPassword'])) { + $password = Password::firstWhere("group_id", $_POST['groupId']); + if ($password) { + $password->password = password_hash($_POST['newPassword'], PASSWORD_DEFAULT); echo $password; - if(!$password->save()){ + if (!$password->save()) { http_response_code(500); - }else{ + } else { http_response_code(201); } - }else{ + } else { http_response_code(400); echo json_encode(["message" => "group not found"]); } - }else{ + } else { http_response_code(400); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "is not admin"]); } \ No newline at end of file diff --git a/Backend/Controllers/Group/SigningUp.php b/Backend/Controllers/Group/SigningUp.php index 40101bc..0e6a8a1 100644 --- a/Backend/Controllers/Group/SigningUp.php +++ b/Backend/Controllers/Group/SigningUp.php @@ -11,34 +11,31 @@ use Illuminate\Support\Str; $groupName = ""; $errors = array(); -if(isset($_POST['regGroup'])){ +if (isset($_POST['regGroup'])) { - if(passwordValidate($pass = $_POST['password'])){ + if (passwordValidate($pass = $_POST['password'])) { $group = new Group(); - $group->gameJam()->associate(GameJam::find($_POST['gameJamId'])); - $group->group_name = $groupName = $_POST['groupName']; - $group->group_amount = $_POST['groupAmount']; - - if(!$group->save()){ + if (!$group->save()) { http_response_code(500); exit(); } - $password = New Password(); + $password = new Password(); $password->group()->associate($group); - $password->password = password_hash($pass ,PASSWORD_DEFAULT); + $password->password = password_hash($pass, PASSWORD_DEFAULT); $token = Str::random(100); $password->remember_token = $token; - if(!$password->save()){ + if (!$password->save()) { + $group->delete(); http_response_code(500); exit(); } @@ -46,14 +43,14 @@ if(isset($_POST['regGroup'])){ $_SESSION['groupName'] = $groupName; $_SESSION['token'] = $token; $_SESSION['success'] = "You are now logged in"; - makeLogin($groupName,$group->id); + makeLogin($groupName, $group->id); http_response_code(201); echo json_encode(["message" => "you are login"]); - }else{ + } else { http_response_code(400); echo json_encode(["message" => "password not valid"]); } -}else{ +} else { http_response_code(400); } diff --git a/Backend/Controllers/Group/UpdateGruop.php b/Backend/Controllers/Group/UpdateGruop.php index 0c2dc5f..20887a5 100644 --- a/Backend/Controllers/Group/UpdateGruop.php +++ b/Backend/Controllers/Group/UpdateGruop.php @@ -4,26 +4,23 @@ require_once "Group.php"; use Backend\Models\Group; -if(isLogin()){ +if (isLogin()) { if (isset($_POST['updateGroup'])) { - if($group = groupViaToken($_SESSION['token'])){ - $group->group_name = $_POST['groupName']; - $group->group_amount = $_POST['groupAmount']; - $group->game_jam_id = $_POST['gameJamId']; - if(!$group->save()){ - http_response_code(500); - }else{ - http_response_code(201); - } - } - else{ + if ($group = groupViaToken($_SESSION['token'])) { + $group->group_name = $_POST['groupName']; + if (!$group->save()) { + http_response_code(500); + } else { + http_response_code(201); + } + } else { http_response_code(400); echo json_encode(["message" => "group not found"]); } - }else{ + } else { http_response_code(400); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "is not login"]); } diff --git a/Backend/Controllers/Group/UpdatePassword.php b/Backend/Controllers/Group/UpdatePassword.php index ea686f2..decd555 100644 --- a/Backend/Controllers/Group/UpdatePassword.php +++ b/Backend/Controllers/Group/UpdatePassword.php @@ -1,28 +1,28 @@ id); - if(passwordValidate($pass = $_POST['password'])){ - $password->password = password_hash($pass,PASSWORD_DEFAULT); - if(!$password->save()){ + if (passwordValidate($pass = $_POST['password'])) { + $password->password = password_hash($pass, PASSWORD_DEFAULT); + if (!$password->save()) { http_response_code(500); - }else{ + } else { http_response_code(201); } - } - else{ + } else { http_response_code(400); echo json_encode(["message" => "password not valid"]); } - }else{ + } else { http_response_code(400); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "is not login"]); } diff --git a/Backend/Controllers/Polls/1vote.php b/Backend/Controllers/Polls/1vote.php index f718230..618fbf8 100644 --- a/Backend/Controllers/Polls/1vote.php +++ b/Backend/Controllers/Polls/1vote.php @@ -1,30 +1,31 @@ group()->associate(Group::find($_POST['groupId'])); + $vote->gameDate()->associate(GameData::find($_POST['gameDataId'])); $vote->points += 1; - if(!empty($_POST['comment'])){ + if (!empty($_POST['comment'])) { $vote->comment = $_POST['comment']; } - if(!$vote->save()){ + if (!$vote->save()) { http_response_code(500); - }else{ + } else { http_response_code(201); } - } else{ + } else { http_response_code(403); echo json_encode(["message" => "you have already voted"]); } -}else{ +} else { http_response_code(400); echo json_encode(["message" => "you have already voted"]); } diff --git a/Backend/Controllers/Polls/321vote.php b/Backend/Controllers/Polls/321vote.php index 2a5e9b2..c558aa4 100644 --- a/Backend/Controllers/Polls/321vote.php +++ b/Backend/Controllers/Polls/321vote.php @@ -2,17 +2,18 @@ use Backend\Models\Group; use Backend\Models\Vote; + require_once "../../../bootstrap.php"; -if(isset($_POST['321Vote'])){ +if (isset($_POST['321Vote'])) { require "Vote.php"; //give 1 point $vote = new Vote(); $vote->group()->associate(Group::find($_POST['1pGroupId'])); $vote->points += 1; - if(!empty($_POST['1pComment'])){ + if (!empty($_POST['1pComment'])) { $vote->comment = $_POST['1pComment']; } $vote->save(); @@ -20,7 +21,7 @@ if(isset($_POST['321Vote'])){ $vote = new Vote(); $vote->group()->associate(Group::find($_POST['2pGroupId'])); $vote->points += 2; - if(!empty($_POST['2pComment'])){ + if (!empty($_POST['2pComment'])) { $vote->comment = $_POST['2pComment']; } $vote->save(); @@ -28,7 +29,7 @@ if(isset($_POST['321Vote'])){ $vote = new Vote(); $vote->group()->associate(Group::find($_POST['3pGroupId'])); $vote->points += 3; - if(!empty($_POST['3pComment'])){ + if (!empty($_POST['3pComment'])) { $vote->comment = $_POST['3pComment']; } $vote->save(); @@ -36,5 +37,4 @@ if(isset($_POST['321Vote'])){ exit(); - } diff --git a/Backend/Controllers/Polls/GetVote.php b/Backend/Controllers/Polls/GetVote.php index f44593f..14e1497 100644 --- a/Backend/Controllers/Polls/GetVote.php +++ b/Backend/Controllers/Polls/GetVote.php @@ -1,13 +1,15 @@ Vote::where('group_id',groupViaToken($_SESSION['token'])->id)->get()->jsonSerialize())); + echo json_encode(array('data' => Vote::where('group_id', groupViaToken($_SESSION['token'])->id)->get()->jsonSerialize())); -}else{ +} else { http_response_code(401); echo json_encode(["message" => "not login"]); } diff --git a/Backend/Controllers/Polls/GetWinner.php b/Backend/Controllers/Polls/GetWinner.php index 28f597d..c702b95 100644 --- a/Backend/Controllers/Polls/GetWinner.php +++ b/Backend/Controllers/Polls/GetWinner.php @@ -1,14 +1,15 @@ get(); - if($groups) { + if ($groups) { $winningGroups = new Collection(); $i = -1; foreach ($groups as $group) { @@ -27,16 +28,15 @@ if(isAdmin()){ header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept"); header('Content-Type: application/json;charset=UTF-8'); echo json_encode(array('data' => $winningGroups->jsonSerialize())); - } - else{ + } else { http_response_code(400); echo json_encode(["message" => "game jam not found"]); } - }else{ + } else { http_response_code(400); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "not admin"]); } diff --git a/Backend/Controllers/Polls/Vote.php b/Backend/Controllers/Polls/Vote.php index eb19086..4d68e14 100644 --- a/Backend/Controllers/Polls/Vote.php +++ b/Backend/Controllers/Polls/Vote.php @@ -1,5 +1,6 @@ id)->pluck("id")->toArray(); - $find_all_keywords = KeyWord::whereIn("group_id", $all_group_in_game_jam_id)->inRandomOrder()->take(6)->get(); + $all_registration_in_game_jam_id = Registration::where("game_jam_id", $game_jam->id)->pluck("id")->toArray(); + $find_all_keywords = KeyWord::whereIn("group_id", $all_registration_in_game_jam_id)->inRandomOrder()->take(6)->get(); $game_jam->key_word = $find_all_keywords->first()->key_word; - if(!$game_jam->save()){ + if (!$game_jam->save()) { http_response_code(500); - }else{ + } else { http_response_code(201); } @@ -24,10 +23,10 @@ if (!isAdmin()){ header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept"); header('Content-Type: application/json;charset=UTF-8'); echo json_encode(array('data' => $find_all_keywords->jsonSerialize())); - }else{ + } else { http_response_code(400); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "is not admin"]); } \ No newline at end of file diff --git a/Backend/Controllers/keyWord/NewKeyWord.php b/Backend/Controllers/keyWord/NewKeyWord.php index 26aeb7f..f58e313 100644 --- a/Backend/Controllers/keyWord/NewKeyWord.php +++ b/Backend/Controllers/keyWord/NewKeyWord.php @@ -1,18 +1,33 @@ "gameJam not found"]); + exit(); + } + $registration = Registration::where('game_jam_id', $gameJam->id)->where("group_id", $group->id)->frist(); + if ($registration === null) { + http_response_code(401); + echo json_encode(["message" => "group not registered for that game jam"]); + exit(); + } + //if statement to find out if the group have use all their keyWords - if(KeyWord::where('group_id', $group->id)->count()<$group->group_amount){ + if (KeyWord::where('registration_id', $registration->id)->count() < $registration->group_amount) { //Make a new keyword[Only to be used if you need to make a new of something] $keyword = new KeyWord(); @@ -21,23 +36,23 @@ if (isLogin()) { $keyword->key_word = $_POST['key_word']; //Make foreign key from the groups table to the keyWords table - $keyword->group()->associate($group); + $keyword->registration()->associate($registration); //Try to save it - if(!$keyword->save()){ + if (!$keyword->save()) { http_response_code(500); - }else{ + } else { http_response_code(201); } - }else{ + } else { http_response_code(400); echo json_encode(["message" => "limited upload reached"]); } - }else{ + } else { http_response_code(400); } -}else{ +} else { http_response_code(401); echo json_encode(["message" => "not login"]); } diff --git a/Backend/Database/AdminUser.php b/Backend/Database/AdminUser.php index d82351a..2e80130 100644 --- a/Backend/Database/AdminUser.php +++ b/Backend/Database/AdminUser.php @@ -1,11 +1,12 @@ create("admin_users", function (Blueprint $table){ +Capsule::schema()->create("admin_users", function (Blueprint $table) { $table->id(); $table->string("user_name"); $table->string("password"); diff --git a/Backend/Database/GameData.php b/Backend/Database/GameData.php index 8fadeb0..2c05c5e 100644 --- a/Backend/Database/GameData.php +++ b/Backend/Database/GameData.php @@ -1,12 +1,13 @@ create("game_data", function (Blueprint $table){ +Capsule::schema()->create("game_data", function (Blueprint $table) { $table->id(); $table->string('game_name'); $table->string("game_link"); diff --git a/Backend/Database/GameJam.php b/Backend/Database/GameJam.php index fc8a468..7baa80d 100644 --- a/Backend/Database/GameJam.php +++ b/Backend/Database/GameJam.php @@ -1,12 +1,13 @@ create("game_jams", function (Blueprint $table){ +Capsule::schema()->create("game_jams", function (Blueprint $table) { $table->id(); $table->string("name"); $table->dateTime("start_time"); diff --git a/Backend/Database/Group.php b/Backend/Database/Group.php index a2feb6a..018c518 100644 --- a/Backend/Database/Group.php +++ b/Backend/Database/Group.php @@ -1,17 +1,15 @@ create("groups", function (Blueprint $table){ +Capsule::schema()->create("groups", function (Blueprint $table) { $table->id(); $table->string("group_name"); - $table->integer("group_amount"); - $table->foreignId("game_jam_id")->constrained("game_jams"); - $table->foreignId("game_data_id")->nullable()->constrained("game_data"); $table->timestamps(); }); diff --git a/Backend/Database/KeyWord.php b/Backend/Database/KeyWord.php index b48be5a..e341c76 100644 --- a/Backend/Database/KeyWord.php +++ b/Backend/Database/KeyWord.php @@ -1,15 +1,16 @@ create("key_words", function (Blueprint $table){ +Capsule::schema()->create("key_words", function (Blueprint $table) { $table->id(); $table->string('key_word'); - $table->foreignId("group_id")->constrained("groups"); + $table->foreignId("registrations_id")->constrained("registrations"); $table->timestamps(); }); diff --git a/Backend/Database/Password.php b/Backend/Database/Password.php index 8835c50..b13af21 100644 --- a/Backend/Database/Password.php +++ b/Backend/Database/Password.php @@ -1,12 +1,13 @@ create("passwords", function (Blueprint $table){ +Capsule::schema()->create("passwords", function (Blueprint $table) { $table->id(); $table->foreignId("group_id")->constrained("groups"); $table->string('password'); diff --git a/Backend/Database/Registration.php b/Backend/Database/Registration.php new file mode 100644 index 0000000..d62d5b0 --- /dev/null +++ b/Backend/Database/Registration.php @@ -0,0 +1,17 @@ +create("registrations", function (Blueprint $table) { + $table->id(); + $table->foreignId("group_id")->constrained("groups"); + $table->integer("group_amount"); + $table->foreignId("game_jam_id")->constrained("game_jams"); + $table->foreignId("game_data_id")->unique()->nullable()->constrained("game_data"); + $table->timestamps(); +}); \ No newline at end of file diff --git a/Backend/Database/Vote.php b/Backend/Database/Vote.php index 246b098..bdc259e 100644 --- a/Backend/Database/Vote.php +++ b/Backend/Database/Vote.php @@ -1,14 +1,15 @@ create("votes", function (Blueprint $table){ +Capsule::schema()->create("votes", function (Blueprint $table) { $table->id(); - $table->foreignId("group_id")->constrained("groups"); $table->tinyInteger("points"); $table->text("comment")->nullable(); + $table->foreignId("game_data_id")->constrained("game_data"); $table->timestamps(); }); \ No newline at end of file diff --git a/Backend/Database/databaseMigration.php b/Backend/Database/databaseMigration.php index 55cff39..249ab2e 100644 --- a/Backend/Database/databaseMigration.php +++ b/Backend/Database/databaseMigration.php @@ -17,6 +17,7 @@ require "GameJam.php"; //GameJam has no foreign key require "GameData.php"; //GameData has no foreign key require "Group.php"; //Group has foreign keys to the GameJam, GameData and Password tables hence we create it as one of the last tables in the database require "Password.php"; //Password has no foreign key +require "Registration.php"; require "KeyWord.php"; //Group has foreign keys to the Group require "Vote.php"; require "AdminUser.php"; diff --git a/Backend/Models/AdminUser.php b/Backend/Models/AdminUser.php index 5cfd1a4..4c63c83 100644 --- a/Backend/Models/AdminUser.php +++ b/Backend/Models/AdminUser.php @@ -1,7 +1,9 @@ hasOne(Group::class); + public function registration() + { + return $this->hasOne(Registration::class); + } + + public function votes() + { + return $this->hasMany(Vote::class); } } diff --git a/Backend/Models/GameJam.php b/Backend/Models/GameJam.php index a12bc32..466fdc2 100644 --- a/Backend/Models/GameJam.php +++ b/Backend/Models/GameJam.php @@ -1,10 +1,13 @@ hasMany(Group::class); - } - public function hasVoted(){ - return $this->hasMany(hasVoted::class); + public function registrations() + { + return $this->hasMany(Registration::class); } + } \ No newline at end of file diff --git a/Backend/Models/Group.php b/Backend/Models/Group.php index fa5a5c6..c5e9073 100644 --- a/Backend/Models/Group.php +++ b/Backend/Models/Group.php @@ -1,26 +1,24 @@ belongsTo(GameJam::class); - } - public function keyWord(){ - return $this->hasMany(KeyWord::class); - } - public function gameData(){ - return $this->belongsTo(GameData::class); - } - public function password(){ + + public function password() + { return $this->hasOne(Password::class); } - public function vote(){ - return $this->hasMany(Vote::class); + + public function registrations() + { + return $this->hasMany(Registration::class); } } diff --git a/Backend/Models/KeyWord.php b/Backend/Models/KeyWord.php index 3df3fa5..cbb61d1 100644 --- a/Backend/Models/KeyWord.php +++ b/Backend/Models/KeyWord.php @@ -1,14 +1,19 @@ belongsTo(Group::class); + public function registration() + { + return $this->belongsTo(Registration::class); } } \ No newline at end of file diff --git a/Backend/Models/Password.php b/Backend/Models/Password.php index 730a308..456974a 100644 --- a/Backend/Models/Password.php +++ b/Backend/Models/Password.php @@ -1,13 +1,18 @@ belongsTo(Group::class); } diff --git a/Backend/Models/Registration.php b/Backend/Models/Registration.php new file mode 100644 index 0000000..6dbfb99 --- /dev/null +++ b/Backend/Models/Registration.php @@ -0,0 +1,35 @@ +belongsTo(Group::class); + } + + public function gameJam() + { + return $this->belongsTo(GameJam::class); + } + + public function keyWords() + { + return $this->hasMany(KeyWord::class); + } + + public function gameData() + { + return $this->belongsTo(GameData::class); + } + + +} diff --git a/Backend/Models/Vote.php b/Backend/Models/Vote.php index f2a67b2..0961204 100644 --- a/Backend/Models/Vote.php +++ b/Backend/Models/Vote.php @@ -11,9 +11,9 @@ class Vote extends Eloquent ]; - public function group() + public function gameDate() { - return $this->belongsTo(Group::class); + return $this->belongsTo(GameData::class); } } \ No newline at end of file