feat(api): email-testing and refactored subscription code
This commit is contained in:
parent
fda6c7c044
commit
23b089bf3d
@ -22,7 +22,6 @@ impl AsRef<str> for SubscriberEmail {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::domain::subscriber_email::SubscriberEmail;
|
||||
use claims::assert_err;
|
||||
use fake::faker::internet::en::SafeEmail;
|
||||
use fake::Fake;
|
||||
|
||||
|
@ -10,6 +10,16 @@ pub struct FormData {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl TryFrom<FormData> for NewSubscriber {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: FormData) -> Result<Self, Self::Error> {
|
||||
let name = SubscriberName::parse(value.name)?;
|
||||
let email = SubscriberEmail::parse(value.email)?;
|
||||
Ok(Self { email, name })
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(
|
||||
name = "Adding a new subscriber",
|
||||
// functions args isn't really relevant to the span
|
||||
@ -23,18 +33,10 @@ pub async fn subscribe_route(
|
||||
form: web::Form<FormData>,
|
||||
db_conn_pool: web::Data<PgPool>,
|
||||
) -> HttpResponse {
|
||||
let name = match SubscriberName::parse(form.0.name) {
|
||||
Ok(name) => name,
|
||||
let new_subscriber = match form.0.try_into() {
|
||||
Ok(form) => form,
|
||||
Err(_) => return HttpResponse::BadRequest().finish(),
|
||||
};
|
||||
|
||||
let email = match SubscriberEmail::parse(form.0.email) {
|
||||
Ok(email) => email,
|
||||
Err(_) => return HttpResponse::BadRequest().finish(),
|
||||
};
|
||||
|
||||
let new_subscriber = NewSubscriber { email, name };
|
||||
|
||||
match insert_subscriber(&db_conn_pool, &new_subscriber).await {
|
||||
Ok(_) => HttpResponse::Ok().finish(),
|
||||
Err(_) => HttpResponse::InternalServerError().finish(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user