private async Task <Result <EmailTemplate> > FindTemplate(EmailTemplateKeys template) { var templateId = (int)template; var templateEntity = await db.EmailTemplates.SingleOrDefaultAsync(t => t.Id == templateId) ?? Maybe <EmailTemplate> .None; return(templateEntity.HasNoValue ? Result.Failure <EmailTemplate>($"The template with this identifier was not found: {(int)template}") : Result.Ok(templateEntity.Value)); }
public async Task <Result> Send <T>(EmailTemplateKeys templateId, IEnumerable <string> recipientAddresses, T messageData) { var mailingList = recipientAddresses as string[] ?? recipientAddresses.ToArray(); if (!mailingList.Any()) { return(Result.Failure("No recipient addresses provided")); } var template = await FindTemplate(templateId); var templateData = GetTemplateData(messageData); return(await Result.Combine(template, templateData) .Bind(() => BuildMessage(template.Value, templateData.Value)) .Bind(async(message) => await SendMessage(template.Value, message, mailingList)) .OnFailure(LogError)); }
public async Task <Result> Send <T>(EmailTemplateKeys templateId, string recipientAddress, T messageData) => await Send(templateId, new[] { recipientAddress }, messageData);