Compare commits

..

No commits in common. "20eabeeb1e36c2c8030b16c74dba65f7bbc9fe57" and "1cdad4c17ce46c4530103dd898cfe4513c32c134" have entirely different histories.

9 changed files with 16 additions and 94 deletions

View File

@ -11,10 +11,9 @@ internal sealed class TemplateConfiguration : IEntityTypeConfiguration<Template>
public void Configure(EntityTypeBuilder<Template> builder) public void Configure(EntityTypeBuilder<Template> builder)
{ {
builder.ToTable("Template"); builder.ToTable("Template");
builder.HasKey(e => new { e.Name, e.Variant }); builder.HasKey(e => e.Name);
builder.Property(e => e.Name).HasMaxLength(50).IsRequired(); builder.Property(e => e.Name).IsRequired();
builder.Property(e => e.Variant).HasMaxLength(50).IsRequired();
builder.Property(e => e.FormatString).IsRequired(); builder.Property(e => e.FormatString).IsRequired();
} }
} }

View File

@ -1,4 +1,4 @@
namespace OliverBooth.Data.Web; namespace OliverBooth.Data;
/// <summary> /// <summary>
/// Represents a template. /// Represents a template.
@ -15,10 +15,4 @@ public interface ITemplate
/// Gets the name of the template. /// Gets the name of the template.
/// </summary> /// </summary>
string Name { get; } string Name { get; }
/// <summary>
/// Gets the variant of the template.
/// </summary>
/// <value>The variant of the template.</value>
string Variant { get; }
} }

View File

@ -11,9 +11,6 @@ public sealed class Template : ITemplate, IEquatable<Template>
/// <inheritdoc /> /// <inheritdoc />
public string Name { get; private set; } = string.Empty; public string Name { get; private set; } = string.Empty;
/// <inheritdoc />
public string Variant { get; private set; } = string.Empty;
/// <summary> /// <summary>
/// Returns a value indicating whether two instances of <see cref="Template" /> are equal. /// Returns a value indicating whether two instances of <see cref="Template" /> are equal.
/// </summary> /// </summary>
@ -49,7 +46,7 @@ public sealed class Template : ITemplate, IEquatable<Template>
{ {
if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true; if (ReferenceEquals(this, other)) return true;
return Name == other.Name && Variant == other.Variant; return Name == other.Name;
} }
/// <summary> /// <summary>
@ -71,7 +68,7 @@ public sealed class Template : ITemplate, IEquatable<Template>
/// <returns>The hash code.</returns> /// <returns>The hash code.</returns>
public override int GetHashCode() public override int GetHashCode()
{ {
// ReSharper disable NonReadonlyMemberInGetHashCode // ReSharper disable once NonReadonlyMemberInGetHashCode
return HashCode.Combine(Name, Variant); return Name.GetHashCode();
} }
} }

View File

@ -30,10 +30,4 @@ public sealed class TemplateInline : Inline
/// </summary> /// </summary>
/// <value>The template parameters.</value> /// <value>The template parameters.</value>
public IReadOnlyDictionary<string, string> Params { get; set; } = null!; public IReadOnlyDictionary<string, string> Params { get; set; } = null!;
/// <summary>
/// Gets the variant of the template.
/// </summary>
/// <value>The variant of the template.</value>
public string Variant { get; set; } = string.Empty;
} }

View File

@ -37,22 +37,11 @@ public sealed class TemplateInlineParser : InlineParser
template = template[2..^2]; // trim {{ and }} template = template[2..^2]; // trim {{ and }}
ReadOnlySpan<char> name = ReadTemplateName(template, out ReadOnlySpan<char> argumentSpan); ReadOnlySpan<char> name = ReadTemplateName(template, out ReadOnlySpan<char> argumentSpan);
int variantIndex = name.IndexOf(':');
bool hasVariant = variantIndex > -1;
var variant = ReadOnlySpan<char>.Empty;
if (hasVariant)
{
variant = name[(variantIndex + 1)..];
name = name[..variantIndex];
}
if (argumentSpan.IsEmpty) if (argumentSpan.IsEmpty)
{ {
processor.Inline = new TemplateInline processor.Inline = new TemplateInline
{ {
Name = name.ToString(), Name = name.ToString(),
Variant = hasVariant ? variant.ToString() : string.Empty,
ArgumentString = string.Empty, ArgumentString = string.Empty,
ArgumentList = ArraySegment<string>.Empty, ArgumentList = ArraySegment<string>.Empty,
Params = EmptyParams Params = EmptyParams
@ -71,7 +60,6 @@ public sealed class TemplateInlineParser : InlineParser
processor.Inline = new TemplateInline processor.Inline = new TemplateInline
{ {
Name = name.ToString(), Name = name.ToString(),
Variant = hasVariant ? variant.ToString() : string.Empty,
ArgumentString = argumentSpan.ToString(), ArgumentString = argumentSpan.ToString(),
ArgumentList = argumentList.AsReadOnly(), ArgumentList = argumentList.AsReadOnly(),
Params = paramsList.AsReadOnly() Params = paramsList.AsReadOnly()

View File

@ -18,7 +18,7 @@
<nav style="--bs-breadcrumb-divider: '>';" aria-label="breadcrumb"> <nav style="--bs-breadcrumb-divider: '>';" aria-label="breadcrumb">
<ol class="breadcrumb"> <ol class="breadcrumb">
<li class="breadcrumb-item"> <li class="breadcrumb-item">
<a asp-page="index">Blog</a> <a asp-page="/index">Blog</a>
</li> </li>
<li class="breadcrumb-item active" aria-current="page">@post.Title</li> <li class="breadcrumb-item active" aria-current="page">@post.Title</li>
</ol> </ol>

View File

@ -10,9 +10,6 @@ using Serilog;
Log.Logger = new LoggerConfiguration() Log.Logger = new LoggerConfiguration()
.WriteTo.Console() .WriteTo.Console()
.WriteTo.File("logs/latest.log", rollingInterval: RollingInterval.Day) .WriteTo.File("logs/latest.log", rollingInterval: RollingInterval.Day)
#if DEBUG
.MinimumLevel.Debug()
#endif
.CreateLogger(); .CreateLogger();
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

View File

@ -1,6 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using OliverBooth.Data; using OliverBooth.Data;
using OliverBooth.Data.Web;
using OliverBooth.Markdown.Template; using OliverBooth.Markdown.Template;
namespace OliverBooth.Services; namespace OliverBooth.Services;
@ -30,27 +28,4 @@ public interface ITemplateService
/// <paramref name="templateInline" /> is <see langword="null" />. /// <paramref name="templateInline" /> is <see langword="null" />.
/// </exception> /// </exception>
string RenderTemplate(TemplateInline templateInline, ITemplate? template); string RenderTemplate(TemplateInline templateInline, ITemplate? template);
/// <summary>
/// Attempts to get the template with the specified name.
/// </summary>
/// <param name="name">The name of the template.</param>
/// <param name="template">
/// When this method returns, contains the template with the specified name, if the template is found;
/// otherwise, <see langword="null" />.
/// </param>
/// <returns><see langword="true" /> if the template exists; otherwise, <see langword="false" />.</returns>
bool TryGetTemplate(string name, [NotNullWhen(true)] out ITemplate? template);
/// <summary>
/// Attempts to get the template with the specified name and variant.
/// </summary>
/// <param name="name">The name of the template.</param>
/// <param name="variant">The variant of the template.</param>
/// <param name="template">
/// When this method returns, contains the template with the specified name and variant, if the template is
/// found; otherwise, <see langword="null" />.
/// </param>
/// <returns><see langword="true" /> if the template exists; otherwise, <see langword="false" />.</returns>
bool TryGetTemplate(string name, string variant, [NotNullWhen(true)] out ITemplate? template);
} }

View File

@ -1,5 +1,4 @@
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using OliverBooth.Data; using OliverBooth.Data;
using OliverBooth.Data.Web; using OliverBooth.Data.Web;
@ -41,17 +40,17 @@ internal sealed class TemplateService : ITemplateService
{ {
if (templateInline is null) throw new ArgumentNullException(nameof(templateInline)); if (templateInline is null) throw new ArgumentNullException(nameof(templateInline));
return TryGetTemplate(templateInline.Name, templateInline.Variant, out ITemplate? template) using WebContext context = _webContextFactory.CreateDbContext();
? RenderTemplate(templateInline, template) Template? template = context.Templates.Find(templateInline.Name);
: GetDefaultRender(templateInline); return RenderTemplate(templateInline, template);
} }
/// <inheritdoc /> /// <inheritdoc />
public string RenderTemplate(TemplateInline templateInline, ITemplate? template) public string RenderTemplate(TemplateInline inline, ITemplate? template)
{ {
if (template is null) if (template is null)
{ {
return GetDefaultRender(templateInline); return $"{{{{{inline.Name}}}}}";
} }
Span<byte> randomBytes = stackalloc byte[20]; Span<byte> randomBytes = stackalloc byte[20];
@ -59,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"),
}; };
@ -72,28 +71,7 @@ internal sealed class TemplateService : ITemplateService
} }
catch catch
{ {
return GetDefaultRender(templateInline); return $"{{{{{inline.Name}|{inline.ArgumentString}}}}}";
} }
} }
/// <inheritdoc />
public bool TryGetTemplate(string name, [NotNullWhen(true)] out ITemplate? template)
{
return TryGetTemplate(name, string.Empty, out template);
}
/// <inheritdoc />
public bool TryGetTemplate(string name, string variant, [NotNullWhen(true)] out ITemplate? template)
{
using WebContext context = _webContextFactory.CreateDbContext();
template = context.Templates.FirstOrDefault(t => t.Name == name && t.Variant == variant);
return template is not null;
}
private static string GetDefaultRender(TemplateInline templateInline)
{
return string.IsNullOrWhiteSpace(templateInline.ArgumentString)
? $"{{{{{templateInline.Name}}}}}"
: $"{{{{{templateInline.Name}|{templateInline.ArgumentString}}}}}";
}
} }