From 6475687ec7a8580df34c67a45e724caba239ce35 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Mon, 1 Jul 2019 16:01:21 -0500 Subject: [PATCH] simplify seed endpoint building and add validation of TModel --- .../Repositories/UrlsRepository.cs | 35 ++++++++----------- src/FilterLists.Agent/Program.cs | 2 +- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/FilterLists.Agent/Infrastructure/Repositories/UrlsRepository.cs b/src/FilterLists.Agent/Infrastructure/Repositories/UrlsRepository.cs index c54ea11a0..2f33ef014 100644 --- a/src/FilterLists.Agent/Infrastructure/Repositories/UrlsRepository.cs +++ b/src/FilterLists.Agent/Infrastructure/Repositories/UrlsRepository.cs @@ -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 EntityUrlsEndpoints = new Dictionary + { + {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> GetAllAsync() { - var endpoint = BuildEndpoint(); - 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>(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() - { - 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"; - } } } \ No newline at end of file diff --git a/src/FilterLists.Agent/Program.cs b/src/FilterLists.Agent/Program.cs index 34f2daa20..ded98d9e3 100644 --- a/src/FilterLists.Agent/Program.cs +++ b/src/FilterLists.Agent/Program.cs @@ -16,7 +16,7 @@ public static async Task Main() { BuildServiceProvider(); var mediator = _serviceProvider.GetService(); - await mediator.Send(new CaptureLists.Command()); + //await mediator.Send(new CaptureLists.Command()); await mediator.Send(new ValidateAllUrls.Command()); }