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 <steve@kentsmiths.com>
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
using BlazorAdmin.Helpers;
|
using BlazorAdmin.Helpers;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using BlazorShared.Interfaces;
|
using BlazorShared.Interfaces;
|
||||||
using BlazorShared.Models;
|
using BlazorShared.Models;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BlazorAdmin.Pages.CatalogItemPage
|
namespace BlazorAdmin.Pages.CatalogItemPage
|
||||||
{
|
{
|
||||||
@@ -12,10 +12,10 @@ namespace BlazorAdmin.Pages.CatalogItemPage
|
|||||||
public ICatalogItemService CatalogItemService { get; set; }
|
public ICatalogItemService CatalogItemService { get; set; }
|
||||||
|
|
||||||
[Microsoft.AspNetCore.Components.Inject]
|
[Microsoft.AspNetCore.Components.Inject]
|
||||||
public ICatalogBrandService CatalogBrandService { get; set; }
|
public ICatalogLookupDataService<CatalogBrand> CatalogBrandService { get; set; }
|
||||||
|
|
||||||
[Microsoft.AspNetCore.Components.Inject]
|
[Microsoft.AspNetCore.Components.Inject]
|
||||||
public ICatalogTypeService CatalogTypeService { get; set; }
|
public ICatalogLookupDataService<CatalogType> CatalogTypeService { get; set; }
|
||||||
|
|
||||||
private List<CatalogItem> catalogItems = new List<CatalogItem>();
|
private List<CatalogItem> catalogItems = new List<CatalogItem>();
|
||||||
private List<CatalogType> catalogTypes = new List<CatalogType>();
|
private List<CatalogType> catalogTypes = new List<CatalogType>();
|
||||||
|
|||||||
@@ -9,15 +9,15 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace BlazorAdmin.Services
|
namespace BlazorAdmin.Services
|
||||||
{
|
{
|
||||||
public class CachedCatalogBrandServiceDecorator : ICatalogBrandService
|
public class CachedCatalogBrandServiceDecorator : ICatalogLookupDataService<CatalogBrand>
|
||||||
{
|
{
|
||||||
// TODO: Make a generic decorator for any LookupData type
|
// TODO: Make a generic decorator for any LookupData type
|
||||||
private readonly ILocalStorageService _localStorageService;
|
private readonly ILocalStorageService _localStorageService;
|
||||||
private readonly CatalogBrandService _catalogBrandService;
|
private readonly CatalogLookupDataService<CatalogBrand, CatalogBrandResponse> _catalogBrandService;
|
||||||
private ILogger<CachedCatalogBrandServiceDecorator> _logger;
|
private ILogger<CachedCatalogBrandServiceDecorator> _logger;
|
||||||
|
|
||||||
public CachedCatalogBrandServiceDecorator(ILocalStorageService localStorageService,
|
public CachedCatalogBrandServiceDecorator(ILocalStorageService localStorageService,
|
||||||
CatalogBrandService catalogBrandService,
|
CatalogLookupDataService<CatalogBrand, CatalogBrandResponse> catalogBrandService,
|
||||||
ILogger<CachedCatalogBrandServiceDecorator> logger)
|
ILogger<CachedCatalogBrandServiceDecorator> logger)
|
||||||
{
|
{
|
||||||
_localStorageService = localStorageService;
|
_localStorageService = localStorageService;
|
||||||
|
|||||||
@@ -9,15 +9,14 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace BlazorAdmin.Services
|
namespace BlazorAdmin.Services
|
||||||
{
|
{
|
||||||
public class CachedCatalogTypeServiceDecorator : ICatalogTypeService
|
public class CachedCatalogTypeServiceDecorator : ICatalogLookupDataService<CatalogType>
|
||||||
{
|
{
|
||||||
// TODO: Make a generic decorator for any LookupData type
|
|
||||||
private readonly ILocalStorageService _localStorageService;
|
private readonly ILocalStorageService _localStorageService;
|
||||||
private readonly CatalogTypeService _catalogTypeService;
|
private readonly CatalogLookupDataService<CatalogType, CatalogTypeResponse> _catalogTypeService;
|
||||||
private ILogger<CachedCatalogTypeServiceDecorator> _logger;
|
private ILogger<CachedCatalogTypeServiceDecorator> _logger;
|
||||||
|
|
||||||
public CachedCatalogTypeServiceDecorator(ILocalStorageService localStorageService,
|
public CachedCatalogTypeServiceDecorator(ILocalStorageService localStorageService,
|
||||||
CatalogTypeService catalogTypeService,
|
CatalogLookupDataService<CatalogType, CatalogTypeResponse> catalogTypeService,
|
||||||
ILogger<CachedCatalogTypeServiceDecorator> logger)
|
ILogger<CachedCatalogTypeServiceDecorator> logger)
|
||||||
{
|
{
|
||||||
_localStorageService = localStorageService;
|
_localStorageService = localStorageService;
|
||||||
|
|||||||
@@ -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<CatalogBrandService> _logger;
|
|
||||||
private string _apiUrl;
|
|
||||||
|
|
||||||
public CatalogBrandService(HttpClient httpClient,
|
|
||||||
BaseUrlConfiguration baseUrlConfiguration,
|
|
||||||
ILogger<CatalogBrandService> logger)
|
|
||||||
{
|
|
||||||
_httpClient = httpClient;
|
|
||||||
_logger = logger;
|
|
||||||
_apiUrl = baseUrlConfiguration.ApiBase;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<CatalogBrand> GetById(int id)
|
|
||||||
{
|
|
||||||
return (await List()).FirstOrDefault(x => x.Id == id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<List<CatalogBrand>> List()
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Fetching brands from API.");
|
|
||||||
return (await _httpClient.GetFromJsonAsync<CatalogBrandResponse>($"{_apiUrl}catalog-brands"))?.CatalogBrands;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using BlazorShared;
|
using BlazorShared.Interfaces;
|
||||||
using BlazorShared.Interfaces;
|
|
||||||
using BlazorShared.Models;
|
using BlazorShared.Models;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -11,24 +10,20 @@ namespace BlazorAdmin.Services
|
|||||||
{
|
{
|
||||||
public class CatalogItemService : ICatalogItemService
|
public class CatalogItemService : ICatalogItemService
|
||||||
{
|
{
|
||||||
private readonly ICatalogBrandService _brandService;
|
private readonly ICatalogLookupDataService<CatalogBrand> _brandService;
|
||||||
private readonly ICatalogTypeService _typeService;
|
private readonly ICatalogLookupDataService<CatalogType> _typeService;
|
||||||
private readonly HttpService _httpService;
|
private readonly HttpService _httpService;
|
||||||
private readonly ILogger<CatalogItemService> _logger;
|
private readonly ILogger<CatalogItemService> _logger;
|
||||||
private string _apiUrl;
|
|
||||||
|
|
||||||
public CatalogItemService(ICatalogBrandService brandService,
|
public CatalogItemService(ICatalogLookupDataService<CatalogBrand> brandService,
|
||||||
ICatalogTypeService typeService,
|
ICatalogLookupDataService<CatalogType> typeService,
|
||||||
HttpService httpService,
|
HttpService httpService,
|
||||||
BaseUrlConfiguration baseUrlConfiguration,
|
|
||||||
ILogger<CatalogItemService> logger)
|
ILogger<CatalogItemService> logger)
|
||||||
{
|
{
|
||||||
_brandService = brandService;
|
_brandService = brandService;
|
||||||
_typeService = typeService;
|
_typeService = typeService;
|
||||||
|
|
||||||
_httpService = httpService;
|
_httpService = httpService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_apiUrl = baseUrlConfiguration.ApiBase;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<CatalogItem> Create(CreateCatalogItemRequest catalogItem)
|
public async Task<CatalogItem> Create(CreateCatalogItemRequest catalogItem)
|
||||||
|
|||||||
48
src/BlazorAdmin/Services/CatalogLookupDataService.cs
Normal file
48
src/BlazorAdmin/Services/CatalogLookupDataService.cs
Normal file
@@ -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<TLookupData, TReponse>
|
||||||
|
: ICatalogLookupDataService<TLookupData>
|
||||||
|
where TLookupData : LookupData
|
||||||
|
where TReponse : ILookupDataResponse<TLookupData>
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly HttpClient _httpClient;
|
||||||
|
private readonly ILogger<CatalogLookupDataService<TLookupData, TReponse>> _logger;
|
||||||
|
private readonly string _apiUrl;
|
||||||
|
|
||||||
|
public CatalogLookupDataService(HttpClient httpClient,
|
||||||
|
BaseUrlConfiguration baseUrlConfiguration,
|
||||||
|
ILogger<CatalogLookupDataService<TLookupData, TReponse>> logger)
|
||||||
|
{
|
||||||
|
_httpClient = httpClient;
|
||||||
|
_logger = logger;
|
||||||
|
_apiUrl = baseUrlConfiguration.ApiBase;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<TLookupData> GetById(int id)
|
||||||
|
{
|
||||||
|
return (await List()).FirstOrDefault(x => x.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<TLookupData>> List()
|
||||||
|
{
|
||||||
|
var endpointName = typeof(TLookupData).GetCustomAttribute<EndpointAttribute>().Name;
|
||||||
|
_logger.LogInformation($"Fetching {typeof(TLookupData).Name} from API. Enpoint : {endpointName}");
|
||||||
|
|
||||||
|
var response = await _httpClient.GetFromJsonAsync<TReponse>($"{_apiUrl}{endpointName}");
|
||||||
|
return response.List;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<CatalogTypeService> _logger;
|
|
||||||
private string _apiUrl;
|
|
||||||
|
|
||||||
public CatalogTypeService(HttpClient httpClient,
|
|
||||||
BaseUrlConfiguration baseUrlConfiguration,
|
|
||||||
ILogger<CatalogTypeService> logger)
|
|
||||||
{
|
|
||||||
_httpClient = httpClient;
|
|
||||||
_logger = logger;
|
|
||||||
_apiUrl = baseUrlConfiguration.ApiBase;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<CatalogType> GetById(int id)
|
|
||||||
{
|
|
||||||
return (await List()).FirstOrDefault(x => x.Id == id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<List<CatalogType>> List()
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Fetching types from API.");
|
|
||||||
return (await _httpClient.GetFromJsonAsync<CatalogTypeResponse>($"{_apiUrl}catalog-types"))?.CatalogTypes;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using BlazorAdmin.Services;
|
using BlazorAdmin.Services;
|
||||||
using BlazorShared.Interfaces;
|
using BlazorShared.Interfaces;
|
||||||
|
using BlazorShared.Models;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace BlazorAdmin
|
namespace BlazorAdmin
|
||||||
@@ -8,10 +9,10 @@ namespace BlazorAdmin
|
|||||||
{
|
{
|
||||||
public static IServiceCollection AddBlazorServices(this IServiceCollection services)
|
public static IServiceCollection AddBlazorServices(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddScoped<ICatalogBrandService, CachedCatalogBrandServiceDecorator>();
|
services.AddScoped<ICatalogLookupDataService<CatalogBrand>, CachedCatalogBrandServiceDecorator>();
|
||||||
services.AddScoped<CatalogBrandService>();
|
services.AddScoped<CatalogLookupDataService<CatalogBrand, CatalogBrandResponse>>();
|
||||||
services.AddScoped<ICatalogTypeService, CachedCatalogTypeServiceDecorator>();
|
services.AddScoped<ICatalogLookupDataService<CatalogType>, CachedCatalogTypeServiceDecorator>();
|
||||||
services.AddScoped<CatalogTypeService>();
|
services.AddScoped<CatalogLookupDataService<CatalogType, CatalogTypeResponse>>();
|
||||||
services.AddScoped<ICatalogItemService, CachedCatalogItemServiceDecorator>();
|
services.AddScoped<ICatalogItemService, CachedCatalogItemServiceDecorator>();
|
||||||
services.AddScoped<CatalogItemService>();
|
services.AddScoped<CatalogItemService>();
|
||||||
|
|
||||||
|
|||||||
9
src/BlazorShared/Attributes/EndpointAttribute.cs
Normal file
9
src/BlazorShared/Attributes/EndpointAttribute.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace BlazorShared.Attributes
|
||||||
|
{
|
||||||
|
public class EndpointAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using BlazorShared.Models;
|
|
||||||
|
|
||||||
namespace BlazorShared.Interfaces
|
|
||||||
{
|
|
||||||
public interface ICatalogBrandService
|
|
||||||
{
|
|
||||||
Task<List<CatalogBrand>> List();
|
|
||||||
Task<CatalogBrand> GetById(int id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
12
src/BlazorShared/Interfaces/ICatalogLookupDataService.cs
Normal file
12
src/BlazorShared/Interfaces/ICatalogLookupDataService.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using BlazorShared.Models;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace BlazorShared.Interfaces
|
||||||
|
{
|
||||||
|
public interface ICatalogLookupDataService<TLookupData> where TLookupData : LookupData
|
||||||
|
{
|
||||||
|
Task<List<TLookupData>> List();
|
||||||
|
Task<TLookupData> GetById(int id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
using BlazorShared.Models;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace BlazorShared.Interfaces
|
|
||||||
{
|
|
||||||
public interface ICatalogTypeService
|
|
||||||
{
|
|
||||||
Task<List<CatalogType>> List();
|
|
||||||
Task<CatalogType> GetById(int id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
10
src/BlazorShared/Interfaces/ILookupDataResponse.cs
Normal file
10
src/BlazorShared/Interfaces/ILookupDataResponse.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using BlazorShared.Models;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace BlazorShared.Interfaces
|
||||||
|
{
|
||||||
|
public interface ILookupDataResponse<TLookupData> where TLookupData : LookupData
|
||||||
|
{
|
||||||
|
List<TLookupData> List { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
namespace BlazorShared.Models
|
using BlazorShared.Attributes;
|
||||||
|
|
||||||
|
namespace BlazorShared.Models
|
||||||
{
|
{
|
||||||
|
[Endpoint(Name = "catalog-brands")]
|
||||||
public class CatalogBrand : LookupData
|
public class CatalogBrand : LookupData
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
using System.Collections.Generic;
|
using BlazorShared.Interfaces;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace BlazorShared.Models
|
namespace BlazorShared.Models
|
||||||
{
|
{
|
||||||
public class CatalogBrandResponse
|
public class CatalogBrandResponse : ILookupDataResponse<CatalogBrand>
|
||||||
{
|
{
|
||||||
public List<CatalogBrand> CatalogBrands { get; set; } = new List<CatalogBrand>();
|
[JsonPropertyName("CatalogBrands")]
|
||||||
|
public List<CatalogBrand> List { get; set; } = new List<CatalogBrand>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
namespace BlazorShared.Models
|
using BlazorShared.Attributes;
|
||||||
|
|
||||||
|
namespace BlazorShared.Models
|
||||||
{
|
{
|
||||||
|
[Endpoint(Name = "catalog-types")]
|
||||||
public class CatalogType : LookupData
|
public class CatalogType : LookupData
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
using System.Collections.Generic;
|
using BlazorShared.Interfaces;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace BlazorShared.Models
|
namespace BlazorShared.Models
|
||||||
{
|
{
|
||||||
public class CatalogTypeResponse
|
public class CatalogTypeResponse : ILookupDataResponse<CatalogType>
|
||||||
{
|
{
|
||||||
public List<CatalogType> CatalogTypes { get; set; } = new List<CatalogType>();
|
|
||||||
|
[JsonPropertyName("CatalogTypes")]
|
||||||
|
public List<CatalogType> List { get; set; } = new List<CatalogType>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
namespace BlazorShared.Models
|
namespace BlazorShared.Models
|
||||||
{
|
{
|
||||||
public abstract class LookupData
|
public abstract class LookupData
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user