mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
extract url validator to extension method
This commit is contained in:
parent
045eac74c9
commit
82c6ed61c9
3 changed files with 13 additions and 9 deletions
12
src/FilterLists.Services/Extensions/StringExtensions.cs
Normal file
12
src/FilterLists.Services/Extensions/StringExtensions.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace FilterLists.Services.Extensions
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static bool IsValidHttpOrHttpsUrl(this string source) =>
|
||||
Uri.TryCreate(source, UriKind.Absolute, out var uriResult) &&
|
||||
new[] {Uri.UriSchemeHttps, Uri.UriSchemeHttp}.Contains(uriResult.Scheme);
|
||||
}
|
||||
}
|
||||
|
|
@ -29,8 +29,4 @@
|
|||
<ProjectReference Include="..\FilterLists.Data\FilterLists.Data.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Extensions\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -45,7 +45,7 @@ public Snapshot(FilterListsDbContext dbContext, EmailService emailService, Filte
|
|||
public async Task TrySaveAsync()
|
||||
{
|
||||
await AddSnapEntity();
|
||||
if (!IsViewUrlValid())
|
||||
if (!list.ViewUrl.IsValidHttpOrHttpsUrl())
|
||||
return;
|
||||
try
|
||||
{
|
||||
|
|
@ -63,10 +63,6 @@ private async Task AddSnapEntity()
|
|||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private bool IsViewUrlValid() =>
|
||||
Uri.TryCreate(list.ViewUrl, UriKind.Absolute, out var uriResult) &&
|
||||
(uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
using (var transaction = dbContext.Database.BeginTransaction())
|
||||
|
|
|
|||
Loading…
Reference in a new issue