feat: db: get form submissions

This commit is contained in:
Aravinth Manivannan 2022-12-29 01:24:44 +05:30
parent ced9a7b55f
commit 777cea74e3
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 46 additions and 42 deletions

View file

@ -1,42 +1,3 @@
{
"db": "PostgreSQL",
"d104afb356962d24a0f950243db587a5bebeeb43edcac36da7eebe0ed3b27f5e": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Varchar",
"Jsonb",
"Timestamptz",
"Text"
]
}
},
"query": "\n INSERT INTO forms_submissions (website_path, value, time, website_id)\n VALUES ($1, $2, $3, (SELECT ID from forms_websites WHERE hostname = $4));\n "
},
"d184df863185d97345d5de3b80823312053a7a38316fd8b3c8fdd32d9a29644a": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Varchar"
]
}
},
"query": "INSERT INTO forms_websites (hostname) VALUES ($1) ON CONFLICT DO NOTHING;"
},
"d548953611b4fccb07ef22cb185deac69e4feb79864fcc98f2dc868298587315": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Text"
]
}
},
"query": "DELETE FROM forms_websites WHERE hostname = ($1)"
}
"db": "PostgreSQL"
}

View file

@ -115,6 +115,35 @@ impl Database {
Ok(())
}
pub async fn get_form_submissions(
&self,
page: usize,
host: &str,
path: &str,
) -> ServiceResult<Vec<FormSubmission>> {
let res = sqlx::query_as!(
FormSubmission,
"
SELECT
value, time, ID
FROM
forms_submissions
WHERE
website_id = (SELECT ID from forms_websites WHERE hostname = $1)
AND
website_path = $2
LIMIT 50 OFFSET $3
",
host,
path,
page as i32
)
.fetch_all(&self.pool)
.await
.unwrap();
Ok(res)
}
pub async fn add_form_submission(
&self,
data: &serde_json::Value,
@ -144,6 +173,13 @@ impl Database {
}
}
#[derive(Clone, Debug)]
pub struct FormSubmission {
pub id: i32,
pub value: Option<serde_json::Value>,
pub time: OffsetDateTime,
}
fn now_unix_time_stamp() -> OffsetDateTime {
OffsetDateTime::now_utc()
}
@ -179,7 +215,7 @@ mod tests {
.unwrap();
assert!(db.ping().await);
let urls = ["example.com", "example.com", "example.com", "example.net"];
let urls = ["example.com", "example.com", "example.net"];
for url in urls.iter() {
db.delete_site(url).await.unwrap();
// ensuring delete doesn't fail when record doesn't exist
@ -189,9 +225,16 @@ mod tests {
// ensuring add_site doesn't fail when record exists
db.add_site(url).await.unwrap();
db.add_form_submission(&serde_json::Value::default(), url, "/foo")
let path = "/foo";
db.add_form_submission(&serde_json::Value::default(), url, path)
.await
.unwrap();
let subs = db.get_form_submissions(0, url, path).await.unwrap();
assert_eq!(
&serde_json::Value::default(),
subs[0].value.as_ref().unwrap()
);
}
}
}