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

60 lines
1.9 KiB
PHP
Raw Normal View History

2021-03-05 09:52:42 +00:00
<?php
2021-03-16 11:46:44 +00:00
require_once "../../../bootstrap.php";
2021-03-11 12:20:10 +00:00
require_once "../Group/Group.php";
2021-04-19 07:15:15 +00:00
use Backend\Models\GameJam;
use Backend\Models\Registration;
2021-03-05 09:52:42 +00:00
use Backend\Models\Group;
use Backend\Models\KeyWord;
session_start();
2021-03-05 09:52:42 +00:00
2021-03-16 11:46:44 +00:00
if (isLogin()) {
2021-04-19 07:15:15 +00:00
if (isset($_POST['submitKeyWord'])) {
//Find the group
2021-03-11 12:20:10 +00:00
$group = groupViaToken($_SESSION['token']);
2021-04-19 07:15:15 +00:00
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
2021-04-19 07:15:15 +00:00
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
2021-03-08 12:09:50 +00:00
$keyword->key_word = $_POST['key_word'];
2021-03-05 09:52:42 +00:00
//Make foreign key from the groups table to the keyWords table
2021-04-19 07:15:15 +00:00
$keyword->registration()->associate($registration);
2021-03-05 09:52:42 +00:00
//Try to save it
2021-04-19 07:15:15 +00:00
if (!$keyword->save()) {
2021-03-23 12:39:44 +00:00
http_response_code(500);
2021-04-19 07:15:15 +00:00
} else {
2021-03-23 12:39:44 +00:00
http_response_code(201);
}
2021-04-19 07:15:15 +00:00
} else {
2021-03-23 12:39:44 +00:00
http_response_code(400);
echo json_encode(["message" => "limited upload reached"]);
}
2021-03-05 09:52:42 +00:00
2021-04-19 07:15:15 +00:00
} else {
2021-03-23 12:39:44 +00:00
http_response_code(400);
}
2021-04-19 07:15:15 +00:00
} else {
2021-03-23 12:39:44 +00:00
http_response_code(401);
echo json_encode(["message" => "not login"]);
}