diff --git a/src/routes/newsletters.rs b/src/routes/newsletters.rs index 0b925a3..b62e25e 100644 --- a/src/routes/newsletters.rs +++ b/src/routes/newsletters.rs @@ -1,5 +1,7 @@ -use actix_web::{web, HttpResponse}; +use actix_web::{web, HttpResponse, ResponseError}; use sqlx::PgPool; +use crate::routes::error_chain_fmt; +use actix_web::http::StatusCode; #[derive(serde::Deserialize)] pub struct BodyData { @@ -13,8 +15,32 @@ pub struct Content { text: String, } -pub async fn publish_newsletter(_body: web::Json) -> HttpResponse { - HttpResponse::Ok().finish() +#[derive(thiserror::Error)] +pub enum PublishError { + #[error(transparent)] + UnexpectedError(#[from] anyhow::Error), +} + +impl std::fmt::Debug for PublishError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + error_chain_fmt(self, f) + } +} + +impl ResponseError for PublishError { + fn status_code(&self) -> StatusCode { + match self { + PublishError::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR, + } + } +} + +pub async fn publish_newsletter( + body: web::Json, + pool: web::Data, +) -> Result { + let subscribers = get_confirmed_subscribers(&pool).await?; + Ok(HttpResponse::Ok().finish()) } struct ConfirmedSubscriber {