71 lines
3.6 KiB
C#
71 lines
3.6 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using NTP.Application.Registrar;
|
|
|
|
namespace NTP.Infrastructure.Persistence.Repositories.Registrar;
|
|
|
|
/// <summary>
|
|
/// The lists of the registrar forms (legacy lookups.cfc: LT_PUBLIC_VIEW by LT_TYPE and the NATIONALITY table), LT_CAPTION texts and the ATTACHMENTS rows
|
|
/// (attachments.cfc getAttachment). All of them are in the NTP schema.
|
|
/// </summary>
|
|
internal sealed class RegistrarLookupRepository(INtpDbSession session, ILogger<RegistrarLookupRepository> logger)
|
|
: NtpRepositoryBase(session, logger), IRegistrarLookupRepository
|
|
{
|
|
/// <summary>The LT_TYPEs the three forms use (lookups.cfc fillLookupArray list).</summary>
|
|
private static readonly string[] Types =
|
|
[
|
|
"TRAILER_TYPE", "TRUCK_TYPE", "TRAILER_USAGE_TYPE", "TRUCK_USAGE_TYPE", "COLOR", "VEHICLE_MANIFACTURER", "DRIVER_REVOKE_REASON",
|
|
"TRUCK_REVOKE_REASON", "TRAILER_REVOKE_REASON", "JORDAN_GOVERNATE", "BLOOD_GROUP", "DRIVER_LICENSE_TYPE",
|
|
];
|
|
|
|
private sealed class ListRow
|
|
{
|
|
public string? ListName { get; set; }
|
|
public string? Code { get; set; }
|
|
public string? Ar { get; set; }
|
|
public string? En { get; set; }
|
|
}
|
|
|
|
public async Task<IReadOnlyDictionary<string, IReadOnlyList<LookupItem>>> GetListsAsync(CancellationToken ct = default)
|
|
{
|
|
var names = string.Join(",", Types.Select((_, i) => ":T" + i));
|
|
var p = new Dapper.DynamicParameters();
|
|
for (var i = 0; i < Types.Length; i++) p.Add("T" + i, Types[i]);
|
|
var rows = await QueryAsync<ListRow>(
|
|
$"SELECT p.LT_TYPE AS ListName, p.CODE AS Code, p.AR AS Ar, p.EN AS En FROM LT_PUBLIC_VIEW p WHERE p.LT_TYPE IN ({names}) ORDER BY p.LT_TYPE, p.ORDERING, p.AR", p, ct);
|
|
|
|
// the legacy shows the caption of the code, or the code itself when there is none
|
|
var lists = rows.GroupBy(r => r.ListName ?? string.Empty).ToDictionary(
|
|
g => g.Key,
|
|
g => (IReadOnlyList<LookupItem>)g.Where(r => !string.IsNullOrEmpty(r.Code))
|
|
.Select(r => new LookupItem(r.Code!, Pick(r.Ar, r.Code!), Pick(r.En, Pick(r.Ar, r.Code!)))).ToList());
|
|
|
|
var nationalities = await QueryAsync<ListRow>("SELECT CODE AS Code, NAME AS Ar FROM NATIONALITY ORDER BY ID", null, ct);
|
|
lists["NATIONALITY"] = nationalities.Where(r => !string.IsNullOrEmpty(r.Code))
|
|
.Select(r => new LookupItem(r.Code!, Pick(r.Ar, r.Code!), Pick(r.Ar, r.Code!))).ToList();
|
|
foreach (var t in Types) lists.TryAdd(t, []);
|
|
return lists;
|
|
}
|
|
|
|
public Task<IReadOnlyList<RegistrarAttachmentRow>> GetAttachmentsAsync(string entity, long id, CancellationToken ct = default) =>
|
|
QueryAsync<RegistrarAttachmentRow>(
|
|
"SELECT ID AS Id, FILENAME AS FileName FROM ATTACHMENTS WHERE ENTITYID = :Id AND ENTITYNAME = :Entity ORDER BY ID", new { Id = id, Entity = entity }, ct);
|
|
|
|
public Task<string?> GetCaptionAsync(string code, CancellationToken ct = default) =>
|
|
QueryFirstAsync<string>("SELECT AR FROM LT_CAPTION WHERE CODE = :Code AND AR IS NOT NULL", new { Code = code }, ct);
|
|
|
|
private static string Pick(string? text, string fallback) => string.IsNullOrWhiteSpace(text) ? fallback : text.Trim();
|
|
}
|
|
|
|
/// <summary>Binds configuration section "Registrar".</summary>
|
|
internal sealed class RegistrarSettingsService : IRegistrarSettingsService
|
|
{
|
|
public RegistrarSettingsService(IConfiguration configuration)
|
|
{
|
|
Value = new RegistrarOptions();
|
|
configuration.GetSection(RegistrarOptions.SectionName).Bind(Value);
|
|
}
|
|
|
|
public RegistrarOptions Value { get; }
|
|
}
|