diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-02-08 20:13:24 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-02-08 20:13:24 +0100 |
commit | fc103599974240cbd5abcfbf2a6f93678e57c2ee (patch) | |
tree | eb4c62696ded0f5beffd83c42efc475b81882b57 /planetwars-server/src/db | |
parent | 69f33763ab7ff5876cff23fb90cfd2497b3fe371 (diff) | |
download | planetwars.dev-fc103599974240cbd5abcfbf2a6f93678e57c2ee.tar.xz planetwars.dev-fc103599974240cbd5abcfbf2a6f93678e57c2ee.zip |
store demo matches in database
Diffstat (limited to 'planetwars-server/src/db')
-rw-r--r-- | planetwars-server/src/db/matches.rs | 21 | ||||
-rw-r--r-- | planetwars-server/src/db/users.rs | 2 |
2 files changed, 13 insertions, 10 deletions
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<i32> { +) -> QueryResult<MatchData> { conn.transaction(|| { let match_base = diesel::insert_into(matches::table) - .values(match_data) + .values(new_match_base) .get_result::<MatchBase>(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::<Vec<_>>(); - 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::<MatchPlayer>(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> { |