refactor: use global templates

This commit is contained in:
Oliver Booth 2023-08-13 18:02:19 +01:00
parent 67d89c1831
commit 369436ccce
Signed by: oliverbooth
GPG Key ID: B89D139977693FED
7 changed files with 37 additions and 117 deletions

View File

@ -25,12 +25,6 @@ internal sealed class BlogContext : DbContext
/// <value>The collection of blog posts.</value> /// <value>The collection of blog posts.</value>
public DbSet<BlogPost> BlogPosts { get; private set; } = null!; public DbSet<BlogPost> BlogPosts { get; private set; } = null!;
/// <summary>
/// Gets the collection of templates in the database.
/// </summary>
/// <value>The collection of templates.</value>
public DbSet<Template> Templates { get; private set; } = null!;
/// <summary> /// <summary>
/// Gets the collection of users in the database. /// Gets the collection of users in the database.
/// </summary> /// </summary>
@ -49,7 +43,6 @@ internal sealed class BlogContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.ApplyConfiguration(new BlogPostConfiguration()); modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
modelBuilder.ApplyConfiguration(new TemplateConfiguration());
modelBuilder.ApplyConfiguration(new UserConfiguration()); modelBuilder.ApplyConfiguration(new UserConfiguration());
} }
} }

View File

@ -1,19 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace OliverBooth.Data.Blog.Configuration;
/// <summary>
/// Represents the configuration for the <see cref="Template" /> entity.
/// </summary>
internal sealed class TemplateConfiguration : IEntityTypeConfiguration<Template>
{
public void Configure(EntityTypeBuilder<Template> builder)
{
builder.ToTable("Template");
builder.HasKey(e => e.Name);
builder.Property(e => e.Name).IsRequired();
builder.Property(e => e.FormatString).IsRequired();
}
}

View File

@ -1,74 +0,0 @@
namespace OliverBooth.Data.Blog;
/// <summary>
/// Represents a MediaWiki-style template.
/// </summary>
public sealed class Template : ITemplate, IEquatable<Template>
{
/// <inheritdoc />
public string FormatString { get; internal set; } = string.Empty;
/// <inheritdoc />
public string Name { get; private set; } = string.Empty;
/// <summary>
/// Returns a value indicating whether two instances of <see cref="Template" /> are equal.
/// </summary>
/// <param name="left">The first instance of <see cref="Template" /> to compare.</param>
/// <param name="right">The second instance of <see cref="Template" /> to compare.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator ==(Template? left, Template? right) => Equals(left, right);
/// <summary>
/// Returns a value indicating whether two instances of <see cref="Template" /> are not equal.
/// </summary>
/// <param name="left">The first instance of <see cref="Template" /> to compare.</param>
/// <param name="right">The second instance of <see cref="Template" /> to compare.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="left" /> and <paramref name="right" /> are not equal; otherwise,
/// <see langword="false" />.
/// </returns>
public static bool operator !=(Template? left, Template? right) => !(left == right);
/// <summary>
/// Returns a value indicating whether this instance of <see cref="Template" /> is equal to another
/// instance.
/// </summary>
/// <param name="other">An instance to compare with this instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="other" /> is equal to this instance; otherwise,
/// <see langword="false" />.
/// </returns>
public bool Equals(Template? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Name == other.Name;
}
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>
/// <see langword="true" /> if <paramref name="obj" /> is an instance of <see cref="Template" /> and
/// equals the value of this instance; otherwise, <see langword="false" />.
/// </returns>
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || obj is Template other && Equals(other);
}
/// <summary>
/// Gets the hash code for this instance.
/// </summary>
/// <returns>The hash code.</returns>
public override int GetHashCode()
{
// ReSharper disable once NonReadonlyMemberInGetHashCode
return Name.GetHashCode();
}
}

View File

@ -23,6 +23,6 @@ internal sealed class TemplateRenderer : HtmlObjectRenderer<TemplateInline>
/// <inheritdoc /> /// <inheritdoc />
protected override void Write(HtmlRenderer renderer, TemplateInline template) protected override void Write(HtmlRenderer renderer, TemplateInline template)
{ {
renderer.Write(_templateService.RenderTemplate(template)); renderer.Write(_templateService.RenderGlobalTemplate(template));
} }
} }

View File

@ -1,3 +1,4 @@
using OliverBooth.Data;
using OliverBooth.Markdown.Template; using OliverBooth.Markdown.Template;
namespace OliverBooth.Services; namespace OliverBooth.Services;
@ -8,12 +9,23 @@ namespace OliverBooth.Services;
public interface ITemplateService public interface ITemplateService
{ {
/// <summary> /// <summary>
/// Renders the specified template with the specified arguments. /// Renders the specified global template with the specified arguments.
/// </summary> /// </summary>
/// <param name="templateInline">The template to render.</param> /// <param name="templateInline">The global template to render.</param>
/// <returns>The rendered template.</returns> /// <returns>The rendered global template.</returns>
/// <exception cref="ArgumentNullException"> /// <exception cref="ArgumentNullException">
/// <paramref name="templateInline" /> is <see langword="null" />. /// <paramref name="templateInline" /> is <see langword="null" />.
/// </exception> /// </exception>
string RenderTemplate(TemplateInline templateInline); string RenderGlobalTemplate(TemplateInline templateInline);
/// <summary>
/// Renders the specified global template with the specified arguments.
/// </summary>
/// <param name="templateInline">The global template to render.</param>
/// <param name="template">The database template object.</param>
/// <returns>The rendered global template.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="templateInline" /> is <see langword="null" />.
/// </exception>
string RenderTemplate(TemplateInline templateInline, ITemplate? template);
} }

View File

@ -1,6 +1,7 @@
using System.Buffers.Binary; using System.Buffers.Binary;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog; using OliverBooth.Data;
using OliverBooth.Data.Web;
using OliverBooth.Formatting; using OliverBooth.Formatting;
using OliverBooth.Markdown.Template; using OliverBooth.Markdown.Template;
using SmartFormat; using SmartFormat;
@ -14,15 +15,16 @@ namespace OliverBooth.Services;
internal sealed class TemplateService : ITemplateService internal sealed class TemplateService : ITemplateService
{ {
private static readonly Random Random = new(); private static readonly Random Random = new();
private readonly IDbContextFactory<BlogContext> _webContextFactory; private readonly IDbContextFactory<WebContext> _webContextFactory;
private readonly SmartFormatter _formatter; private readonly SmartFormatter _formatter;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="TemplateService" /> class. /// Initializes a new instance of the <see cref="TemplateService" /> class.
/// </summary> /// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param> /// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param>
/// <param name="webContextFactory">The <see cref="BlogContext" /> factory.</param> /// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param>
public TemplateService(IServiceProvider serviceProvider, IDbContextFactory<BlogContext> webContextFactory) public TemplateService(IServiceProvider serviceProvider,
IDbContextFactory<WebContext> webContextFactory)
{ {
_formatter = Smart.CreateDefaultSmartFormat(); _formatter = Smart.CreateDefaultSmartFormat();
_formatter.AddExtensions(new DefaultSource()); _formatter.AddExtensions(new DefaultSource());
@ -34,15 +36,21 @@ internal sealed class TemplateService : ITemplateService
} }
/// <inheritdoc /> /// <inheritdoc />
public string RenderTemplate(TemplateInline templateInline) public string RenderGlobalTemplate(TemplateInline templateInline)
{ {
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline)); if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
using BlogContext webContext = _webContextFactory.CreateDbContext(); using WebContext context = _webContextFactory.CreateDbContext();
Template? template = webContext.Templates.Find(templateInline.Name); Template? template = context.Templates.Find(templateInline.Name);
return RenderTemplate(templateInline, template);
}
/// <inheritdoc />
public string RenderTemplate(TemplateInline inline, ITemplate? template)
{
if (template is null) if (template is null)
{ {
return $"{{{{{templateInline.Name}}}}}"; return $"{{{{{inline.Name}}}}}";
} }
Span<byte> randomBytes = stackalloc byte[20]; Span<byte> randomBytes = stackalloc byte[20];
@ -50,9 +58,9 @@ internal sealed class TemplateService : ITemplateService
var formatted = new var formatted = new
{ {
templateInline.ArgumentList, inline.ArgumentList,
templateInline.ArgumentString, inline.ArgumentString,
templateInline.Params, inline.Params,
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]), RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
RandomGuid = new Guid(randomBytes[4..]).ToString("N"), RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
}; };
@ -63,7 +71,7 @@ internal sealed class TemplateService : ITemplateService
} }
catch catch
{ {
return $"{{{{{templateInline.Name}|{templateInline.ArgumentString}}}}}"; return $"{{{{{inline.Name}|{inline.ArgumentString}}}}}";
} }
} }
} }