From 9e9d01c1332db5130a271db9882cb37418dc3f9b Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 16 Mar 2021 13:00:38 +0100 Subject: feat: get all emails --- src/email/mod.rs | 2 ++ src/email/models.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/email/routes.rs | 26 ++++++++++++++++++++++++++ src/main.rs | 2 ++ 4 files changed, 68 insertions(+) create mode 100644 src/email/mod.rs create mode 100644 src/email/models.rs create mode 100644 src/email/routes.rs (limited to 'src') diff --git a/src/email/mod.rs b/src/email/mod.rs new file mode 100644 index 0000000..a0e1883 --- /dev/null +++ b/src/email/mod.rs @@ -0,0 +1,2 @@ +pub mod models; +pub mod routes; 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, 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::>(); + + Ok(emails) + } +} diff --git a/src/email/routes.rs b/src/email/routes.rs new file mode 100644 index 0000000..06b74fb --- /dev/null +++ b/src/email/routes.rs @@ -0,0 +1,26 @@ +use crate::config::AppState; +use crate::email::models::Email; +use crate::errors::AppErrorResponse; +use actix_web::{web, HttpResponse, Responder}; +use slog::info; + +/// Endpoint used for retrieve all emails +async fn index(state: web::Data) -> impl Responder { + let result = Email::find_all(state.pool.clone()).await; + info!(state.log, "GET /email/"); + + match result { + Ok(emails) => HttpResponse::Ok().json(emails), + _ => HttpResponse::BadRequest().json(AppErrorResponse { + detail: "Error trying to read all emails from database" + .to_string(), + }), + } +} + +pub fn config(cfg: &mut web::ServiceConfig) { + cfg.service( + web::scope("/email") + .service(web::resource("{_:/?}").route(web::get().to(index))), + ); +} diff --git a/src/main.rs b/src/main.rs index 2c5532a..63f48fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod db; mod errors; mod helpers; +mod email; mod repository; use actix_web::{middleware, App, HttpServer}; @@ -35,6 +36,7 @@ async fn main() -> std::io::Result<()> { }) .wrap(middleware::Logger::default()) .configure(repository::routes::config) + .configure(email::routes::config) }) .bind(format!("{}:{}", config.server.host, config.server.port))? .run() -- cgit v1.2.3-18-g5258