Game-Jaming/Backend/Controllers/keyWord/NewKeyWord.php

60 lines
1.9 KiB
PHP

<?php
require_once "../../../bootstrap.php";
require_once "../Group/Group.php";
use Backend\Models\GameJam;
use Backend\Models\Registration;
use Backend\Models\Group;
use Backend\Models\KeyWord;
session_start();
if (isLogin()) {
if (isset($_POST['submitKeyWord'])) {
//Find the group
$group = groupViaToken($_SESSION['token']);
if($gameJam = GameJam::find($_POST["gameJamId"]) === 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();
}
//if statement to find out if the group have use all their keyWords
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();
//Take the keyWord the user typed and set it equal to the keyword valuable
$keyword->key_word = $_POST['key_word'];
//Make foreign key from the groups table to the keyWords table
$keyword->registration()->associate($registration);
//Try to save it
if (!$keyword->save()) {
http_response_code(500);
} else {
http_response_code(201);
}
} else {
http_response_code(400);
echo json_encode(["message" => "limited upload reached"]);
}
} else {
http_response_code(400);
}
} else {
http_response_code(401);
echo json_encode(["message" => "not login"]);
}