use chrono::{DateTime, Utc}; use deadpool_postgres::Object; use serde_json::Value; use tokio_postgres::Row; pub struct Operation { pub did: String, pub hash: String, pub prev: Option, pub sig: String, pub nullified: bool, pub operation: Value, pub created_at: DateTime, } impl From for Operation { fn from(row: Row) -> Self { Operation { did: row.get(0), hash: row.get(1), prev: row.get(2), sig: row.get(3), nullified: row.get(4), operation: row.get(5), created_at: row.get(6), } } } pub async fn get_latest_operation( conn: &Object, did: &str, ) -> Result, tokio_postgres::Error> { let maybe_op = conn .query_opt( "SELECT * FROM operations WHERE did=$1 ORDER BY created_at DESC LIMIT 1", &[&did], ) .await?; Ok(maybe_op.map(Operation::from)) } pub async fn get_operations( conn: &Object, did: &str, ) -> Result, tokio_postgres::Error> { let ops = conn .query( "SELECT * FROM operations WHERE did=$1 ORDER BY created_at", &[&did], ) .await?; Ok(ops.into_iter().map(Operation::from).collect()) } pub async fn get_last_operation_ts( conn: &Object, ) -> Result>, tokio_postgres::Error> { conn.query_opt( "SELECT created_at FROM operations ORDER BY created_at DESC LIMIT 1", &[], ) .await .map(|v| v.map(|row| row.get(0))) }