114 lines
2.4 KiB
Rust
114 lines
2.4 KiB
Rust
|
|
use crate::atproto::{
|
||
|
|
Did,
|
||
|
|
Uri,
|
||
|
|
}
|
||
|
|
use sqlx::{
|
||
|
|
query,
|
||
|
|
Database,
|
||
|
|
Pool,
|
||
|
|
Postgres,
|
||
|
|
pool::PoolOptions,
|
||
|
|
postgres::{
|
||
|
|
PgConnectOptions,
|
||
|
|
PgSslMode,
|
||
|
|
},
|
||
|
|
Result,
|
||
|
|
};
|
||
|
|
use std::string::ToString;
|
||
|
|
|
||
|
|
pub struct Db<Dbimp: Database> {
|
||
|
|
pool: Pool<Dbimp>
|
||
|
|
}
|
||
|
|
|
||
|
|
pub struct User {
|
||
|
|
userdid: Did,
|
||
|
|
handle: Handle,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[non_exhaustive]
|
||
|
|
enum Role {
|
||
|
|
Owner,
|
||
|
|
Participant
|
||
|
|
}
|
||
|
|
|
||
|
|
impl ToString for Role {
|
||
|
|
fn to_string(&self) -> String {
|
||
|
|
match *self {
|
||
|
|
Role::Owner => "owner".to_string(),
|
||
|
|
Role::Participant => "participant".to_string(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
struct Participant {
|
||
|
|
participantdid: Did,
|
||
|
|
role: Role,
|
||
|
|
}
|
||
|
|
|
||
|
|
pub struct Session {
|
||
|
|
sessionuri: Uri,
|
||
|
|
label: Option<String>,
|
||
|
|
participants: Vec<Participant>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Db<Postgres> {
|
||
|
|
async fn connect() -> Result<Self> {
|
||
|
|
let conn = PgConnectOptions::new()
|
||
|
|
.host("localhost")
|
||
|
|
.port(5432)
|
||
|
|
.username("postgres")
|
||
|
|
.password("062217")
|
||
|
|
.database("anisky")
|
||
|
|
.ssl_mode(PgSslMode::Disable);
|
||
|
|
|
||
|
|
let pool = match PoolOptions::new().connect_with(conn).await {
|
||
|
|
Ok(p) => p,
|
||
|
|
Err(e) => return Err(e),
|
||
|
|
};
|
||
|
|
|
||
|
|
Ok(Db { pool })
|
||
|
|
}
|
||
|
|
//
|
||
|
|
// pub async fn add_user(&self, user: &User) -> Result<()> {
|
||
|
|
// query!(r#"
|
||
|
|
// INSERT INTO users(userdid, handle) VALUES ($1, $2)
|
||
|
|
// "#,
|
||
|
|
// user.userdid, user.handle
|
||
|
|
// ).execute(self.pool).await?;
|
||
|
|
// Ok(())
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// pub async fn add_session(&self, session: &Session) -> Result<()> {
|
||
|
|
// let mut transaction = self.pool.begin().await?;
|
||
|
|
//
|
||
|
|
// query!(r#"
|
||
|
|
// INSERT INTO sessions(sessionuri, label) VALUES ($1, $2)
|
||
|
|
// "#,
|
||
|
|
// session.sessionuri, session.label
|
||
|
|
// ).execute(&mut *transaction).await?;
|
||
|
|
//
|
||
|
|
// for participant in session.participants {
|
||
|
|
// query!(r#"
|
||
|
|
// INSERT INTO participants(sessionuri, userdid, role) VALUES ($1, $2, $3)
|
||
|
|
// "#,
|
||
|
|
// session.sessionuri, participant.userdid, participant.role.to_string()
|
||
|
|
// ).execute(&mut *transaction).await?;
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// transaction.commit().await
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// pub async fn add_participant(&self, session: Session,
|
||
|
|
// participant: Participant) -> Result<Session> {
|
||
|
|
// query!(r#"
|
||
|
|
// INSERT INTO participants(sessionuri, userdid, role) VALUES ($1, $2, $3)
|
||
|
|
// "#,
|
||
|
|
// session.sessionuri, participant.userdid, participant.role.to_string()
|
||
|
|
// ).execute(self.pool).await?;
|
||
|
|
//
|
||
|
|
// session.participants.push(participant);
|
||
|
|
//
|
||
|
|
// Ok(session)
|
||
|
|
// }
|
||
|
|
}
|