diff --git a/OliverBooth/Data/Blog/BlogContext.cs b/OliverBooth/Data/Blog/BlogContext.cs
index ff4c0a2..ed9f56f 100644
--- a/OliverBooth/Data/Blog/BlogContext.cs
+++ b/OliverBooth/Data/Blog/BlogContext.cs
@@ -25,12 +25,6 @@ internal sealed class BlogContext : DbContext
/// The collection of blog posts.
public DbSet BlogPosts { get; private set; } = null!;
- ///
- /// Gets the collection of templates in the database.
- ///
- /// The collection of templates.
- public DbSet Templates { get; private set; } = null!;
-
///
/// Gets the collection of users in the database.
///
@@ -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());
}
}
diff --git a/OliverBooth/Data/Blog/Configuration/TemplateConfiguration.cs b/OliverBooth/Data/Blog/Configuration/TemplateConfiguration.cs
deleted file mode 100644
index 9366eb7..0000000
--- a/OliverBooth/Data/Blog/Configuration/TemplateConfiguration.cs
+++ /dev/null
@@ -1,19 +0,0 @@
-using Microsoft.EntityFrameworkCore;
-using Microsoft.EntityFrameworkCore.Metadata.Builders;
-
-namespace OliverBooth.Data.Blog.Configuration;
-
-///
-/// Represents the configuration for the entity.
-///
-internal sealed class TemplateConfiguration : IEntityTypeConfiguration
-{
- public void Configure(EntityTypeBuilder builder)
- {
- builder.ToTable("Template");
- builder.HasKey(e => e.Name);
-
- builder.Property(e => e.Name).IsRequired();
- builder.Property(e => e.FormatString).IsRequired();
- }
-}
diff --git a/OliverBooth/Data/Blog/Template.cs b/OliverBooth/Data/Blog/Template.cs
deleted file mode 100644
index dbb611a..0000000
--- a/OliverBooth/Data/Blog/Template.cs
+++ /dev/null
@@ -1,74 +0,0 @@
-namespace OliverBooth.Data.Blog;
-
-///
-/// Represents a MediaWiki-style template.
-///
-public sealed class Template : ITemplate, IEquatable
-{
- ///
- public string FormatString { get; internal set; } = string.Empty;
-
- ///
- public string Name { get; private set; } = string.Empty;
-
- ///
- /// Returns a value indicating whether two instances of are equal.
- ///
- /// The first instance of to compare.
- /// The second instance of to compare.
- ///
- /// if and are equal; otherwise,
- /// .
- ///
- public static bool operator ==(Template? left, Template? right) => Equals(left, right);
-
- ///
- /// Returns a value indicating whether two instances of are not equal.
- ///
- /// The first instance of to compare.
- /// The second instance of to compare.
- ///
- /// if and are not equal; otherwise,
- /// .
- ///
- public static bool operator !=(Template? left, Template? right) => !(left == right);
-
- ///
- /// Returns a value indicating whether this instance of is equal to another
- /// instance.
- ///
- /// An instance to compare with this instance.
- ///
- /// if is equal to this instance; otherwise,
- /// .
- ///
- public bool Equals(Template? other)
- {
- if (ReferenceEquals(null, other)) return false;
- if (ReferenceEquals(this, other)) return true;
- return Name == other.Name;
- }
-
- ///
- /// Returns a value indicating whether this instance is equal to a specified object.
- ///
- /// An object to compare with this instance.
- ///
- /// if is an instance of and
- /// equals the value of this instance; otherwise, .
- ///
- public override bool Equals(object? obj)
- {
- return ReferenceEquals(this, obj) || obj is Template other && Equals(other);
- }
-
- ///
- /// Gets the hash code for this instance.
- ///
- /// The hash code.
- public override int GetHashCode()
- {
- // ReSharper disable once NonReadonlyMemberInGetHashCode
- return Name.GetHashCode();
- }
-}
diff --git a/OliverBooth/Data/ITemplate.cs b/OliverBooth/Data/Web/ITemplate.cs
similarity index 100%
rename from OliverBooth/Data/ITemplate.cs
rename to OliverBooth/Data/Web/ITemplate.cs
diff --git a/OliverBooth/Markdown/Template/TemplateRenderer.cs b/OliverBooth/Markdown/Template/TemplateRenderer.cs
index fea4ab3..9e70fb2 100644
--- a/OliverBooth/Markdown/Template/TemplateRenderer.cs
+++ b/OliverBooth/Markdown/Template/TemplateRenderer.cs
@@ -23,6 +23,6 @@ internal sealed class TemplateRenderer : HtmlObjectRenderer
///
protected override void Write(HtmlRenderer renderer, TemplateInline template)
{
- renderer.Write(_templateService.RenderTemplate(template));
+ renderer.Write(_templateService.RenderGlobalTemplate(template));
}
}
diff --git a/OliverBooth/Services/ITemplateService.cs b/OliverBooth/Services/ITemplateService.cs
index 68f9829..77e2e0d 100644
--- a/OliverBooth/Services/ITemplateService.cs
+++ b/OliverBooth/Services/ITemplateService.cs
@@ -1,3 +1,4 @@
+using OliverBooth.Data;
using OliverBooth.Markdown.Template;
namespace OliverBooth.Services;
@@ -8,12 +9,23 @@ namespace OliverBooth.Services;
public interface ITemplateService
{
///
- /// Renders the specified template with the specified arguments.
+ /// Renders the specified global template with the specified arguments.
///
- /// The template to render.
- /// The rendered template.
+ /// The global template to render.
+ /// The rendered global template.
///
/// is .
///
- string RenderTemplate(TemplateInline templateInline);
+ string RenderGlobalTemplate(TemplateInline templateInline);
+
+ ///
+ /// Renders the specified global template with the specified arguments.
+ ///
+ /// The global template to render.
+ /// The database template object.
+ /// The rendered global template.
+ ///
+ /// is .
+ ///
+ string RenderTemplate(TemplateInline templateInline, ITemplate? template);
}
diff --git a/OliverBooth/Services/TemplateService.cs b/OliverBooth/Services/TemplateService.cs
index 474df8a..d8668d6 100644
--- a/OliverBooth/Services/TemplateService.cs
+++ b/OliverBooth/Services/TemplateService.cs
@@ -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 _webContextFactory;
+ private readonly IDbContextFactory _webContextFactory;
private readonly SmartFormatter _formatter;
///
/// Initializes a new instance of the class.
///
/// The .
- /// The factory.
- public TemplateService(IServiceProvider serviceProvider, IDbContextFactory webContextFactory)
+ /// The factory.
+ public TemplateService(IServiceProvider serviceProvider,
+ IDbContextFactory webContextFactory)
{
_formatter = Smart.CreateDefaultSmartFormat();
_formatter.AddExtensions(new DefaultSource());
@@ -34,15 +36,21 @@ internal sealed class TemplateService : ITemplateService
}
///
- 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);
+ }
+
+ ///
+ public string RenderTemplate(TemplateInline inline, ITemplate? template)
+ {
if (template is null)
{
- return $"{{{{{templateInline.Name}}}}}";
+ return $"{{{{{inline.Name}}}}}";
}
Span 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}}}}}";
}
}
}