summaryrefslogtreecommitdiff
path: root/src/email/models.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-03-16 13:00:38 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-03-16 13:00:38 +0100
commit9e9d01c1332db5130a271db9882cb37418dc3f9b (patch)
tree94ea8bf29cb826d6335e6a3bb082c03fa6c9e48f /src/email/models.rs
parentb950072a3109d2c13881611a3950baa191caf097 (diff)
feat: get all emails
Diffstat (limited to 'src/email/models.rs')
-rw-r--r--src/email/models.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/email/models.rs b/src/email/models.rs
new file mode 100644
index 0000000..477092d
--- /dev/null
+++ b/src/email/models.rs
@@ -0,0 +1,38 @@
+use crate::db::get_client;
+use crate::errors::AppError;
+
+use deadpool_postgres::Pool;
+use serde::{Deserialize, Serialize};
+use tokio_pg_mapper::FromTokioPostgresRow;
+use tokio_pg_mapper_derive::PostgresMapper;
+
+#[derive(Serialize, Deserialize, PostgresMapper)]
+#[pg_mapper(table = "email")]
+/// Emails model
+pub struct Email {
+ pub email: String,
+ pub hash_md5: String,
+}
+
+// Struct used to creare a new email
+#[derive(Serialize, Deserialize)]
+pub struct EmailData {
+ pub email: String,
+}
+
+impl Email {
+ /// Find all emails, returns email and its MD5 hash
+ pub async fn find_all(pool: Pool) -> Result<Vec<Email>, AppError> {
+ let client = get_client(pool.clone()).await.unwrap();
+ let statement = client.prepare("SELECT * FROM email").await?;
+
+ let emails = client
+ .query(&statement, &[])
+ .await?
+ .iter()
+ .map(|row| Email::from_row_ref(row).unwrap())
+ .collect::<Vec<Email>>();
+
+ Ok(emails)
+ }
+}