A PLC Mirror written in Rust
at main 1.6 kB view raw
1use chrono::{DateTime, Utc}; 2use deadpool_postgres::Object; 3use serde_json::Value; 4use tokio_postgres::Row; 5 6pub struct Operation { 7 pub did: String, 8 pub hash: String, 9 pub prev: Option<String>, 10 pub sig: String, 11 pub nullified: bool, 12 pub operation: Value, 13 pub created_at: DateTime<Utc>, 14} 15 16impl From<Row> for Operation { 17 fn from(row: Row) -> Self { 18 Operation { 19 did: row.get(0), 20 hash: row.get(1), 21 prev: row.get(2), 22 sig: row.get(3), 23 nullified: row.get(4), 24 operation: row.get(5), 25 created_at: row.get(6), 26 } 27 } 28} 29 30pub async fn get_latest_operation( 31 conn: &Object, 32 did: &str, 33) -> Result<Option<Operation>, tokio_postgres::Error> { 34 let maybe_op = conn 35 .query_opt( 36 "SELECT * FROM operations WHERE did=$1 ORDER BY created_at DESC LIMIT 1", 37 &[&did], 38 ) 39 .await?; 40 41 Ok(maybe_op.map(Operation::from)) 42} 43 44pub async fn get_operations( 45 conn: &Object, 46 did: &str, 47) -> Result<Vec<Operation>, tokio_postgres::Error> { 48 let ops = conn 49 .query( 50 "SELECT * FROM operations WHERE did=$1 ORDER BY created_at", 51 &[&did], 52 ) 53 .await?; 54 55 Ok(ops.into_iter().map(Operation::from).collect()) 56} 57 58pub async fn get_last_operation_ts( 59 conn: &Object, 60) -> Result<Option<DateTime<Utc>>, tokio_postgres::Error> { 61 conn.query_opt( 62 "SELECT created_at FROM operations ORDER BY created_at DESC LIMIT 1", 63 &[], 64 ) 65 .await 66 .map(|v| v.map(|row| row.get(0))) 67}