|
|
@@ -1,10 +1,42 @@
|
|
|
use uuid::Uuid;
|
|
|
use chrono::{DateTime, Utc};
|
|
|
+use sqlx::PgPool;
|
|
|
+use serde::Serialize;
|
|
|
+use crate::http_error::HttpError;
|
|
|
|
|
|
+#[derive(Serialize)]
|
|
|
pub struct Game {
|
|
|
pub id: Uuid,
|
|
|
pub user_id: Uuid,
|
|
|
pub title: String,
|
|
|
- pub world_context: String,
|
|
|
- pub createdAt: DateTime<Utc>
|
|
|
+ pub game_context: String,
|
|
|
+ pub created_at: DateTime<Utc>
|
|
|
+}
|
|
|
+
|
|
|
+impl Game {
|
|
|
+ pub fn new(title: String, game_context: String, user_id: Uuid) -> Self {
|
|
|
+ Self {
|
|
|
+ id: Uuid::now_v7(),
|
|
|
+ user_id: user_id,
|
|
|
+ title: title,
|
|
|
+ game_context: game_context,
|
|
|
+ created_at: Utc::now()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pub async fn insert(&self, db: &PgPool) -> Result<(), HttpError> {
|
|
|
+ sqlx::query(
|
|
|
+ "INSERT into games(id, user_id, title, game_context, created_at)
|
|
|
+ VALUES($1, $2, $3, $4, $5)"
|
|
|
+ )
|
|
|
+ .bind(&self.id)
|
|
|
+ .bind(&self.user_id)
|
|
|
+ .bind(&self.title)
|
|
|
+ .bind(&self.game_context)
|
|
|
+ .bind(&self.created_at)
|
|
|
+ .execute(db)
|
|
|
+ .await?;
|
|
|
+
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
}
|