Compare commits
3 Commits
28b2411ceb
...
92c067c9ed
| Author | SHA1 | Date | |
|---|---|---|---|
| 92c067c9ed | |||
| 164184e5e9 | |||
| 9c8e8beaa6 |
@@ -391,14 +391,19 @@ async fn post_player(req: Request<Incoming>, db: Arc<Mutex<SqlitePool>>) -> Resu
|
||||
let name = data.get("name").unwrap().as_str().unwrap();
|
||||
let pool = db.clone().lock().unwrap().clone();
|
||||
let mut conn = pool.acquire().await.unwrap();
|
||||
let r = sqlx::query!(r#"INSERT INTO players (name) VALUES (?1)"#, data).execute(&mut *conn).await;
|
||||
if let Ok(Some(player)) = sqlx::query!(r#"SELECT * FROM players WHERE name = ?1"#, name).fetch_optional(&pool).await {
|
||||
let player = Player{id: player.id, name: player.name};
|
||||
return Ok(Response::builder().body(Body::new(serde_json::to_string(&player).unwrap())).unwrap());
|
||||
}
|
||||
let r = sqlx::query!(r#"INSERT INTO players (name) VALUES (?1) RETURNING id"#, name).fetch_one(&mut *conn).await;
|
||||
if r.is_err() {
|
||||
return Ok(Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(Body::Empty)
|
||||
.unwrap());
|
||||
}
|
||||
ok().await
|
||||
let player = Player{id: r.unwrap().id, name: name.to_string()};
|
||||
Ok(Response::builder().body(Body::new(serde_json::to_string(&player).unwrap())).unwrap())
|
||||
}
|
||||
|
||||
async fn post_admin(
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
<h1 id="app_title">Vote +</h1>
|
||||
<label for="player_id">Pour qui votes tu?</label>
|
||||
<select id="player_id"></select>
|
||||
<label id="other_label" for="other" hidden="hidden">Autre:</label>
|
||||
<input id="other" hidden="hidden">
|
||||
<label for="nickname">As-tu un surnom à lui donner?</label>
|
||||
<input type="text" id="nickname">
|
||||
<label for="reason">Pourquoi votes-tu pour lui?</label>
|
||||
|
||||
@@ -24,10 +24,25 @@ async function main() {
|
||||
option.textContent = x["name"];
|
||||
select.append(option);
|
||||
})
|
||||
let other = document.createElement("option");
|
||||
other.value = "other";
|
||||
other.textContent = "Autre";
|
||||
select.append(other);
|
||||
select.value = null;
|
||||
nickname.value = "";
|
||||
reason.value = "";
|
||||
select.addEventListener("change", () => {
|
||||
if (select.value === "other") {
|
||||
let other = document.getElementById("other");
|
||||
other.hidden = false;
|
||||
let otherLabel = document.getElementById("other_label");
|
||||
otherLabel.hidden = false;
|
||||
} else {
|
||||
let other = document.getElementById("other");
|
||||
other.hidden = true;
|
||||
let otherLabel = document.getElementById("other_label");
|
||||
otherLabel.hidden = true;
|
||||
}
|
||||
if (current_page) {
|
||||
vote.minus_player_id = parseInt(select.value);
|
||||
} else {
|
||||
@@ -59,6 +74,23 @@ async function main() {
|
||||
true, "warning");
|
||||
return;
|
||||
}
|
||||
if (select.value === "other") {
|
||||
let otherInput = document.getElementById("other");
|
||||
if (otherInput.value === "") {
|
||||
showMessage("Tu as sélectionné autre mais tu n'as pas donné de nom", "N'oublie pas de soit sélectioner une personne ou de mettre un nom dans ''Autre:''.", true, "warning");
|
||||
return;
|
||||
}
|
||||
let otherInputLabel = document.getElementById("other_label");
|
||||
let player = await fetch("/player", {method: "post", body: JSON.stringify({"name": otherInput.value})}).then(r => r.json());
|
||||
let option = document.createElement("option");
|
||||
option.value = player["id"];
|
||||
option.textContent = player["name"];
|
||||
select.insertBefore(option, other);
|
||||
vote.minus_player_id = parseInt(player["id"]);
|
||||
otherInput.value = "";
|
||||
otherInput.hidden = true;
|
||||
otherInputLabel.hidden = true;
|
||||
}
|
||||
if (await fetch("/vote", {
|
||||
method: "post", body: JSON.stringify(vote)
|
||||
})
|
||||
@@ -76,6 +108,23 @@ async function main() {
|
||||
}
|
||||
rightButton.textContent = "À voté!";
|
||||
title.textContent = "Vote -";
|
||||
if (select.value === "other") {
|
||||
let otherInput = document.getElementById("other");
|
||||
if (otherInput.value === "") {
|
||||
showMessage("Tu as sélectionné autre mais tu n'as pas donné de nom", "N'oublie pas de soit sélectioner une personne ou de mettre un nom dans ''Autre:''.", true, "warning");
|
||||
return;
|
||||
}
|
||||
let otherInputLabel = document.getElementById("other_label");
|
||||
let player = await fetch("/player", {method: "post", body: JSON.stringify({"name": otherInput.value})}).then(r => r.json());
|
||||
let option = document.createElement("option");
|
||||
option.value = player["id"];
|
||||
option.textContent = player["name"];
|
||||
select.insertBefore(option, other);
|
||||
vote.plus_player_id = parseInt(player["id"]);
|
||||
otherInput.value = "";
|
||||
otherInput.hidden = true;
|
||||
otherInputLabel.hidden = true;
|
||||
}
|
||||
current_page = 1;
|
||||
leftButton.hidden = false;
|
||||
select.value = vote.minus_player_id;
|
||||
|
||||
@@ -23,7 +23,7 @@ function show_plus(id, votes, players) {
|
||||
const app = document.getElementById("app");
|
||||
app.innerHTML = "";
|
||||
let vote = votes[id];
|
||||
let player = "";
|
||||
let player = "?";
|
||||
for (let i = 0; i < players.length; i++) {
|
||||
if (players[i].id === vote["plus_player_id"]) {
|
||||
player = players[i]["name"];
|
||||
@@ -63,7 +63,7 @@ function show_minus(id, votes, players) {
|
||||
let vote = votes[id];
|
||||
let nickname = vote["minus_nickname"];
|
||||
let reason = vote["minus_reason"];
|
||||
let player = "";
|
||||
let player = "?";
|
||||
for (let i = 0; i < players.length; i++) {
|
||||
if (players[i].id === vote["minus_player_id"]) {
|
||||
player = players[i]["name"];
|
||||
@@ -122,7 +122,7 @@ async function show_results(players) {
|
||||
let counter = 0;
|
||||
for (let i = 0; i < plus.length; i++) {
|
||||
let p = plus[i];
|
||||
let player = "";
|
||||
let player = "?";
|
||||
for (let i = 0; i < players.length; i++) {
|
||||
if (players[i].id === p[0]) {
|
||||
player = players[i]["name"];
|
||||
@@ -149,7 +149,7 @@ async function show_results(players) {
|
||||
counter = 0;
|
||||
for (let i = 0; i < minus.length; i++) {
|
||||
let p = minus[i];
|
||||
let player = "";
|
||||
let player = "?";
|
||||
for (let i = 0; i < players.length; i++) {
|
||||
if (players[i].id === p[0]) {
|
||||
player = players[i]["name"];
|
||||
|
||||
Reference in New Issue
Block a user