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>
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>
/// Gets the collection of users in the database.
/// </summary>
@ -49,7 +43,6 @@ internal sealed class BlogContext : DbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new BlogPostConfiguration());
modelBuilder.ApplyConfiguration(new TemplateConfiguration());
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 />
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;
namespace OliverBooth.Services;
@ -8,12 +9,23 @@ namespace OliverBooth.Services;
public interface ITemplateService
{
/// <summary>
/// Renders the specified template with the specified arguments.
/// Renders the specified global template with the specified arguments.
/// </summary>
/// <param name="templateInline">The template to render.</param>
/// <returns>The rendered template.</returns>
/// <param name="templateInline">The global template to render.</param>
/// <returns>The rendered global template.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="templateInline" /> is <see langword="null" />.
/// </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 Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Blog;
using OliverBooth.Data;
using OliverBooth.Data.Web;
using OliverBooth.Formatting;
using OliverBooth.Markdown.Template;
using SmartFormat;
@ -14,15 +15,16 @@ namespace OliverBooth.Services;
internal sealed class TemplateService : ITemplateService
{
private static readonly Random Random = new();
private readonly IDbContextFactory<BlogContext> _webContextFactory;
private readonly IDbContextFactory<WebContext> _webContextFactory;
private readonly SmartFormatter _formatter;
/// <summary>
/// Initializes a new instance of the <see cref="TemplateService" /> class.
/// </summary>
/// <param name="serviceProvider">The <see cref="IServiceProvider" />.</param>
/// <param name="webContextFactory">The <see cref="BlogContext" /> factory.</param>
public TemplateService(IServiceProvider serviceProvider, IDbContextFactory<BlogContext> webContextFactory)
/// <param name="webContextFactory">The <see cref="WebContext" /> factory.</param>
public TemplateService(IServiceProvider serviceProvider,
IDbContextFactory<WebContext> webContextFactory)
{
_formatter = Smart.CreateDefaultSmartFormat();
_formatter.AddExtensions(new DefaultSource());
@ -34,15 +36,21 @@ internal sealed class TemplateService : ITemplateService
}
/// <inheritdoc />
public string RenderTemplate(TemplateInline templateInline)
public string RenderGlobalTemplate(TemplateInline templateInline)
{
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
using BlogContext webContext = _webContextFactory.CreateDbContext();
Template? template = webContext.Templates.Find(templateInline.Name);
using WebContext context = _webContextFactory.CreateDbContext();
Template? template = context.Templates.Find(templateInline.Name);
return RenderTemplate(templateInline, template);
}
/// <inheritdoc />
public string RenderTemplate(TemplateInline inline, ITemplate? template)
{
if (template is null)
{
return $"{{{{{templateInline.Name}}}}}";
return $"{{{{{inline.Name}}}}}";
}
Span<byte> randomBytes = stackalloc byte[20];
@ -50,9 +58,9 @@ internal sealed class TemplateService : ITemplateService
var formatted = new
{
templateInline.ArgumentList,
templateInline.ArgumentString,
templateInline.Params,
inline.ArgumentList,
inline.ArgumentString,
inline.Params,
RandomInt = BinaryPrimitives.ReadInt32LittleEndian(randomBytes[..4]),
RandomGuid = new Guid(randomBytes[4..]).ToString("N"),
};
@ -63,7 +71,7 @@ internal sealed class TemplateService : ITemplateService
}
catch
{
return $"{{{{{templateInline.Name}|{templateInline.ArgumentString}}}}}";
return $"{{{{{inline.Name}|{inline.ArgumentString}}}}}";
}
}
}