From fc103599974240cbd5abcfbf2a6f93678e57c2ee Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 8 Feb 2022 20:13:24 +0100 Subject: store demo matches in database --- planetwars-server/src/db/matches.rs | 21 ++++++++++++--------- planetwars-server/src/db/users.rs | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'planetwars-server/src/db') diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index 36c2200..efaa1eb 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -46,16 +46,16 @@ pub struct MatchPlayerData { } pub fn create_match( - match_data: &NewMatch, - match_players: &[MatchPlayerData], + new_match_base: &NewMatch, + new_match_players: &[MatchPlayerData], conn: &PgConnection, -) -> QueryResult { +) -> QueryResult { conn.transaction(|| { let match_base = diesel::insert_into(matches::table) - .values(match_data) + .values(new_match_base) .get_result::(conn)?; - let match_players = match_players + let new_match_players = new_match_players .iter() .enumerate() .map(|(num, player_data)| NewMatchPlayer { @@ -65,11 +65,14 @@ pub fn create_match( }) .collect::>(); - diesel::insert_into(match_players::table) - .values(&match_players) - .execute(conn)?; + let match_players = diesel::insert_into(match_players::table) + .values(&new_match_players) + .get_results::(conn)?; - Ok(match_base.id) + Ok(MatchData { + base: match_base, + match_players, + }) }) } diff --git a/planetwars-server/src/db/users.rs b/planetwars-server/src/db/users.rs index 663f173..a97ade5 100644 --- a/planetwars-server/src/db/users.rs +++ b/planetwars-server/src/db/users.rs @@ -2,7 +2,7 @@ use crate::schema::users; use argon2; use diesel::{prelude::*, PgConnection}; use rand::Rng; -use serde::{Deserialize, Serialize}; +use serde::Deserialize; #[derive(Debug, Deserialize)] pub struct Credentials<'a> { -- cgit v1.2.3