| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- use uuid::Uuid;
- use chrono::{DateTime, Utc};
- use sqlx::{PgPool, FromRow};
- use serde::Serialize;
- use crate::http_error::HttpError;
- #[derive(Serialize, FromRow)]
- pub struct Session {
- pub id: Uuid,
- pub game_id: Uuid,
- pub summary: Option<String>,
- pub started_at: DateTime<Utc>,
- pub ended_at: Option<DateTime<Utc>>,
- }
- impl Session {
- pub fn new(game_id: Uuid) -> Self {
- Self {
- id: Uuid::now_v7(),
- game_id: game_id,
- summary: None,
- started_at: Utc::now(),
- ended_at: None
- }
- }
- pub async fn insert(&self, db: &PgPool) -> Result<(), HttpError> {
- sqlx::query(
- "INSERT INTO sessions(id, game_id, summary, started_at, ended_at)
- VALUES($1, $2, $3, $4, $5)"
- )
- .bind(&self.id)
- .bind(&self.game_id)
- .bind(&self.summary)
- .bind(&self.started_at)
- .bind(&self.ended_at)
- .execute(db)
- .await?;
- Ok(())
- }
- }
|