2024-09-09 10:00:49 -07:00

60 lines
2.1 KiB
C#

using FabApprovalWorkerService.Models;
using FabApprovalWorkerService.Utilities;
using System.Text;
namespace FabApprovalWorkerService.Services;
public interface IMRBService {
Task<bool> MRBNumberIsActive(int number);
Task<MRB> GetMRBById(int id);
}
public class MRBService : IMRBService {
private readonly ILogger<MRBService> _logger;
private readonly IDalService _dalService;
public MRBService(ILogger<MRBService> logger, IDalService dalService) {
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
_dalService = dalService ?? throw new ArgumentNullException("IDalService not injected");
}
public async Task<bool> MRBNumberIsActive(int number) {
try {
_logger.LogInformation($"Attempting to determine if {number} is an active MRB#");
if (number <= 0) return false;
StringBuilder queryBuilder = new();
queryBuilder.Append($"select count(MRBNumber) as count from MRB where MRBNumber={number} and ");
queryBuilder.Append($"(CloseDate is null or CloseDate >= '{DateTimeUtilities.MAX_DT.ToString("yyyy-MM-dd HH:mm")}')");
int rowsReturned = (await _dalService.QueryAsync<int>(queryBuilder.ToString())).FirstOrDefault();
return rowsReturned > 0;
} catch (Exception ex) {
_logger.LogError($"Unable to determine if {number} is an active MRB#, because {ex.Message}");
throw;
}
}
public async Task<MRB> GetMRBById(int id) {
try {
_logger.LogInformation("Attempting to get an MRB by ID");
if (id < 0) throw new ArgumentException("Invalid MRB number");
string sql = $"select * from MRB where MRBNumber={id}";
MRB? mrb = (await _dalService.QueryAsync<MRB>(sql)).FirstOrDefault();
if (mrb is null) throw new Exception($"Unable to get MRB {id}");
return mrb;
} catch (Exception ex) {
_logger.LogError($"Unable to get MRB# {id}, because {ex.Message}");
throw;
}
}
}