Browse Source
* 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>main
committed by
GitHub
18 changed files with 122 additions and 140 deletions
@ -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; |
|||
} |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
@ -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,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<CatalogBrand> |
|||
{ |
|||
public List<CatalogBrand> CatalogBrands { get; set; } = new List<CatalogBrand>(); |
|||
[JsonPropertyName("CatalogBrands")] |
|||
public List<CatalogBrand> List { get; set; } = new List<CatalogBrand>(); |
|||
} |
|||
} |
|||
|
|||
@ -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<CatalogType> |
|||
{ |
|||
public List<CatalogType> CatalogTypes { get; set; } = new List<CatalogType>(); |
|||
|
|||
[JsonPropertyName("CatalogTypes")] |
|||
public List<CatalogType> List { get; set; } = new List<CatalogType>(); |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue