From f2446f1f7c0e80f77b15ee09e1a2fde01a424881 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Fri, 10 Mar 2023 11:54:42 +0200 Subject: [PATCH] feat(api): impl models action, gamedata, game --- Cargo.lock | 6 ++++-- api/Cargo.toml | 7 ++++++- api/src/models/action.rs | 34 ++++++++++++++++++++++++++++++++++ api/src/models/game.rs | 36 ++++++++++++++++++++++++++++++++++++ api/src/models/gamedata.rs | 36 ++++++++++++++++++++++++++++++++++++ api/src/models/mod.rs | 9 +++++++++ 6 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 api/src/models/action.rs create mode 100644 api/src/models/game.rs create mode 100644 api/src/models/gamedata.rs diff --git a/Cargo.lock b/Cargo.lock index d1b64d4..53d9858 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -473,6 +473,7 @@ dependencies = [ "log", "pretty_env_logger", "serde", + "serde_json", "shared", "uuid 1.3.0", ] @@ -2434,6 +2435,7 @@ dependencies = [ "itoa", "pq-sys", "r2d2", + "serde_json", "uuid 1.3.0", ] @@ -5348,9 +5350,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.91" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" +checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" dependencies = [ "itoa", "ryu", diff --git a/api/Cargo.toml b/api/Cargo.toml index f3cc4aa..3e0cfb7 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -25,6 +25,11 @@ version = "1.0.152" default-features = false features = [ "derive" ] +[dependencies.serde_json] +version = "1.0.94" +default-features = false +features = [ "std" ] + [dependencies.chrono] version = "0.4.23" default-features = false @@ -43,7 +48,7 @@ features = [ "redis-actor-session" ] [dependencies.diesel] version = "2.0.3" default-features = false -features = [ "with-deprecated", "32-column-tables", "postgres", "r2d2", "chrono", "uuid" ] +features = [ "with-deprecated", "32-column-tables", "postgres", "r2d2", "chrono", "uuid", "serde_json" ] [dependencies.uuid] version = "1.3.0" diff --git a/api/src/models/action.rs b/api/src/models/action.rs new file mode 100644 index 0000000..3b819b3 --- /dev/null +++ b/api/src/models/action.rs @@ -0,0 +1,34 @@ +/* + * This file is part of laurelin/api + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use chrono::NaiveDateTime; +use diesel::{Insertable, Queryable}; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use crate::schema::actions; + +#[derive(Serialize, Queryable)] +pub(crate) struct Action { + pub id: Uuid, + pub created_at: NaiveDateTime, + pub updated_at: NaiveDateTime, + pub game_data_id: Uuid, + pub invoker: Uuid, + pub data: serde_json::Value, + pub timestamp: NaiveDateTime, +} + +#[derive(Deserialize, Insertable)] +#[diesel(table_name=actions)] +pub(crate) struct InsertableAction { + pub game_data_id: Uuid, + pub invoker: Uuid, + pub data: serde_json::Value, + pub timestamp: NaiveDateTime, +} diff --git a/api/src/models/game.rs b/api/src/models/game.rs new file mode 100644 index 0000000..81c87e1 --- /dev/null +++ b/api/src/models/game.rs @@ -0,0 +1,36 @@ +/* + * This file is part of laurelin/api + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use chrono::NaiveDateTime; +use diesel::{Insertable, Queryable}; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use crate::schema::games; + +#[derive(Serialize, Queryable)] +pub(crate) struct Game { + pub id: Uuid, + pub created_at: NaiveDateTime, + pub updated_at: NaiveDateTime, + pub host_id: Uuid, + pub guest_id: Option, + pub state: i16, + pub ended_at: Option, + pub game_data_id: Uuid, +} + +#[derive(Deserialize, Insertable)] +#[diesel(table_name=games)] +pub(crate) struct InsertableGame { + pub host_id: Uuid, + pub guest_id: Option, + pub state: i16, + pub ended_at: Option, + pub game_data_id: Uuid, +} diff --git a/api/src/models/gamedata.rs b/api/src/models/gamedata.rs new file mode 100644 index 0000000..857a241 --- /dev/null +++ b/api/src/models/gamedata.rs @@ -0,0 +1,36 @@ +/* + * This file is part of laurelin/api + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use chrono::NaiveDateTime; +use diesel::{Insertable, Queryable}; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use crate::schema::gamedata; + +#[derive(Serialize, Queryable)] +pub(crate) struct GameData { + pub id: Uuid, + pub created_at: NaiveDateTime, + pub updated_at: NaiveDateTime, + pub turn: i16, + pub host_hand: Option, + pub host_deck: Option, + pub guest_hand: Option, + pub guest_deck: Option, +} + +#[derive(Deserialize, Insertable)] +#[diesel(table_name=gamedata)] +pub(crate) struct InsertableGameData { + pub turn: i16, + pub host_hand: Option, + pub host_deck: Option, + pub guest_hand: Option, + pub guest_deck: Option, +} diff --git a/api/src/models/mod.rs b/api/src/models/mod.rs index fd25ca9..2ea9cda 100644 --- a/api/src/models/mod.rs +++ b/api/src/models/mod.rs @@ -8,3 +8,12 @@ mod user; pub(crate) use user::*; + +mod action; +pub(crate) use action::*; + +mod game; +pub(crate) use game::*; + +mod gamedata; +pub(crate) use gamedata::*; -- 2.44.1