diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/email/models.rs | 75 | ||||
-rw-r--r-- | src/email/routes.rs | 55 |
2 files changed, 126 insertions, 4 deletions
diff --git a/src/email/models.rs b/src/email/models.rs index 477092d..ed46026 100644 --- a/src/email/models.rs +++ b/src/email/models.rs @@ -1,11 +1,14 @@ use crate::db::get_client; -use crate::errors::AppError; +use crate::errors::{AppError, AppErrorType}; use deadpool_postgres::Pool; use serde::{Deserialize, Serialize}; use tokio_pg_mapper::FromTokioPostgresRow; use tokio_pg_mapper_derive::PostgresMapper; +use hex; +use md5::{Digest, Md5}; + #[derive(Serialize, Deserialize, PostgresMapper)] #[pg_mapper(table = "email")] /// Emails model @@ -35,4 +38,74 @@ impl Email { Ok(emails) } + + /// Search an email + pub async fn search( + pool: Pool, + email: &String, + ) -> Result<Email, AppError> { + let client = get_client(pool.clone()).await.unwrap(); + + let statement = + client.prepare("SELECT * FROM email WHERE email=$1").await?; + + let email = client + .query_opt(&statement, &[&email]) + .await? + .map(|row| Email::from_row_ref(&row).unwrap()); + + match email { + Some(email) => Ok(email), + None => Err(AppError { + error_type: AppErrorType::NotFoundError, + cause: None, + message: Some("Email not found".to_string()), + }), + } + } + + /// Create new email + pub async fn create( + pool: Pool, + data: &EmailData, + ) -> Result<Email, AppError> { + // Search an email that matches with that string, because if it's + // exists, the server cannot create a clone + match Email::search(pool.clone(), &data.email).await { + Ok(_) => { + return Err(AppError { + message: Some("Email already exists".to_string()), + cause: Some("".to_string()), + error_type: AppErrorType::AuthorizationError, + }); + } + Err(_) => {} + }; + + let client = get_client(pool.clone()).await.unwrap(); + + let mut hasher = Md5::new(); + hasher.update(&data.email.as_bytes()); + let hash_final = hasher.finalize(); + + let digest = hex::encode(&hash_final.as_slice()); + + let statement = client + .prepare("INSERT INTO email VALUES ($1, $2) RETURNING *") + .await?; + + let email = client + .query_opt(&statement, &[&data.email, &digest]) + .await? + .map(|row| Email::from_row_ref(&row).unwrap()); + + match email { + Some(email) => Ok(email), + None => Err(AppError { + message: Some("Error creating a new email".to_string()), + cause: Some("Unknown error".to_string()), + error_type: AppErrorType::DbError, + }), + } + } } diff --git a/src/email/routes.rs b/src/email/routes.rs index 06b74fb..14299eb 100644 --- a/src/email/routes.rs +++ b/src/email/routes.rs @@ -1,7 +1,9 @@ +use std::collections::HashMap; + use crate::config::AppState; -use crate::email::models::Email; +use crate::email::models::{Email, EmailData}; use crate::errors::AppErrorResponse; -use actix_web::{web, HttpResponse, Responder}; +use actix_web::{web, HttpRequest, HttpResponse, Responder}; use slog::info; /// Endpoint used for retrieve all emails @@ -18,9 +20,56 @@ async fn index(state: web::Data<AppState>) -> impl Responder { } } +// Endpoint used for create new email +async fn create_email( + payload: web::Json<EmailData>, + state: web::Data<AppState>, +) -> impl Responder { + info!(state.log, "POST /email/"); + let result = Email::create(state.pool.clone(), &payload).await; + + result + .map(|email| HttpResponse::Created().json(email)) + .map_err(|e| e) +} + +// Endpoint used for email search +async fn search_email( + req: HttpRequest, + state: web::Data<AppState>, +) -> impl Responder { + let query = + web::Query::<HashMap<String, String>>::from_query(req.query_string()) + .unwrap(); + let email = match query.get("q") { + Some(x) => x, + None => { + return HttpResponse::NotFound().json(AppErrorResponse { + detail: "No email found".to_string(), + }); + } + }; + let result = Email::search(state.pool.clone(), email).await; + info!(state.log, "GET /email/search?q={}", email); + + match result { + Ok(email) => HttpResponse::Ok().json(email), + _ => HttpResponse::NotFound().json(AppErrorResponse { + detail: "No email found".to_string(), + }), + } +} pub fn config(cfg: &mut web::ServiceConfig) { cfg.service( web::scope("/email") - .service(web::resource("{_:/?}").route(web::get().to(index))), + .service( + web::resource("{_:/?}") + .route(web::get().to(index)) + .route(web::post().to(create_email)), + ) + .service( + web::resource("/search{_:/?}") + .route(web::get().to(search_email)), + ), ); } |