using System.Linq; using EntitlementDAL.DTOs; using EntitlementDAL.Interfaces; using EntitlementDAL.QueryBuilders; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; namespace EntitlementDAL.Implementations; public class AgreementDAL : IAgreementDAL { private readonly IQueryExecutor _QueryExecutor; public AgreementDAL(IQueryExecutor queryExecutor) => _QueryExecutor = queryExecutor; public async Task GetTypeByCodeAsync(string typeCode, LoginDTO login, CancellationToken ct) { var rows = await _QueryExecutor.QueryAsync( login, AgreementQB.GET_TYPE_BY_CODE, new { TypeCode = typeCode }, cancellationToken: ct).ConfigureAwait(false); return rows.FirstOrDefault(); } public async Task InsertTypeAsync(AgreementTypeDTO dto, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, AgreementQB.INSERT_TYPE, dto, cancellationToken: ct).ConfigureAwait(false); public async Task> GetTypeListAsync(LoginDTO login, CancellationToken ct) { var rows = await _QueryExecutor.QueryAsync( login, AgreementQB.GET_TYPE_LIST, cancellationToken: ct).ConfigureAwait(false); return rows.ToList(); } public async Task> GetApplicableVersionsAsync( IEnumerable typeCodes, string jurisdictionCode, LoginDTO login, CancellationToken ct) { var rows = await _QueryExecutor.QueryAsync( login, AgreementQB.GET_APPLICABLE_VERSIONS, new { TypeCodes = typeCodes, JurisdictionCode = jurisdictionCode }, cancellationToken: ct).ConfigureAwait(false); return rows.ToList(); } public async Task GetVersionByIdAsync(int agreementVersionId, LoginDTO login, CancellationToken ct) { var rows = await _QueryExecutor.QueryAsync( login, AgreementQB.GET_VERSION_BY_ID, new { AgreementVersionId = agreementVersionId }, cancellationToken: ct).ConfigureAwait(false); return rows.FirstOrDefault(); } public async Task> GetRequiredIndividualVersionsAsync(string jurisdictionCode, LoginDTO login, CancellationToken ct) { var rows = await _QueryExecutor.QueryAsync( login, AgreementQB.GET_REQUIRED_INDIVIDUAL_VERSIONS, new { JurisdictionCode = jurisdictionCode }, cancellationToken: ct).ConfigureAwait(false); return rows.ToList(); } public async Task> GetVersionListByTypeAsync(int agreementTypeId, LoginDTO login, CancellationToken ct) { var rows = await _QueryExecutor.QueryAsync( login, AgreementQB.GET_VERSION_LIST_BY_TYPE, new { AgreementTypeId = agreementTypeId }, cancellationToken: ct).ConfigureAwait(false); return rows.ToList(); } public async Task InsertVersionAsync(AgreementVersionDTO dto, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, AgreementQB.INSERT_VERSION, dto, cancellationToken: ct).ConfigureAwait(false); public async Task UpdateVersionStatusAsync(int agreementVersionId, byte versionStatus, int modifiedById, DateTime modifiedOn, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteAsync( login, AgreementQB.UPDATE_VERSION_STATUS, new { AgreementVersionId = agreementVersionId, VersionStatus = versionStatus, ModifiedById = modifiedById, ModifiedOn = modifiedOn }, cancellationToken: ct).ConfigureAwait(false); public async Task PublishVersionAsync(int agreementVersionId, string? contentHash, int modifiedById, DateTime modifiedOn, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteAsync( login, AgreementQB.PUBLISH_VERSION, new { AgreementVersionId = agreementVersionId, VersionStatus = (byte)DTOs.AgreementVersionStatus.Published, ContentHash = contentHash, ModifiedById = modifiedById, ModifiedOn = modifiedOn }, cancellationToken: ct).ConfigureAwait(false); public async Task InsertAcceptanceAsync(AgreementAcceptanceDTO dto, LoginDTO login, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, AgreementQB.INSERT_ACCEPTANCE, dto, cancellationToken: ct).ConfigureAwait(false); public async Task> GetAcceptancesBySubjectAsync(byte subjectType, int subjectId, LoginDTO login, CancellationToken ct) { var rows = await _QueryExecutor.QueryAsync( login, AgreementQB.GET_ACCEPTANCES_BY_SUBJECT, new { SubjectType = subjectType, SubjectId = subjectId }, cancellationToken: ct).ConfigureAwait(false); return rows.ToList(); } }