fixed ArticleView throwing exception instead of article not found notice
This commit is contained in:
parent
d869ec6688
commit
6beb81675d
|
@ -8,7 +8,7 @@
|
||||||
@inject NavigationManager Navigation
|
@inject NavigationManager Navigation
|
||||||
@inject IStringLocalizer<ArticleView> Localizer
|
@inject IStringLocalizer<ArticleView> Localizer
|
||||||
|
|
||||||
<PageTitle>@(TitlePrefix + Article.Title)</PageTitle>
|
<PageTitle>@(TitlePrefix + (Article?.Title ?? @Localizer["NotFound_Title"]))</PageTitle>
|
||||||
|
|
||||||
<ErrorBoundary>
|
<ErrorBoundary>
|
||||||
<ChildContent>
|
<ChildContent>
|
||||||
|
@ -51,9 +51,10 @@
|
||||||
|
|
||||||
[Parameter]
|
[Parameter]
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
private Article Article { get; set; } = default!;
|
private Article? Article { get; set; } = default!;
|
||||||
|
|
||||||
private Article GetArticlePublic() {
|
private Article GetArticlePublic() {
|
||||||
|
if (Article is null) throw new ApplicationException("Article not found.");
|
||||||
if (Article.Status >= ArticleStatus.Published && Article.PublishDate <= DateTimeOffset.UtcNow) {
|
if (Article.Status >= ArticleStatus.Published && Article.PublishDate <= DateTimeOffset.UtcNow) {
|
||||||
return Article;
|
return Article;
|
||||||
}
|
}
|
||||||
|
@ -62,6 +63,8 @@
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "ConvertIfStatementToSwitchStatement")]
|
[SuppressMessage("ReSharper", "ConvertIfStatementToSwitchStatement")]
|
||||||
private Article GetArticleProtected(ClaimsPrincipal principal) {
|
private Article GetArticleProtected(ClaimsPrincipal principal) {
|
||||||
|
if (Article is null) throw new ApplicationException("Article not found.");
|
||||||
|
|
||||||
// Admins always get access
|
// Admins always get access
|
||||||
if (principal.IsInRole("Admin")) {
|
if (principal.IsInRole("Admin")) {
|
||||||
return Article;
|
return Article;
|
||||||
|
@ -88,19 +91,16 @@
|
||||||
protected override void OnInitialized() {
|
protected override void OnInitialized() {
|
||||||
// We need blocking calls here, bc otherwise Blazor will execute Render in parallel,
|
// We need blocking calls here, bc otherwise Blazor will execute Render in parallel,
|
||||||
// running into a null pointer on the Article property and panicking
|
// running into a null pointer on the Article property and panicking
|
||||||
|
|
||||||
using var context = ContextFactory.CreateDbContext();
|
using var context = ContextFactory.CreateDbContext();
|
||||||
Article = context.Set<Article>()
|
Article = context.Set<Article>()
|
||||||
.Include(a => a.Author)
|
.Include(a => a.Author)
|
||||||
.Include(a => a.Reviewer)
|
.Include(a => a.Reviewer)
|
||||||
.First(a => a.Id == Id);
|
.FirstOrDefault(a => a.Id == Id);
|
||||||
|
|
||||||
if (Article is null) throw new ApplicationException("Article not found.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SubmitForReview() {
|
private async Task SubmitForReview() {
|
||||||
await using var context = await ContextFactory.CreateDbContextAsync();
|
await using var context = await ContextFactory.CreateDbContextAsync();
|
||||||
Article.Status = ArticleStatus.InReview;
|
Article!.Status = ArticleStatus.InReview;
|
||||||
context.Update(Article);
|
context.Update(Article);
|
||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
Navigation.NavigateTo("/");
|
Navigation.NavigateTo("/");
|
||||||
|
@ -108,7 +108,7 @@
|
||||||
|
|
||||||
private async Task SubmitForPublish() {
|
private async Task SubmitForPublish() {
|
||||||
await using var context = await ContextFactory.CreateDbContextAsync();
|
await using var context = await ContextFactory.CreateDbContextAsync();
|
||||||
Article.Status = ArticleStatus.Published;
|
Article!.Status = ArticleStatus.Published;
|
||||||
context.Update(Article);
|
context.Update(Article);
|
||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
Navigation.NavigateTo("/");
|
Navigation.NavigateTo("/");
|
||||||
|
|
Loading…
Reference in a new issue