cat_gateway/db/event/signed_docs/
signed_doc_body.rs

1//! `SignedDocBody` struct implementation.
2
3use futures::{Stream, StreamExt};
4
5use super::DocsQueryFilter;
6use crate::{
7    db::event::{common::query_limits::QueryLimits, error::NotFoundError, EventDB},
8    jinja::{get_template, JinjaTemplateSource},
9};
10
11/// Filtered select sql query jinja template
12pub(crate) const FILTERED_SELECT_SIGNED_DOCS_TEMPLATE: JinjaTemplateSource = JinjaTemplateSource {
13    name: "filtered_select_signed_documents.jinja.template",
14    source: include_str!("./sql/filtered_select_signed_documents.sql.jinja"),
15};
16
17/// Filtered count sql query jinja template
18pub(crate) const FILTERED_COUNT_SIGNED_DOCS_TEMPLATE: JinjaTemplateSource = JinjaTemplateSource {
19    name: "filtered_count_signed_documents.jinja.template",
20    source: include_str!("./sql/filtered_count_signed_documents.sql.jinja"),
21};
22
23/// Signed doc body event db struct
24#[derive(Debug, Clone, PartialEq)]
25pub(crate) struct SignedDocBody {
26    /// `signed_doc` table `id` field
27    id: uuid::Uuid,
28    /// `signed_doc` table `ver` field
29    ver: uuid::Uuid,
30    /// `signed_doc` table `type` field
31    doc_type: uuid::Uuid,
32    /// `signed_doc` table `authors` field
33    authors: Vec<String>,
34    /// `signed_doc` table `metadata` field
35    metadata: Option<serde_json::Value>,
36}
37
38impl SignedDocBody {
39    /// Returns the document id.
40    pub(crate) fn id(&self) -> &uuid::Uuid {
41        &self.id
42    }
43
44    /// Returns the document version.
45    pub(crate) fn ver(&self) -> &uuid::Uuid {
46        &self.ver
47    }
48
49    /// Returns the document type.
50    pub(crate) fn doc_type(&self) -> &uuid::Uuid {
51        &self.doc_type
52    }
53
54    /// Returns the document metadata.
55    pub(crate) fn metadata(&self) -> Option<&serde_json::Value> {
56        self.metadata.as_ref()
57    }
58
59    /// Returns all signed document fields for the event db queries
60    pub(crate) fn postgres_db_fields(&self) -> [&(dyn tokio_postgres::types::ToSql + Sync); 5] {
61        [
62            &self.id,
63            &self.ver,
64            &self.doc_type,
65            &self.authors,
66            &self.metadata,
67        ]
68    }
69
70    /// Creates a  `SignedDocBody` instance.
71    pub(crate) fn new(
72        id: uuid::Uuid, ver: uuid::Uuid, doc_type: uuid::Uuid, authors: Vec<String>,
73        metadata: Option<serde_json::Value>,
74    ) -> Self {
75        Self {
76            id,
77            ver,
78            doc_type,
79            authors,
80            metadata,
81        }
82    }
83
84    /// Loads a async stream of `SignedDocBody` from the event db.
85    pub(crate) async fn retrieve(
86        conditions: &DocsQueryFilter, query_limits: &QueryLimits,
87    ) -> anyhow::Result<impl Stream<Item = anyhow::Result<Self>>> {
88        let query_template = get_template(&FILTERED_SELECT_SIGNED_DOCS_TEMPLATE)?;
89        let query = query_template.render(serde_json::json!({
90            "conditions": conditions.to_string(),
91            "query_limits": query_limits.to_string(),
92        }))?;
93        let rows = EventDB::query_stream(&query, &[]).await?;
94        let docs = rows.map(|res_row| res_row.and_then(|row| SignedDocBody::from_row(&row)));
95        Ok(docs)
96    }
97
98    /// Loads a count of `SignedDocBody` from the event db.
99    pub(crate) async fn retrieve_count(condition: &DocsQueryFilter) -> anyhow::Result<i64> {
100        let query_template = get_template(&FILTERED_COUNT_SIGNED_DOCS_TEMPLATE)?;
101        let query = query_template.render(serde_json::json!({
102            "conditions": condition.to_string(),
103        }))?;
104        let row = EventDB::query_one(&query, &[]).await?;
105
106        match row.get(0) {
107            Some(count) => Ok(count),
108            None => Err(NotFoundError.into()),
109        }
110    }
111
112    /// Creates a  `SignedDocBody` from postgresql row object.
113    fn from_row(row: &tokio_postgres::Row) -> anyhow::Result<Self> {
114        let id = row.try_get("id")?;
115        let ver = row.try_get("ver")?;
116        let doc_type = row.try_get("type")?;
117        let authors = row.try_get("authors")?;
118        let metadata = row.try_get("metadata")?;
119        Ok(Self {
120            id,
121            ver,
122            doc_type,
123            authors,
124            metadata,
125        })
126    }
127}