Added automatic newsletter distribution to articles on publish

This commit is contained in:
Mia Rose Winter 2024-02-14 14:43:26 +01:00
parent 9cf97fbef7
commit a94852298d
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
2 changed files with 446 additions and 423 deletions

View file

@ -8,6 +8,7 @@
@rendermode InteractiveServer @rendermode InteractiveServer
@attribute [Authorize(Policy = "ArticleEditOrReviewPermissions")] @attribute [Authorize(Policy = "ArticleEditOrReviewPermissions")]
@inject ILogger<ArticleEditor> Logger
@inject IDbContextFactory<ApplicationDbContext> ContextFactory @inject IDbContextFactory<ApplicationDbContext> ContextFactory
@inject NavigationManager Navigation @inject NavigationManager Navigation
@inject UserManager<ApplicationUser> UserManager @inject UserManager<ApplicationUser> UserManager
@ -313,10 +314,17 @@
} }
} }
var newsletter = await context.Set<EmailNewsletter>().FirstOrDefaultAsync(n => n.Article == article);
if (newsletter is not null) {
newsletter.DistributionDateTime = article.PublishDate;
}
await context.SaveChangesAsync(); await context.SaveChangesAsync();
Navigation.NavigateTo($"/article/{article.Id}"); Navigation.NavigateTo($"/article/{article.Id}");
} catch (Exception ex) { } catch (Exception ex) {
// TODO toast // TODO toast
Logger.LogError(ex, "Failed to save article.");
} finally { } finally {
Busy = false; Busy = false;
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);

View file

@ -6,6 +6,7 @@
@using System.Diagnostics.CodeAnalysis @using System.Diagnostics.CodeAnalysis
@using Microsoft.Extensions.Options @using Microsoft.Extensions.Options
@inject ILogger<ArticleView> Logger
@inject IDbContextFactory<ApplicationDbContext> ContextFactory @inject IDbContextFactory<ApplicationDbContext> ContextFactory
@inject NavigationManager Navigation @inject NavigationManager Navigation
@inject IOptions<Customization> Customizations @inject IOptions<Customization> Customizations
@ -181,6 +182,20 @@
} }
context.Update(Article); context.Update(Article);
await context.SaveChangesAsync(); await context.SaveChangesAsync();
if (Features.Value.EmailSubscriptions) {
try {
var newsletter = new EmailNewsletter {
Article = Article,
DistributionDateTime = Article.PublishDate
};
context.Add(newsletter);
await context.SaveChangesAsync();
} catch (Exception ex) {
Logger.LogError(ex, "Failed to schedule article {name} for E-Mail distribution.", Article.Title);
}
}
Navigation.NavigateTo("/"); Navigation.NavigateTo("/");
} }