mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-15 21:21:00 +00:00
Refactor subscribe.
This commit is contained in:
parent
bdddc29ac1
commit
3f3c73a93e
1 changed files with 20 additions and 15 deletions
|
@ -43,24 +43,29 @@ pub async fn subscribe(
|
|||
.await
|
||||
.map_err(|_| HttpResponse::InternalServerError().finish())?;
|
||||
// We are swallowing the error for the time being.
|
||||
let confirmation_link = "https://my-api.com/subscriptions/confirm";
|
||||
let _ = email_client
|
||||
.send_email(
|
||||
new_subscriber.email,
|
||||
"Welcome!",
|
||||
&format!(
|
||||
"Welcome to our newsletter!<br />Click <a href=\"{}\">here</a> to confirm your subscription.",
|
||||
confirmation_link
|
||||
),
|
||||
&format!(
|
||||
"Welcome to our newsletter!\nVisit {} to confirm your subscription.",
|
||||
confirmation_link
|
||||
),
|
||||
)
|
||||
.await;
|
||||
let _ = send_confirmation_email(&email_client).await;
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
#[tracing::instrument(
|
||||
name = "Send a confirmation email to a new subscriber",
|
||||
skip(new_subscriber, pool)
|
||||
)]
|
||||
pub async fn send_confirmation_email(email_client: &EmailClient) -> Result<(), reqwest::Error> {
|
||||
let confirmation_link = "https://my-api.com/subscriptions/confirm";
|
||||
let plain_body = format!(
|
||||
"Welcome to our newsletter!\nVisit {} to confirm your subscription.",
|
||||
confirmation_link
|
||||
);
|
||||
let html_body = format!(
|
||||
"Welcome to our newsletter!<br />Click <a href=\"{}\">here</a> to confirm your subscription.",
|
||||
confirmation_link
|
||||
);
|
||||
email_client
|
||||
.send_email(new_subscriber.email, "Welcome!", &html_body, &plain_body)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tracing::instrument(
|
||||
name = "Saving new subscriber details in the database",
|
||||
skip(new_subscriber, pool)
|
||||
|
|
Loading…
Reference in a new issue