Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
v7.7 release
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-nitehawk committed Sep 23, 2022
1 parent 59fb9e7 commit 0d572eb
Show file tree
Hide file tree
Showing 23 changed files with 100 additions and 128 deletions.
2 changes: 1 addition & 1 deletion MongoWebApiStarterTemplate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>

<PackageVersion>7.6.3</PackageVersion>
<PackageVersion>7.7.0</PackageVersion>

<PackageType>Template</PackageType>
<PackageId>MongoWebApiStarter</PackageId>
Expand Down
17 changes: 8 additions & 9 deletions Template/Source/Auth/Roles.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace MongoWebApiStarter.Auth
namespace MongoWebApiStarter.Auth;

/// <summary>
/// the default roles of the system. roles is simply a grouping of permissions.
/// </summary>
public static class Roles
{
/// <summary>
/// the default roles of the system. roles is simply a grouping of permissions.
/// </summary>
public static class Roles
{
public const string Manager = "manager";
public const string Employee = "employee";
}
public const string Manager = "manager";
public const string Employee = "employee";
}
3 changes: 3 additions & 0 deletions Template/Source/Entities/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# CS8618: Non-nullable field is uninitialized. Consider declaring as nullable.
[{*.cs}]
dotnet_diagnostic.CS8618.severity = none
2 changes: 0 additions & 2 deletions Template/Source/Entities/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class Account : Entity
[Preserve] public bool IsEmailVerified { get; set; }
[Preserve] public string EmailVerificationCode { get; set; }



static Account()
{
DB.Index<Account>()
Expand Down
2 changes: 1 addition & 1 deletion Template/Source/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global using FastEndpoints;
global using FastEndpoints.Security;
global using FluentValidation;
global using MongoDB.Entities;
global using FluentValidation;
19 changes: 8 additions & 11 deletions Template/Source/Logic/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,21 @@ public static Task LinkAsync(string[] imageIDs)
{
var validIDs = imageIDs.Where(i => i.HasValue());

if (validIDs.Any())
{
return DB.Update<Dom.Image>()
.Match(i => validIDs.Contains(i.ID))
.Modify(i => i.IsLinked, true)
.ExecuteAsync();
}

return Task.CompletedTask;
return validIDs.Any()
? DB.Update<Dom.Image>()
.Match(i => validIDs.Contains(i.ID))
.Modify(i => i.IsLinked, true)
.ExecuteAsync()
: Task.CompletedTask;
}

public static async Task<long> DeleteUnlinkedAsync()
{
var anHourAgo = DateTime.UtcNow.AddHours(-1);

return (await DB.DeleteAsync<Dom.Image>(i =>
i.CreatedOn <= anHourAgo &&
!i.IsLinked))
.DeletedCount;
}
}

}
12 changes: 6 additions & 6 deletions Template/Source/MongoWebApiStarter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
</PropertyGroup>

<PropertyGroup>
<NoWarn>1701;1702;CA2016;RCS1090;CA2254;CS8618</NoWarn>
<NoWarn>1701;1702;CA2016;RCS1090;CA2254</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="FastEndpoints" Version="4.3.1" />
<PackageReference Include="FastEndpoints.Security" Version="4.3.1" />
<PackageReference Include="FastEndpoints.Swagger" Version="4.3.1" />
<PackageReference Include="FastEndpoints" Version="5.1.0" />
<PackageReference Include="FastEndpoints.Security" Version="5.1.0" />
<PackageReference Include="FastEndpoints.Swagger" Version="5.1.0" />
<PackageReference Include="Flurl" Version="3.0.6" />
<PackageReference Include="Flurl.Http" Version="3.2.4" />
<PackageReference Include="MailKit" Version="3.3.0" />
<PackageReference Include="MailKit" Version="3.4.1" />
<PackageReference Include="MlkPwgen" Version="0.3.0" />
<PackageReference Include="NodaTime" Version="3.1.0" />
<PackageReference Include="NodaTime" Version="3.1.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="MongoDB.Entities" Version="20.26.5" />
Expand Down
2 changes: 1 addition & 1 deletion Template/Source/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
app.UseAuthentication();
app.UseAuthorization();
app.UseResponseCaching();
app.UseFastEndpoints(c => c.SerializerOptions = o => o.PropertyNamingPolicy = null);
app.UseFastEndpoints(c => c.Serializer.Options.PropertyNamingPolicy = null);

var settings = app.Services.GetRequiredService<IOptions<Settings>>().Value;
await DB.InitAsync(settings.Database.Name, settings.Database.Host);
Expand Down
47 changes: 21 additions & 26 deletions Template/Source/Tools/Dates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ public static class Dates
/// <param name="timeZone">The time zone to convert the DateTime in to</param>
public static string ToDatePart(this DateTime UTCDateTime, string timeZone = default_timezone)
{
if (UTCDateTime == default) throw new ArgumentException("Cannot convert default dates to local dates!");

return ToLocal(UTCDateTime, timeZone)
.ToString(year_month_date);
return UTCDateTime == default
? throw new ArgumentException("Cannot convert default dates to local dates!")
: ToLocal(UTCDateTime, timeZone).ToString(year_month_date);
}

/// <summary>
Expand All @@ -32,10 +31,9 @@ public static string ToDatePart(this DateTime UTCDateTime, string timeZone = def
/// <param name="timeZone">The time zone to convert the DateTime in to</param>
public static string ToTimePart(this DateTime UTCDateTime, string timeZone = default_timezone)
{
if (UTCDateTime == default) throw new ArgumentException("Cannot convert default dates to local dates!");

return ToLocal(UTCDateTime, timeZone)
.ToString(hour_minute);
return UTCDateTime == default
? throw new ArgumentException("Cannot convert default dates to local dates!")
: ToLocal(UTCDateTime, timeZone).ToString(hour_minute);
}

/// <summary>
Expand All @@ -45,13 +43,13 @@ public static string ToTimePart(this DateTime UTCDateTime, string timeZone = def
/// <param name="timeZone">The time zone to convert the DateTime in to</param>
public static DateTime ToLocal(this DateTime UTCDateTime, string timeZone = default_timezone)
{
if (UTCDateTime == default) throw new ArgumentException("Cannot convert default dates to local dates!");

if (UTCDateTime.Kind != DateTimeKind.Utc) throw new ArgumentException("The supplied date must be a UTC date/time!");

return Instant.FromDateTimeUtc(UTCDateTime)
.InZone(DateTimeZoneProviders.Tzdb[timeZone])
.ToDateTimeUnspecified();
return UTCDateTime == default
? throw new ArgumentException("Cannot convert default dates to local dates!")
: UTCDateTime.Kind != DateTimeKind.Utc
? throw new ArgumentException("The supplied date must be a UTC date/time!")
: Instant.FromDateTimeUtc(UTCDateTime)
.InZone(DateTimeZoneProviders.Tzdb[timeZone])
.ToDateTimeUnspecified();
}

/// <summary>
Expand All @@ -63,15 +61,14 @@ public static DateTime ToLocal(this DateTime UTCDateTime, string timeZone = defa
public static DateTime ToUTC(string date, string time = "00:00", string timeZone = default_timezone)
{
var result = LocalDateTimePattern
.CreateWithInvariantCulture(
$"{year_month_date} {hour_minute}")
.CreateWithInvariantCulture($"{year_month_date} {hour_minute}")
.Parse(date + " " + time);

if (!result.Success) throw new InvalidPatternException(result.Exception.Message);

return result.Value
.InZoneStrictly(DateTimeZoneProviders.Tzdb[timeZone])
.ToDateTimeUtc();
return !result.Success
? throw new InvalidPatternException(result.Exception.Message)
: result.Value
.InZoneStrictly(DateTimeZoneProviders.Tzdb[timeZone])
.ToDateTimeUtc();
}

/// <summary>
Expand All @@ -96,11 +93,9 @@ public static DateTime FirstDayOfWeek(this DateTime dayInWeek)
public static bool DateTimeFormatIsCorrect(string date, string time = "00:00")
{
var result = LocalDateTimePattern
.CreateWithInvariantCulture(
$"{year_month_date} {hour_minute}")
.CreateWithInvariantCulture($"{year_month_date} {hour_minute}")
.Parse(date + " " + time);

return result.Success;
}
}

}
18 changes: 8 additions & 10 deletions Template/Source/Tools/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace MongoWebApiStarter;

Expand All @@ -11,40 +12,37 @@ public static class Extensions
/// </summary>
public static string TitleCase(this string value)
{
if (value.HasNoValue()) return value;
return txt.ToTitleCase(value.Trim());
return value.HasNoValue() ? value : txt.ToTitleCase(value.Trim());
}

/// <summary>
/// Converts a string to lower-case and trims whites space
/// </summary>
public static string LowerCase(this string value)
{
if (value.HasNoValue()) return value;
return txt.ToLower(value.Trim());
return value.HasNoValue() ? value : txt.ToLower(value.Trim());
}

/// <summary>
/// Converts a string to UPPER-CASE and trims whites space
/// </summary>
public static string UpperCase(this string value)
{
if (value.HasNoValue()) return value;
return txt.ToUpper(value.Trim());
return value.HasNoValue() ? value : txt.ToUpper(value.Trim());
}

/// <summary>
/// Not a null or empty string
/// </summary>
public static bool HasValue(this string? value)
public static bool HasValue([AllowNull, NotNullWhen(true)] this string? value)
{
return value != "null" && !string.IsNullOrEmpty(value);
}

/// <summary>
/// Is either null or an empty string
/// </summary>
public static bool HasNoValue(this string? value)
public static bool HasNoValue([AllowNull, NotNullWhen(false)] this string? value)
{
return value == "null" || string.IsNullOrEmpty(value);
}
Expand All @@ -67,7 +65,7 @@ public static bool IsAValidPassword(this string password)

if (password == null) return false;

bool meetsLengthRequirements = password.Length >= MIN_LENGTH && password.Length <= MAX_LENGTH;
bool meetsLengthRequirements = password.Length is >= MIN_LENGTH and <= MAX_LENGTH;
bool hasUpperCaseLetter = false;
bool hasLowerCaseLetter = false;
bool hasDecimalDigit = false;
Expand Down
15 changes: 5 additions & 10 deletions Template/Source/Tools/GoogleMapLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace MongoWebApiStarter;
/// </summary>
public static class MapLocation
{
private static readonly Regex rxOne = new Regex("@(.*),(.*),", RegexOptions.Compiled); //map link
private static readonly Regex rxTwo = new Regex(@"(?:2d|3d)([-\d].*?)!", RegexOptions.Compiled); //extract lon+lat from embed code
private static readonly Regex rxThree = new Regex(@"src=""(.+?)""", RegexOptions.Compiled); //extract url from embed code
private static readonly Regex rxOne = new("@(.*),(.*),", RegexOptions.Compiled); //map link
private static readonly Regex rxTwo = new(@"(?:2d|3d)([-\d].*?)!", RegexOptions.Compiled); //extract lon+lat from embed code
private static readonly Regex rxThree = new(@"src=""(.+?)""", RegexOptions.Compiled); //extract url from embed code

/// <summary>
/// Extracts lon+lat info from a google map link or embed code and returns a Coordinates2D object
Expand Down Expand Up @@ -48,11 +48,7 @@ public static Coordinates2D Get2DCoordinates(string gMapLinkOrCode)
/// </summary>
/// <param name="gMapLinkOrCode">Google map embed code or link</param>
public static bool IsValid(string gMapLinkOrCode)
{
if (gMapLinkOrCode.HasNoValue()) return true;

return Get2DCoordinates(gMapLinkOrCode) != null;
}
=> gMapLinkOrCode.HasNoValue() || Get2DCoordinates(gMapLinkOrCode) != null;

/// <summary>
/// Extracts the URL from Google Map Embed code
Expand All @@ -78,5 +74,4 @@ public static bool IsEmbedLink(string link)
{
return link.HasNoValue() || link.StartsWith("https://www.google.com/maps/embed");
}
}

}
23 changes: 10 additions & 13 deletions Template/Source/Tools/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace MongoWebApiStarter;

public record Notification
{
private static readonly Dictionary<string, NotificationTemplate> templates = new Dictionary<string, NotificationTemplate>();
private static readonly Dictionary<string, NotificationTemplate> templates = new();
private static readonly Regex rx = new("{.*}", RegexOptions.Compiled);

public static async Task Initialize()
Expand All @@ -15,17 +15,15 @@ public static async Task Initialize()
templates.Add(t.ID, t);
}

#pragma warning disable CS8618
public string ToName { get; init; }
public string ToEmail { get; init; }
public string ToMobile { get; init; }
public string ToName { get; init; } = null!;
public string ToEmail { get; init; } = null!;
public string ToMobile { get; init; } = null!;
public bool SendEmail { get; init; }
public bool SendSMS { get; init; }
public string Type { get; init; }
#pragma warning restore CS8618
public string Type { get; init; } = null!;

private readonly HashSet<(string Name, string Value)> mergeFields = new HashSet<(string Name, string Value)>();
private readonly List<string> missingTags = new List<string>();
private readonly HashSet<(string Name, string Value)> mergeFields = new();
private readonly List<string> missingTags = new();

public Notification Merge(string fieldName, string fieldValue)
{
Expand All @@ -36,8 +34,8 @@ public Notification Merge(string fieldName, string fieldValue)
public Task AddToSendingQueueAsync()
{
if (ToName.HasNoValue() ||
(SendEmail && ToEmail.HasNoValue()) ||
(SendSMS && ToMobile.HasNoValue()) ||
(SendEmail && ToEmail.HasNoValue()) ||
(SendSMS && ToMobile.HasNoValue()) ||
Type.HasNoValue())
{
throw new ArgumentNullException("Unable to send notification without all required parameters!");
Expand Down Expand Up @@ -100,5 +98,4 @@ private string MergeFields(string input, string fieldName)
missingTags.AddRange(rx.Matches(body).Select(m => m.Value).Distinct());
return body;
}
}

}
3 changes: 3 additions & 0 deletions Template/Source/[Features]/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# CS8618: Non-nullable field is uninitialized. Consider declaring as nullable.
[{*Request*.cs,*Response*.cs,*Model*.cs}]
dotnet_diagnostic.CS8618.severity = none
10 changes: 5 additions & 5 deletions Template/Source/[Features]/Account/Get/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public override async Task HandleAsync(Request r, CancellationToken ct)
if (acc is null)
{
await SendNotFoundAsync();
return;
}

await SendAsync(Map.FromEntity(acc), 200, ct);
else
{
await SendAsync(Map.FromEntity(acc));
}
}
}

}
3 changes: 1 addition & 2 deletions Template/Source/[Features]/Account/Get/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ public class Response : Model
{
public string AccountID { get; set; }
public bool IsEmailVerified { get; set; }
}

}
3 changes: 1 addition & 2 deletions Template/Source/[Features]/Account/Login/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,4 @@ public override async Task HandleAsync(Request r, CancellationToken ct)

await SendAsync(Response, cancellation: ct);
}
}

}
Loading

0 comments on commit 0d572eb

Please sign in to comment.