style: apply braces style to project

This commit is contained in:
Oliver Booth 2024-02-25 14:11:33 +00:00
parent 14d73851ea
commit 278c807fa3
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
13 changed files with 128 additions and 28 deletions

View File

@ -45,8 +45,16 @@ internal sealed class BlacklistEntry : IEquatable<BlacklistEntry>, IBlacklistEnt
/// </returns>
public bool Equals(BlacklistEntry? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return EmailAddress.Equals(other.EmailAddress);
}

View File

@ -42,8 +42,16 @@ internal sealed class ProgrammingLanguage : IEquatable<ProgrammingLanguage>, IPr
/// </returns>
public bool Equals(ProgrammingLanguage? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return Key.Equals(other.Key);
}

View File

@ -74,8 +74,16 @@ internal sealed class Project : IEquatable<Project>, IProject
/// </returns>
public bool Equals(Project? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return Id.Equals(other.Id);
}

View File

@ -50,8 +50,16 @@ public sealed class SiteConfiguration : IEquatable<SiteConfiguration>
/// </returns>
public bool Equals(SiteConfiguration? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return Key == other.Key;
}

View File

@ -47,8 +47,16 @@ public sealed class Template : ITemplate, IEquatable<Template>
/// </returns>
public bool Equals(Template? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return Name == other.Name && Variant == other.Variant;
}

View File

@ -62,8 +62,14 @@ internal sealed class User : IUser, IBlogAuthor
Span<char> hex = stackalloc char[2];
for (var index = 0; index < hash.Length; index++)
{
if (hash[index].TryFormat(hex, out _, "x2")) builder.Append(hex);
else builder.Append("00");
if (hash[index].TryFormat(hex, out _, "x2"))
{
builder.Append(hex);
}
else
{
builder.Append("00");
}
}
return new Uri($"https://www.gravatar.com/avatar/{builder}?size={size}");

View File

@ -18,13 +18,17 @@ public sealed class DateFormatter : IFormatter
public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
if (formattingInfo.CurrentValue is not string value)
{
return false;
}
if (!DateTime.TryParseExact(value, "yyyy-MM-dd",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out DateTime date))
{
return false;
}
formattingInfo.Write(date.ToString(formattingInfo.Format?.ToString(), CultureInfo.InvariantCulture));

View File

@ -29,7 +29,9 @@ public sealed class MarkdownFormatter : IFormatter
public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
if (formattingInfo.CurrentValue is not string value)
{
return false;
}
var pipeline = _serviceProvider.GetService<MarkdownPipeline>();
formattingInfo.Write(Markdig.Markdown.ToHtml(value, pipeline));

View File

@ -56,7 +56,11 @@ public sealed class TimestampInlineParser : InlineParser
timestamp = default;
format = default;
if (!source.StartsWith("<t:")) return false;
if (!source.StartsWith("<t:"))
{
return false;
}
timestamp = source[3..];
if (timestamp.IndexOf('>') == -1)

View File

@ -39,7 +39,9 @@ public class RawArticle : PageModel
builder.AppendLine($"Published: {post.Published:R}");
if (post.Updated.HasValue)
{
builder.AppendLine($"Updated: {post.Updated:R}");
}
builder.AppendLine();
builder.AppendLine(post.Body);

View File

@ -35,8 +35,15 @@ internal sealed class SessionService : ISessionService
/// <inheritdoc />
public ISession CreateSession(HttpRequest request, IUser user)
{
if (request is null) throw new ArgumentNullException(nameof(request));
if (user is null) throw new ArgumentNullException(nameof(user));
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
if (user is null)
{
throw new ArgumentNullException(nameof(user));
}
using WebContext context = _webContextFactory.CreateDbContext();
var now = DateTimeOffset.UtcNow;
@ -66,11 +73,21 @@ internal sealed class SessionService : ISessionService
/// <inheritdoc />
public void SaveSessionCookie(HttpResponse response, ISession session)
{
if (response is null) throw new ArgumentNullException(nameof(response));
if (session is null) throw new ArgumentNullException(nameof(session));
if (response is null)
{
throw new ArgumentNullException(nameof(response));
}
if (session is null)
{
throw new ArgumentNullException(nameof(session));
}
Span<byte> buffer = stackalloc byte[16];
if (!session.Id.TryWriteBytes(buffer)) return;
if (!session.Id.TryWriteBytes(buffer))
{
return;
}
IPAddress? remoteIpAddress = response.HttpContext.Connection.RemoteIpAddress;
_logger.LogDebug("Writing cookie 'sid' to HTTP response for {RemoteAddr}", remoteIpAddress);
@ -88,18 +105,28 @@ internal sealed class SessionService : ISessionService
/// <inheritdoc />
public bool TryGetSession(HttpRequest request, [NotNullWhen(true)] out ISession? session)
{
if (request is null) throw new ArgumentNullException(nameof(request));
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
session = null;
IPAddress? remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;
if (remoteIpAddress is null) return false;
if (remoteIpAddress is null)
{
return false;
}
if (!request.Cookies.TryGetValue("sid", out string? sessionIdCookie))
{
return false;
}
Span<byte> bytes = stackalloc byte[16];
if (!Convert.TryFromBase64Chars(sessionIdCookie, bytes, out int bytesWritten) || bytesWritten < 16)
{
return false;
}
var sessionId = new Guid(bytes);
return TryGetSession(sessionId, out session);
@ -108,13 +135,18 @@ internal sealed class SessionService : ISessionService
/// <inheritdoc />
public bool ValidateSession(HttpRequest request, ISession session)
{
if (request is null) throw new ArgumentNullException(nameof(request));
if (session is null) throw new ArgumentNullException(nameof(session));
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
if (session is null)
{
throw new ArgumentNullException(nameof(session));
}
IPAddress? remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;
if (remoteIpAddress is null) return false;
if (session.Expires >= DateTimeOffset.UtcNow)
if (remoteIpAddress is null)
{
return false;
}

View File

@ -38,7 +38,10 @@ internal sealed class TemplateService : ITemplateService
/// <inheritdoc />
public string RenderGlobalTemplate(TemplateInline templateInline)
{
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)
? RenderTemplate(templateInline, template)

View File

@ -28,12 +28,19 @@ internal sealed class UserService : IUserService
/// <inheritdoc />
public bool TryGetUser(Guid id, [NotNullWhen(true)] out IUser? user)
{
if (_userCache.TryGetValue(id, out user)) return true;
if (_userCache.TryGetValue(id, out user))
{
return true;
}
using WebContext context = _dbContextFactory.CreateDbContext();
user = context.Users.Find(id);
if (user is not null) _userCache.TryAdd(id, user);
if (user is not null)
{
_userCache.TryAdd(id, user);
}
return user is not null;
}