simplify seed endpoint building and add validation of TModel

This commit is contained in:
Collin M. Barrett 2019-07-01 16:01:21 -05:00
parent 821a0ec0d2
commit 6475687ec7
2 changed files with 15 additions and 22 deletions

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using FilterLists.Agent.Core.Interfaces;
@ -11,6 +12,15 @@ namespace FilterLists.Agent.Infrastructure.Repositories
{
public class UrlsRepository : IUrlsRepository
{
private static readonly Dictionary<string, string> EntityUrlsEndpoints = new Dictionary<string, string>
{
{nameof(LicenseUrls), "licenses"},
{nameof(ListUrls), "lists"},
{nameof(MaintainerUrls), "maintainers"},
{nameof(SoftwareUrls), "software"},
{nameof(SyntaxUrls), "syntaxes"}
};
private readonly IFilterListsApiClient _apiClient;
public UrlsRepository(IFilterListsApiClient apiClient)
@ -20,31 +30,14 @@ public UrlsRepository(IFilterListsApiClient apiClient)
public async Task<IEnumerable<Uri>> GetAllAsync<TModel>()
{
var endpoint = BuildEndpoint<TModel>();
var request = new RestRequest(endpoint);
if (!EntityUrlsEndpoints.ContainsKey(typeof(TModel).Name))
throw new InvalidEnumArgumentException("The type of TModel is not valid.");
var request = new RestRequest($"{EntityUrlsEndpoints[typeof(TModel).Name]}/seed");
var response = await _apiClient.ExecuteAsync<IEnumerable<TModel>>(request);
return response.SelectMany(r =>
r.GetType().GetProperties().Where(p => p.GetType() == typeof(Uri) && p.GetValue(r) != null)
.Select(p => (Uri)p.GetValue(r)));
}
private static string BuildEndpoint<TModel>()
{
string endpointSuffix;
switch (typeof(TModel).Name)
{
case nameof(SyntaxUrls):
endpointSuffix = "es";
break;
case nameof(SoftwareUrls):
endpointSuffix = "";
break;
default:
endpointSuffix = "s";
break;
}
return $"{typeof(TModel).Name.Replace("Urls", endpointSuffix).ToLowerInvariant()}/seed";
}
}
}

View file

@ -16,7 +16,7 @@ public static async Task Main()
{
BuildServiceProvider();
var mediator = _serviceProvider.GetService<IMediator>();
await mediator.Send(new CaptureLists.Command());
//await mediator.Send(new CaptureLists.Command());
await mediator.Send(new ValidateAllUrls.Command());
}