From f6a975acbe8e75656339eaca74d4c9b23b4a4664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Michel?= Date: Tue, 26 Oct 2021 16:59:50 +0200 Subject: [PATCH] make : // TODO: Make a generic service for any LookupData type (#594) * make : // TODO: Make a generic service for any LookupData type => CatalogLookupDataService * Update src/BlazorAdmin/Services/CachedCatalogTypeServiceDecorator.cs Co-authored-by: Steve Smith --- .../Pages/CatalogItemPage/List.razor.cs | 8 ++-- .../CachedCatalogBrandServiceDecorator.cs | 6 +-- .../CachedCatalogTypeServiceDecorator.cs | 7 ++- .../Services/CatalogBrandService.cs | 41 ---------------- .../Services/CatalogItemService.cs | 17 +++---- .../Services/CatalogLookupDataService.cs | 48 +++++++++++++++++++ .../Services/CatalogTypeService.cs | 40 ---------------- src/BlazorAdmin/ServicesConfiguration.cs | 9 ++-- .../Attributes/EndpointAttribute.cs | 9 ++++ .../Interfaces/ICatalogBrandService.cs | 12 ----- .../Interfaces/ICatalogLookupDataService.cs | 12 +++++ .../Interfaces/ICatalogTypeService.cs | 12 ----- .../Interfaces/ILookupDataResponse.cs | 10 ++++ src/BlazorShared/Models/CatalogBrand.cs | 5 +- .../Models/CatalogBrandResponse.cs | 9 ++-- src/BlazorShared/Models/CatalogType.cs | 5 +- .../Models/CatalogTypeResponse.cs | 10 ++-- src/BlazorShared/Models/LookupData.cs | 2 +- 18 files changed, 122 insertions(+), 140 deletions(-) delete mode 100644 src/BlazorAdmin/Services/CatalogBrandService.cs create mode 100644 src/BlazorAdmin/Services/CatalogLookupDataService.cs delete mode 100644 src/BlazorAdmin/Services/CatalogTypeService.cs create mode 100644 src/BlazorShared/Attributes/EndpointAttribute.cs delete mode 100644 src/BlazorShared/Interfaces/ICatalogBrandService.cs create mode 100644 src/BlazorShared/Interfaces/ICatalogLookupDataService.cs delete mode 100644 src/BlazorShared/Interfaces/ICatalogTypeService.cs create mode 100644 src/BlazorShared/Interfaces/ILookupDataResponse.cs diff --git a/src/BlazorAdmin/Pages/CatalogItemPage/List.razor.cs b/src/BlazorAdmin/Pages/CatalogItemPage/List.razor.cs index 9b12a73..73a8d34 100644 --- a/src/BlazorAdmin/Pages/CatalogItemPage/List.razor.cs +++ b/src/BlazorAdmin/Pages/CatalogItemPage/List.razor.cs @@ -1,8 +1,8 @@ using BlazorAdmin.Helpers; -using System.Collections.Generic; -using System.Threading.Tasks; using BlazorShared.Interfaces; using BlazorShared.Models; +using System.Collections.Generic; +using System.Threading.Tasks; namespace BlazorAdmin.Pages.CatalogItemPage { @@ -12,10 +12,10 @@ namespace BlazorAdmin.Pages.CatalogItemPage public ICatalogItemService CatalogItemService { get; set; } [Microsoft.AspNetCore.Components.Inject] - public ICatalogBrandService CatalogBrandService { get; set; } + public ICatalogLookupDataService CatalogBrandService { get; set; } [Microsoft.AspNetCore.Components.Inject] - public ICatalogTypeService CatalogTypeService { get; set; } + public ICatalogLookupDataService CatalogTypeService { get; set; } private List catalogItems = new List(); private List catalogTypes = new List(); diff --git a/src/BlazorAdmin/Services/CachedCatalogBrandServiceDecorator.cs b/src/BlazorAdmin/Services/CachedCatalogBrandServiceDecorator.cs index b076490..740a1bb 100644 --- a/src/BlazorAdmin/Services/CachedCatalogBrandServiceDecorator.cs +++ b/src/BlazorAdmin/Services/CachedCatalogBrandServiceDecorator.cs @@ -9,15 +9,15 @@ using System.Threading.Tasks; namespace BlazorAdmin.Services { - public class CachedCatalogBrandServiceDecorator : ICatalogBrandService + public class CachedCatalogBrandServiceDecorator : ICatalogLookupDataService { // TODO: Make a generic decorator for any LookupData type private readonly ILocalStorageService _localStorageService; - private readonly CatalogBrandService _catalogBrandService; + private readonly CatalogLookupDataService _catalogBrandService; private ILogger _logger; public CachedCatalogBrandServiceDecorator(ILocalStorageService localStorageService, - CatalogBrandService catalogBrandService, + CatalogLookupDataService catalogBrandService, ILogger logger) { _localStorageService = localStorageService; diff --git a/src/BlazorAdmin/Services/CachedCatalogTypeServiceDecorator.cs b/src/BlazorAdmin/Services/CachedCatalogTypeServiceDecorator.cs index 86c0cc0..d61e8b8 100644 --- a/src/BlazorAdmin/Services/CachedCatalogTypeServiceDecorator.cs +++ b/src/BlazorAdmin/Services/CachedCatalogTypeServiceDecorator.cs @@ -9,15 +9,14 @@ using System.Threading.Tasks; namespace BlazorAdmin.Services { - public class CachedCatalogTypeServiceDecorator : ICatalogTypeService + public class CachedCatalogTypeServiceDecorator : ICatalogLookupDataService { - // TODO: Make a generic decorator for any LookupData type private readonly ILocalStorageService _localStorageService; - private readonly CatalogTypeService _catalogTypeService; + private readonly CatalogLookupDataService _catalogTypeService; private ILogger _logger; public CachedCatalogTypeServiceDecorator(ILocalStorageService localStorageService, - CatalogTypeService catalogTypeService, + CatalogLookupDataService catalogTypeService, ILogger logger) { _localStorageService = localStorageService; diff --git a/src/BlazorAdmin/Services/CatalogBrandService.cs b/src/BlazorAdmin/Services/CatalogBrandService.cs deleted file mode 100644 index 263d6ea..0000000 --- a/src/BlazorAdmin/Services/CatalogBrandService.cs +++ /dev/null @@ -1,41 +0,0 @@ -using BlazorShared; -using BlazorShared.Interfaces; -using BlazorShared.Models; -using Microsoft.Extensions.Logging; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Net.Http.Json; -using System.Threading.Tasks; - - -namespace BlazorAdmin.Services -{ - public class CatalogBrandService : ICatalogBrandService - { - // TODO: Make a generic service for any LookupData type - private readonly HttpClient _httpClient; - private readonly ILogger _logger; - private string _apiUrl; - - public CatalogBrandService(HttpClient httpClient, - BaseUrlConfiguration baseUrlConfiguration, - ILogger logger) - { - _httpClient = httpClient; - _logger = logger; - _apiUrl = baseUrlConfiguration.ApiBase; - } - - public async Task GetById(int id) - { - return (await List()).FirstOrDefault(x => x.Id == id); - } - - public async Task> List() - { - _logger.LogInformation("Fetching brands from API."); - return (await _httpClient.GetFromJsonAsync($"{_apiUrl}catalog-brands"))?.CatalogBrands; - } - } -} diff --git a/src/BlazorAdmin/Services/CatalogItemService.cs b/src/BlazorAdmin/Services/CatalogItemService.cs index fb8194f..b5b5ddd 100644 --- a/src/BlazorAdmin/Services/CatalogItemService.cs +++ b/src/BlazorAdmin/Services/CatalogItemService.cs @@ -1,5 +1,4 @@ -using BlazorShared; -using BlazorShared.Interfaces; +using BlazorShared.Interfaces; using BlazorShared.Models; using Microsoft.Extensions.Logging; using System.Collections.Generic; @@ -11,24 +10,20 @@ namespace BlazorAdmin.Services { public class CatalogItemService : ICatalogItemService { - private readonly ICatalogBrandService _brandService; - private readonly ICatalogTypeService _typeService; + private readonly ICatalogLookupDataService _brandService; + private readonly ICatalogLookupDataService _typeService; private readonly HttpService _httpService; private readonly ILogger _logger; - private string _apiUrl; - public CatalogItemService(ICatalogBrandService brandService, - ICatalogTypeService typeService, + public CatalogItemService(ICatalogLookupDataService brandService, + ICatalogLookupDataService typeService, HttpService httpService, - BaseUrlConfiguration baseUrlConfiguration, ILogger logger) { _brandService = brandService; _typeService = typeService; - _httpService = httpService; _logger = logger; - _apiUrl = baseUrlConfiguration.ApiBase; } public async Task Create(CreateCatalogItemRequest catalogItem) @@ -85,7 +80,7 @@ namespace BlazorAdmin.Services _logger.LogInformation("Fetching catalog items from API."); var brandListTask = _brandService.List(); - var typeListTask = _typeService.List(); + var typeListTask = _typeService.List(); var itemListTask = _httpService.HttpGet($"catalog-items"); await Task.WhenAll(brandListTask, typeListTask, itemListTask); var brands = brandListTask.Result; diff --git a/src/BlazorAdmin/Services/CatalogLookupDataService.cs b/src/BlazorAdmin/Services/CatalogLookupDataService.cs new file mode 100644 index 0000000..b2a2639 --- /dev/null +++ b/src/BlazorAdmin/Services/CatalogLookupDataService.cs @@ -0,0 +1,48 @@ +using BlazorShared; +using BlazorShared.Attributes; +using BlazorShared.Interfaces; +using BlazorShared.Models; +using Microsoft.Extensions.Logging; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Json; +using System.Reflection; +using System.Threading.Tasks; + +namespace BlazorAdmin.Services +{ + public class CatalogLookupDataService + : ICatalogLookupDataService + where TLookupData : LookupData + where TReponse : ILookupDataResponse + { + + private readonly HttpClient _httpClient; + private readonly ILogger> _logger; + private readonly string _apiUrl; + + public CatalogLookupDataService(HttpClient httpClient, + BaseUrlConfiguration baseUrlConfiguration, + ILogger> logger) + { + _httpClient = httpClient; + _logger = logger; + _apiUrl = baseUrlConfiguration.ApiBase; + } + + public async Task GetById(int id) + { + return (await List()).FirstOrDefault(x => x.Id == id); + } + + public async Task> List() + { + var endpointName = typeof(TLookupData).GetCustomAttribute().Name; + _logger.LogInformation($"Fetching {typeof(TLookupData).Name} from API. Enpoint : {endpointName}"); + + var response = await _httpClient.GetFromJsonAsync($"{_apiUrl}{endpointName}"); + return response.List; + } + } +} diff --git a/src/BlazorAdmin/Services/CatalogTypeService.cs b/src/BlazorAdmin/Services/CatalogTypeService.cs deleted file mode 100644 index a41fc7d..0000000 --- a/src/BlazorAdmin/Services/CatalogTypeService.cs +++ /dev/null @@ -1,40 +0,0 @@ -using BlazorShared; -using BlazorShared.Interfaces; -using BlazorShared.Models; -using Microsoft.Extensions.Logging; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Net.Http.Json; -using System.Threading.Tasks; - -namespace BlazorAdmin.Services -{ - public class CatalogTypeService : ICatalogTypeService - { - // TODO: Make a generic service for any LookupData type - private readonly HttpClient _httpClient; - private readonly ILogger _logger; - private string _apiUrl; - - public CatalogTypeService(HttpClient httpClient, - BaseUrlConfiguration baseUrlConfiguration, - ILogger logger) - { - _httpClient = httpClient; - _logger = logger; - _apiUrl = baseUrlConfiguration.ApiBase; - } - - public async Task GetById(int id) - { - return (await List()).FirstOrDefault(x => x.Id == id); - } - - public async Task> List() - { - _logger.LogInformation("Fetching types from API."); - return (await _httpClient.GetFromJsonAsync($"{_apiUrl}catalog-types"))?.CatalogTypes; - } - } -} diff --git a/src/BlazorAdmin/ServicesConfiguration.cs b/src/BlazorAdmin/ServicesConfiguration.cs index fb4302b..31dd1bb 100644 --- a/src/BlazorAdmin/ServicesConfiguration.cs +++ b/src/BlazorAdmin/ServicesConfiguration.cs @@ -1,5 +1,6 @@ using BlazorAdmin.Services; using BlazorShared.Interfaces; +using BlazorShared.Models; using Microsoft.Extensions.DependencyInjection; namespace BlazorAdmin @@ -8,10 +9,10 @@ namespace BlazorAdmin { public static IServiceCollection AddBlazorServices(this IServiceCollection services) { - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + services.AddScoped, CachedCatalogBrandServiceDecorator>(); + services.AddScoped>(); + services.AddScoped, CachedCatalogTypeServiceDecorator>(); + services.AddScoped>(); services.AddScoped(); services.AddScoped(); diff --git a/src/BlazorShared/Attributes/EndpointAttribute.cs b/src/BlazorShared/Attributes/EndpointAttribute.cs new file mode 100644 index 0000000..ae0207b --- /dev/null +++ b/src/BlazorShared/Attributes/EndpointAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace BlazorShared.Attributes +{ + public class EndpointAttribute : Attribute + { + public string Name { get; set; } + } +} diff --git a/src/BlazorShared/Interfaces/ICatalogBrandService.cs b/src/BlazorShared/Interfaces/ICatalogBrandService.cs deleted file mode 100644 index ba2f920..0000000 --- a/src/BlazorShared/Interfaces/ICatalogBrandService.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using BlazorShared.Models; - -namespace BlazorShared.Interfaces -{ - public interface ICatalogBrandService - { - Task> List(); - Task GetById(int id); - } -} diff --git a/src/BlazorShared/Interfaces/ICatalogLookupDataService.cs b/src/BlazorShared/Interfaces/ICatalogLookupDataService.cs new file mode 100644 index 0000000..3d973fc --- /dev/null +++ b/src/BlazorShared/Interfaces/ICatalogLookupDataService.cs @@ -0,0 +1,12 @@ +using BlazorShared.Models; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace BlazorShared.Interfaces +{ + public interface ICatalogLookupDataService where TLookupData : LookupData + { + Task> List(); + Task GetById(int id); + } +} diff --git a/src/BlazorShared/Interfaces/ICatalogTypeService.cs b/src/BlazorShared/Interfaces/ICatalogTypeService.cs deleted file mode 100644 index 437ff34..0000000 --- a/src/BlazorShared/Interfaces/ICatalogTypeService.cs +++ /dev/null @@ -1,12 +0,0 @@ -using BlazorShared.Models; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace BlazorShared.Interfaces -{ - public interface ICatalogTypeService - { - Task> List(); - Task GetById(int id); - } -} diff --git a/src/BlazorShared/Interfaces/ILookupDataResponse.cs b/src/BlazorShared/Interfaces/ILookupDataResponse.cs new file mode 100644 index 0000000..83e95c8 --- /dev/null +++ b/src/BlazorShared/Interfaces/ILookupDataResponse.cs @@ -0,0 +1,10 @@ +using BlazorShared.Models; +using System.Collections.Generic; + +namespace BlazorShared.Interfaces +{ + public interface ILookupDataResponse where TLookupData : LookupData + { + List List { get; set; } + } +} diff --git a/src/BlazorShared/Models/CatalogBrand.cs b/src/BlazorShared/Models/CatalogBrand.cs index 631a23b..5335ae8 100644 --- a/src/BlazorShared/Models/CatalogBrand.cs +++ b/src/BlazorShared/Models/CatalogBrand.cs @@ -1,5 +1,8 @@ -namespace BlazorShared.Models +using BlazorShared.Attributes; + +namespace BlazorShared.Models { + [Endpoint(Name = "catalog-brands")] public class CatalogBrand : LookupData { } diff --git a/src/BlazorShared/Models/CatalogBrandResponse.cs b/src/BlazorShared/Models/CatalogBrandResponse.cs index ba01032..80cf0d3 100644 --- a/src/BlazorShared/Models/CatalogBrandResponse.cs +++ b/src/BlazorShared/Models/CatalogBrandResponse.cs @@ -1,9 +1,12 @@ -using System.Collections.Generic; +using BlazorShared.Interfaces; +using System.Collections.Generic; +using System.Text.Json.Serialization; namespace BlazorShared.Models { - public class CatalogBrandResponse + public class CatalogBrandResponse : ILookupDataResponse { - public List CatalogBrands { get; set; } = new List(); + [JsonPropertyName("CatalogBrands")] + public List List { get; set; } = new List(); } } diff --git a/src/BlazorShared/Models/CatalogType.cs b/src/BlazorShared/Models/CatalogType.cs index ed7dad9..6e2526d 100644 --- a/src/BlazorShared/Models/CatalogType.cs +++ b/src/BlazorShared/Models/CatalogType.cs @@ -1,5 +1,8 @@ -namespace BlazorShared.Models +using BlazorShared.Attributes; + +namespace BlazorShared.Models { + [Endpoint(Name = "catalog-types")] public class CatalogType : LookupData { } diff --git a/src/BlazorShared/Models/CatalogTypeResponse.cs b/src/BlazorShared/Models/CatalogTypeResponse.cs index 664732e..8c81100 100644 --- a/src/BlazorShared/Models/CatalogTypeResponse.cs +++ b/src/BlazorShared/Models/CatalogTypeResponse.cs @@ -1,9 +1,13 @@ -using System.Collections.Generic; +using BlazorShared.Interfaces; +using System.Collections.Generic; +using System.Text.Json.Serialization; namespace BlazorShared.Models { - public class CatalogTypeResponse + public class CatalogTypeResponse : ILookupDataResponse { - public List CatalogTypes { get; set; } = new List(); + + [JsonPropertyName("CatalogTypes")] + public List List { get; set; } = new List(); } } diff --git a/src/BlazorShared/Models/LookupData.cs b/src/BlazorShared/Models/LookupData.cs index 53e029b..d54f94f 100644 --- a/src/BlazorShared/Models/LookupData.cs +++ b/src/BlazorShared/Models/LookupData.cs @@ -1,7 +1,7 @@ namespace BlazorShared.Models { public abstract class LookupData -{ + { public int Id { get; set; } public string Name { get; set; } }