Mike Phares 538b1f817e Align .editorconfig files
When debugging only
app.Services.GetRequiredService<IPCRBService>();

Injected AppSettings instead of using GetEnvironmentVariable at Services level

Get ready to use VSCode IDE
2024-12-03 12:23:56 -07:00

38 lines
1.4 KiB
C#

using Microsoft.Extensions.Caching.Memory;
namespace MesaFabApproval.API.Services;
public interface ICustomerService {
Task<IEnumerable<string>> GetCustomerNames();
}
public class CustomerService : ICustomerService {
private readonly ILogger<CustomerService> _logger;
private readonly IDalService _dalService;
private readonly IMemoryCache _cache;
public CustomerService(ILogger<CustomerService> logger, IDalService dalService, IMemoryCache cache) {
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
_dalService = dalService ?? throw new ArgumentNullException("IDalService not injected");
_cache = cache ?? throw new ArgumentNullException("IMemoryCache not injected");
}
public async Task<IEnumerable<string>> GetCustomerNames() {
try {
_logger.LogInformation("Attempting to get customer names");
IEnumerable<string>? customerNames = _cache.Get<IEnumerable<string>>("allCustomerNames");
if (customerNames is null) {
string sql = "select ProductFamily from ProductFamilies;";
customerNames = (await _dalService.QueryAsync<string>(sql)).ToList();
}
return customerNames;
} catch (Exception ex) {
_logger.LogError($"Unable to get customer names, because {ex.Message}");
throw;
}
}
}