2024-11-15 09:28:21 -07:00

39 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;
}
}
}