End of chapter.

This commit is contained in:
LukeMathWalker 2021-08-01 14:57:02 +01:00
parent d7999e7241
commit d54f33858d
4 changed files with 38 additions and 18 deletions

View file

@ -19,6 +19,12 @@ impl AsRef<str> for SubscriberEmail {
}
}
impl std::fmt::Display for SubscriberEmail {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::SubscriberEmail;

View file

@ -24,7 +24,7 @@ impl EmailClient {
pub async fn send_email(
&self,
recipient: SubscriberEmail,
recipient: &SubscriberEmail,
subject: &str,
html_content: &str,
text_content: &str,
@ -124,7 +124,7 @@ mod tests {
// Act
let _ = email_client
.send_email(email(), &subject(), &content(), &content())
.send_email(&email(), &subject(), &content(), &content())
.await;
// Assert
@ -144,7 +144,7 @@ mod tests {
// Act
let outcome = email_client
.send_email(email(), &subject(), &content(), &content())
.send_email(&email(), &subject(), &content(), &content())
.await;
// Assert
@ -166,7 +166,7 @@ mod tests {
// Act
let outcome = email_client
.send_email(email(), &subject(), &content(), &content())
.send_email(&email(), &subject(), &content(), &content())
.await;
// Assert
@ -188,7 +188,7 @@ mod tests {
// Act
let outcome = email_client
.send_email(email(), &subject(), &content(), &content())
.send_email(&email(), &subject(), &content(), &content())
.await;
// Assert

View file

@ -45,15 +45,28 @@ pub async fn publish_newsletter(
) -> Result<HttpResponse, PublishError> {
let subscribers = get_confirmed_subscribers(&pool).await?;
for subscriber in subscribers {
email_client
.send_email(
subscriber.email,
&body.title,
&body.content.html,
&body.content.text,
)
.await
.with_context(|| format!("Failed to send newsletter issue to {}", subscriber.email))?;
match subscriber {
Ok(subscriber) => {
email_client
.send_email(
&subscriber.email,
&body.title,
&body.content.html,
&body.content.text,
)
.await
.with_context(|| {
format!("Failed to send newsletter issue to {}", subscriber.email)
})?;
}
Err(error) => {
tracing::warn!(
error.cause_chain = ?error,
"Skipping a confirmed subscriber. \
Their stored contact details are invalid",
);
}
}
}
Ok(HttpResponse::Ok().finish())
}
@ -65,7 +78,7 @@ struct ConfirmedSubscriber {
#[tracing::instrument(name = "Adding a new subscriber", skip(pool))]
async fn get_confirmed_subscribers(
pool: &PgPool,
) -> Result<Vec<ConfirmedSubscriber>, anyhow::Error> {
) -> Result<Vec<Result<ConfirmedSubscriber, anyhow::Error>>, anyhow::Error> {
struct Row {
email: String,
}
@ -82,8 +95,9 @@ async fn get_confirmed_subscribers(
.await?;
let confirmed_subscribers = rows
.into_iter()
.map(|r| ConfirmedSubscriber {
email: SubscriberEmail::parse(r.email).unwrap(),
.map(|r| match SubscriberEmail::parse(r.email) {
Ok(email) => Ok(ConfirmedSubscriber { email }),
Err(error) => Err(anyhow::anyhow!(error)),
})
.collect();
Ok(confirmed_subscribers)

View file

@ -122,7 +122,7 @@ pub async fn send_confirmation_email(
confirmation_link
);
email_client
.send_email(new_subscriber.email, "Welcome!", &html_body, &plain_body)
.send_email(&new_subscriber.email, "Welcome!", &html_body, &plain_body)
.await
}