admin panel has a new game button
This commit is contained in:
@@ -73,6 +73,33 @@ pub async fn update_game(
|
||||
execute_stmt(pool, update_query).await
|
||||
}
|
||||
|
||||
pub async fn new_game(pool: &Pool, code: String) -> Result<String, Error> {
|
||||
let insert_game = Query::insert()
|
||||
.into_table("games")
|
||||
.columns(["code", "team", "start_time", "end_time"])
|
||||
.values_panic([
|
||||
code.clone().into(),
|
||||
"test".into(),
|
||||
"2011-01-25T03:00:00".into(),
|
||||
"2011-01-25T08:00:00".into(),
|
||||
])
|
||||
.take();
|
||||
let insert_game = &insert_game.to_string(SqliteQueryBuilder);
|
||||
let insert_team = Query::insert()
|
||||
.into_table("teams")
|
||||
.columns(["game", "team", "color", "player"])
|
||||
.values_panic([
|
||||
code.into(),
|
||||
"test".into(),
|
||||
"#ffffffff".into(),
|
||||
"test".into(),
|
||||
])
|
||||
.take();
|
||||
let insert_team = &insert_team.to_string(SqliteQueryBuilder);
|
||||
let _ = execute_stmt(pool, insert_game).await;
|
||||
execute_stmt(pool, insert_team).await
|
||||
}
|
||||
|
||||
fn get_all_games(conn: Connection, all_teams: HashMap<String, Team>) -> GamesResult {
|
||||
let mut games_stmt = conn.prepare(GAMES_QUERY)?;
|
||||
games_stmt
|
||||
|
||||
+14
@@ -69,6 +69,11 @@ struct UpdateGameForm {
|
||||
end_time: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct NewGameForm {
|
||||
code: String,
|
||||
}
|
||||
|
||||
impl Actor for GamesActor {
|
||||
type Context = actix::Context<Self>;
|
||||
}
|
||||
@@ -215,6 +220,14 @@ async fn update_game(pool: web::Data<Pool>, form: web::Form<UpdateGameForm>) ->
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/admin/game/new")]
|
||||
async fn new_game(pool: web::Data<Pool>, form: web::Form<NewGameForm>) -> impl Responder {
|
||||
match db::new_game(&pool, form.code.clone()).await {
|
||||
Ok(_) => HttpResponse::Ok().body("{}"),
|
||||
Err(err) => HttpResponse::InternalServerError().body(format!("{err:?}")),
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/admin/query")]
|
||||
async fn admin_query(pool: web::Data<Pool>, form: web::Form<QueryForm>) -> impl Responder {
|
||||
let res: String;
|
||||
@@ -255,6 +268,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(list_of_games)
|
||||
.service(get_territories)
|
||||
.service(update_game)
|
||||
.service(new_game)
|
||||
})
|
||||
.bind(("0.0.0.0", 8080))?
|
||||
.run()
|
||||
|
||||
@@ -46,6 +46,10 @@
|
||||
</tr></thead>
|
||||
<tbody id="games_table"></tbody>
|
||||
</table>
|
||||
<form id="add_game_form">
|
||||
<input type="text" name="game_name" id="game_name" value="game name"/>
|
||||
<button type="submit">Add A Game</button>
|
||||
</form>
|
||||
<form id="query_form">
|
||||
<p><label for="query">Compose a sql query:</label></p>
|
||||
<textarea id="query" name="query" rows="6" cols="60">select * from games limit 10;</textarea>
|
||||
@@ -82,10 +86,12 @@
|
||||
const code_edit = document.getElementById("code_edit");
|
||||
const query_form = document.getElementById("query_form");
|
||||
const edit_form = document.getElementById("edit_form");
|
||||
const add_game_form = document.getElementById("add_game_form");
|
||||
|
||||
games_form.addEventListener("submit", refresh_games);
|
||||
query_form.addEventListener("submit", submit_query);
|
||||
edit_form.addEventListener("submit", submit_edit);
|
||||
add_game_form.addEventListener("submit", add_game);
|
||||
|
||||
function submit_query(event) {
|
||||
event.preventDefault();
|
||||
@@ -109,6 +115,7 @@
|
||||
console.error("Fetching data failed: ", error);
|
||||
});
|
||||
}
|
||||
|
||||
function submit_edit(event) {
|
||||
event.preventDefault();
|
||||
fetch("/admin/game/update", {
|
||||
@@ -134,6 +141,7 @@
|
||||
});
|
||||
console.log("should handle this");
|
||||
}
|
||||
|
||||
function refresh_games(event) {
|
||||
event.preventDefault();
|
||||
fetch("/admin/games")
|
||||
@@ -191,18 +199,42 @@
|
||||
console.error("Fetching data failed:", error);
|
||||
});
|
||||
}
|
||||
|
||||
function showPlayersModal(name, color, players) {
|
||||
td_name.textContent = name;
|
||||
td_color.textContent = color;
|
||||
td_players.textContent = players.map((e)=>e['username']);
|
||||
team_dialog.showModal();
|
||||
}
|
||||
|
||||
function showEditModal(code, start_time, end_time) {
|
||||
code_edit.value = code;
|
||||
start_time_edit.value = start_time;
|
||||
end_time_edit.value = end_time;
|
||||
edit_dialog.showModal()
|
||||
}
|
||||
|
||||
function add_game(event) {
|
||||
event.preventDefault();
|
||||
fetch("/admin/game/new", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({
|
||||
'code': document.getElementById("game_name").value,
|
||||
})
|
||||
})
|
||||
.then(res => {
|
||||
if (!res.ok) {
|
||||
throw new Error(`Response status: ${res.status}`);
|
||||
}
|
||||
return res.json();
|
||||
})
|
||||
.then((data) => {
|
||||
console.log(data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("creating game failed: ", error);
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user