Implemented Article deletion

This commit is contained in:
Mia Rose Winter 2024-03-12 12:08:23 +01:00
parent 08119f26e0
commit a530375669
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
14 changed files with 491 additions and 5 deletions

View file

@ -1,4 +1,5 @@
<div class="alert @GetClass shadow" role="alert"> @using System.Globalization
<div class="alert @GetClass shadow" role="alert">
<div> <div>
@* ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault *@ @* ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault *@
@switch (Type) { @switch (Type) {
@ -24,7 +25,7 @@
break; break;
} }
</div> </div>
<div class="w-full"> <div class="w-full hyphens-auto text-justify" lang="@CultureInfo.CurrentCulture">
@ChildContent @ChildContent
</div> </div>
@if (CanRemove) { @if (CanRemove) {

View file

@ -1,4 +1,5 @@
@using Wave.Data @using Wave.Data
@using Humanizer
<article class="card card-side card-compact bg-base-200 text-base-content"> <article class="card card-side card-compact bg-base-200 text-base-content">
<figure class="shrink-0 max-md:!hidden sm:max-w-40"> <figure class="shrink-0 max-md:!hidden sm:max-w-40">
@ -9,12 +10,21 @@
<div class="card-body"> <div class="card-body">
<ArticleLink Article="Article"> <ArticleLink Article="Article">
<h2 class="card-title line-clamp-1">@Article.Title</h2> <h2 class="card-title line-clamp-1">@Article.Title</h2>
<small>@Article.PublishDate.ToString("d")</small> <small>
@Article.PublishDate.ToString("d")
@if (Article.Status is not ArticleStatus.Published) {
<span class="badge badge-sm badge-warning ml-2">@Article.Status.Humanize()</span>
}
</small>
<p class="@(Article.Categories.Count > 0 ? "line-clamp-2" : "line-clamp-4")"> <p class="@(Article.Categories.Count > 0 ? "line-clamp-2" : "line-clamp-4")">
@Article.BodyPlain[..Math.Min(1000, Article.BodyPlain.Length)] @Article.BodyPlain[..Math.Min(1000, Article.BodyPlain.Length)]
</p> </p>
</ArticleLink> </ArticleLink>
@if (Article.Categories.Count > 0) { @if (Action is not null) {
<div class="card-actions w-full">
@Action(Article)
</div>
} else if (Article.Categories.Count > 0) {
<div class="card-actions flex flex-wrap gap-2"> <div class="card-actions flex flex-wrap gap-2">
@foreach (var category in Article.Categories.OrderBy(c => c.Color)) { @foreach (var category in Article.Categories.OrderBy(c => c.Color)) {
<CategoryBadgeComponent Category="category" /> <CategoryBadgeComponent Category="category" />
@ -27,4 +37,6 @@
@code { @code {
[Parameter] [Parameter]
public required Article Article { get; set; } public required Article Article { get; set; }
[Parameter]
public RenderFragment<Article>? Action { get; set; }
} }

View file

@ -5,7 +5,7 @@
} else { } else {
<div class="flex flex-col gap-4"> <div class="flex flex-col gap-4">
@foreach (var article in Articles.OrderByDescending(a => a.PublishDate)) { @foreach (var article in Articles.OrderByDescending(a => a.PublishDate)) {
<ArticleCard Article="article" /> <ArticleCard Article="article" Action="Action" />
} }
</div> </div>
} }
@ -13,4 +13,6 @@
@code { @code {
[Parameter] [Parameter]
public required IList<Article> Articles { get; set; } = []; public required IList<Article> Articles { get; set; } = [];
[Parameter]
public RenderFragment<Article>? Action { get; set; }
} }

View file

@ -24,6 +24,7 @@
<AuthorizeView Policy="ArticleDeletePermissions"> <AuthorizeView Policy="ArticleDeletePermissions">
<Authorized> <Authorized>
<li><NavLink href="future">@Localizer["Future_Label"]</NavLink></li> <li><NavLink href="future">@Localizer["Future_Label"]</NavLink></li>
<li><NavLink href="deleted">@Localizer["Deleted_Label"]</NavLink></li>
</Authorized> </Authorized>
</AuthorizeView> </AuthorizeView>
<AuthorizeView Policy="CategoryManagePermissions"> <AuthorizeView Policy="CategoryManagePermissions">

View file

@ -0,0 +1,62 @@
@page "/article/{id:guid}/delete"
@using Microsoft.EntityFrameworkCore
@using Wave.Data
@attribute [Authorize(Policy = "ArticleDeletePermissions")]
@inject IDbContextFactory<ApplicationDbContext> ContextFactory
@inject NavigationManager Navigation
@inject ILogger<ArticleDeleteConfirm> Logger
@inject IStringLocalizer<ArticleView> Localizer
<PageTitle>@(TitlePrefix + (Article is not null ? Localizer["Delete_Title"] : Localizer["NotFound_Title"]))</PageTitle>
@if (Article is not null) {
<BoardComponent CenterContent="true">
<BoardCardComponent Heading="@Localizer["Delete_Title"]">
<Alert CanRemove="false" Type="Alert.MessageType.Warning">
<strong>@Localizer["Delete_Warning"]</strong>
</Alert>
<p class="my-3">@Article.Title</p>
<form @formname="delete" method="post" @onsubmit="Delete">
<AntiforgeryToken />
<button type="submit" class="btn btn-error w-full">
@Localizer["Delete_Confirm"]
</button>
</form>
</BoardCardComponent>
</BoardComponent>
} else {
<h1 class="text-3xl lg:text-5xl font-light mb-6 text-primary">@Localizer["NotFound_Title"]</h1>
<p class="my-3">@Localizer["NotFound_Description"]</p>
<a class="btn btn-primary" href="/">@Localizer["NotFound_BackToHome_Label"]</a>
}
@code {
[CascadingParameter(Name = "TitlePrefix")]
private string TitlePrefix { get; set; } = default!;
[Parameter]
public Guid Id { get; set; }
private Article? Article { get; set; }
protected override async Task OnInitializedAsync() {
await using var context = await ContextFactory.CreateDbContextAsync();
Article = await context.Set<Article>().IgnoreQueryFilters()
.Where(a => !a.IsDeleted).FirstOrDefaultAsync(a => a.Id == Id);
}
private async Task Delete() {
if (Article is null) return;
var context = await ContextFactory.CreateDbContextAsync();
Article.IsDeleted = true;
context.Entry(Article).State = EntityState.Modified;
await context.SaveChangesAsync();
Navigation.NavigateTo("/");
}
}

View file

@ -78,6 +78,11 @@
} }
</form> </form>
} }
<AuthorizeView Policy="ArticleDeletePermissions" Context="_">
<a class="btn btn-error w-full sm:btn-wide" href="/article/@Article.Id/delete">
@Localizer["Delete_Submit"]
</a>
</AuthorizeView>
</div> </div>
</Authorized> </Authorized>
<NotAuthorized> <NotAuthorized>

View file

@ -0,0 +1,58 @@
@page "/deleted"
@using Microsoft.EntityFrameworkCore
@using Wave.Data
@attribute [Authorize(Policy = "ArticleDeletePermissions")]
@inject ILogger<Deleted> Logger
@inject IDbContextFactory<ApplicationDbContext> ContextFactory
@inject IStringLocalizer<Deleted> Localizer
<PageTitle>@(TitlePrefix + Localizer["Title"])</PageTitle>
<h1 class="text-3xl lg:text-5xl font-light mb-6 text-primary">@Localizer["Title"]</h1>
<ArticleCardList Articles="Articles">
<Action Context="article">
<form method="post" @formname="@article.Id.ToString()" @onsubmit="Restore" class="w-full">
<AntiforgeryToken />
<input type="hidden" name="id" value="@article.Id"/>
<button type="submit" class="btn btn-sm btn-wide btn-primary max-sm:w-full">@Localizer["Restore_Submit"]</button>
</form>
</Action>
</ArticleCardList>
@code {
[CascadingParameter(Name = "TitlePrefix")]
private string TitlePrefix { get; set; } = default!;
private List<Article> Articles { get; } = [];
[SupplyParameterFromForm(Name = "id")]
private Guid Id { get; set; }
protected override async Task OnInitializedAsync() {
await using var context = await ContextFactory.CreateDbContextAsync();
Articles.AddRange(await context.Set<Article>()
.IgnoreQueryFilters().IgnoreAutoIncludes()
.Include(a => a.Author)
.Where(a => a.IsDeleted)
.OrderByDescending(a => a.PublishDate).ThenBy(a => a.Id)
.ToListAsync());
}
private async Task Restore() {
await using var context = await ContextFactory.CreateDbContextAsync();
var article = await context.Set<Article>()
.IgnoreQueryFilters().IgnoreAutoIncludes()
.FirstOrDefaultAsync(a => a.Id == Id);
if (article is null) throw new ApplicationException("Error restoring Article, not found.");
article.IsDeleted = false;
await context.SaveChangesAsync();
Articles.RemoveAt(Articles.FindIndex(a => a.Id == article.Id));
}
}

View file

@ -131,4 +131,7 @@
<data name="ManageApi_Label" xml:space="preserve"> <data name="ManageApi_Label" xml:space="preserve">
<value>API Verwalten</value> <value>API Verwalten</value>
</data> </data>
<data name="Deleted_Label" xml:space="preserve">
<value>Gelöscht</value>
</data>
</root> </root>

View file

@ -134,4 +134,7 @@
<data name="ManageApi_Label" xml:space="preserve"> <data name="ManageApi_Label" xml:space="preserve">
<value>Manage API</value> <value>Manage API</value>
</data> </data>
<data name="Deleted_Label" xml:space="preserve">
<value>Deleted</value>
</data>
</root> </root>

View file

@ -137,4 +137,16 @@
<data name="Publish_Silent_Label" xml:space="preserve"> <data name="Publish_Silent_Label" xml:space="preserve">
<value>Ohne E-Mail Veröffentlichen</value> <value>Ohne E-Mail Veröffentlichen</value>
</data> </data>
<data name="Delete_Submit" xml:space="preserve">
<value>Artikel löschen</value>
</data>
<data name="Delete_Title" xml:space="preserve">
<value>Löschen Bestätigen</value>
</data>
<data name="Delete_Confirm" xml:space="preserve">
<value>Ja, Artikel Löschen</value>
</data>
<data name="Delete_Warning" xml:space="preserve">
<value>Ihr Artikel wird runtergenommen. Bitte beachten Sie das Suchmaschinen eventuell noch links zu diesem Artikel haben, welche dann eine Fehlernachricht produzieren. Der Artikel ist womöglich noch in manchen Caches für die nächsten paar Stunden auffindbar. </value>
</data>
</root> </root>

View file

@ -137,4 +137,16 @@
<data name="Publish_Silent_Label" xml:space="preserve"> <data name="Publish_Silent_Label" xml:space="preserve">
<value>Publish without Email</value> <value>Publish without Email</value>
</data> </data>
<data name="Delete_Submit" xml:space="preserve">
<value>Delete Article</value>
</data>
<data name="Delete_Title" xml:space="preserve">
<value>Confirm Delete</value>
</data>
<data name="Delete_Confirm" xml:space="preserve">
<value>Yes, Delete Article</value>
</data>
<data name="Delete_Warning" xml:space="preserve">
<value>This will take down your Article. Please keep in mind that search engines may still have links to this article, which will produce error messages. The Article may still be accessable in some caches the next couple of hours.</value>
</data>
</root> </root>

View file

@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 1.3
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">1.3</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1">this is my long string</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
[base64 mime encoded serialized .NET Framework object]
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
[base64 mime encoded string representing a byte array form of the .NET Framework object]
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Title" xml:space="preserve">
<value>Gelöschte Artikel</value>
</data>
<data name="Restore_Submit" xml:space="preserve">
<value>Artikel Wiederherstellen</value>
</data>
</root>

View file

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 1.3
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">1.3</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1">this is my long string</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
[base64 mime encoded serialized .NET Framework object]
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
[base64 mime encoded string representing a byte array form of the .NET Framework object]
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View file

@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 1.3
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">1.3</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1">this is my long string</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
[base64 mime encoded serialized .NET Framework object]
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
[base64 mime encoded string representing a byte array form of the .NET Framework object]
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Title" xml:space="preserve">
<value>Deleted Articles</value>
</data>
<data name="Restore_Submit" xml:space="preserve">
<value>Restore Article</value>
</data>
</root>