diff options
Diffstat (limited to 'src/email/routes.rs')
-rw-r--r-- | src/email/routes.rs | 55 |
1 files changed, 52 insertions, 3 deletions
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)), + ), ); } |