using System.Data.Common; using EntitlementDAL.DTOs; using EntitlementDAL.Interfaces; using EntitlementDAL.QueryBuilders; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; namespace EntitlementDAL.Implementations; public class DemoDAL : IDemoDAL { private readonly IQueryExecutor _QueryExecutor; public DemoDAL(IQueryExecutor queryExecutor) => _QueryExecutor = queryExecutor; public async Task GetVariantByIdAsync(int demoVariantId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QuerySingleAsync( login, DemoQB.VARIANT_GET_BY_ID, new { DemoVariantId = demoVariantId }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetVariantListAsync(LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync(login, DemoQB.VARIANT_GET_LIST, null, cancellationToken: ct).ConfigureAwait(false); public async Task SaveVariantAsync(DemoVariantDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.VARIANT_INSERT, dto, tx, cancellationToken: ct).ConfigureAwait(false); public async Task UpdateVariantAsync(DemoVariantDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.VARIANT_UPDATE, dto, tx, cancellationToken: ct).ConfigureAwait(false); public async Task GetPoolInstanceByIdAsync(int demoPoolInstanceId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QuerySingleAsync( login, DemoQB.POOL_INSTANCE_GET_BY_ID, new { DemoPoolInstanceId = demoPoolInstanceId }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetPoolInstancesByVariantAsync(int demoVariantId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, DemoQB.POOL_INSTANCE_GET_BY_VARIANT, new { DemoVariantId = demoVariantId }, cancellationToken: ct).ConfigureAwait(false); public async Task GetOneAvailablePoolInstanceAsync(int demoVariantId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QuerySingleAsync( login, DemoQB.POOL_INSTANCE_GET_ONE_AVAILABLE, new { DemoVariantId = demoVariantId }, cancellationToken: ct).ConfigureAwait(false); public async Task SavePoolInstanceAsync(DemoPoolInstanceDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.POOL_INSTANCE_INSERT, dto, tx, cancellationToken: ct).ConfigureAwait(false); public async Task UpdatePoolInstanceAsync(DemoPoolInstanceDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.POOL_INSTANCE_UPDATE, dto, tx, cancellationToken: ct).ConfigureAwait(false); public async Task UpdatePoolInstanceStateAsync(int demoPoolInstanceId, byte poolState, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.POOL_INSTANCE_UPDATE_STATE, new { DemoPoolInstanceId = demoPoolInstanceId, PoolState = poolState, ModifiedById = login.UserId, ModifiedOn = DateTime.UtcNow }, tx, cancellationToken: ct).ConfigureAwait(false); public async Task TryClaimPoolInstanceAsync(int demoPoolInstanceId, LoginDTO login, DbTransaction tx, CancellationToken ct) { var rows = await _QueryExecutor.ExecuteAsync(login, DemoQB.POOL_INSTANCE_TRY_CLAIM, new { DemoPoolInstanceId = demoPoolInstanceId, ModifiedById = login.UserId, ModifiedOn = DateTime.UtcNow }, tx, cancellationToken: ct).ConfigureAwait(false); return rows > 0; } public async Task GetSessionByIdAsync(int demoSessionId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QuerySingleAsync( login, DemoQB.SESSION_GET_BY_ID, new { DemoSessionId = demoSessionId }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetSessionListByOwnerAsync(int ownerUserId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, DemoQB.SESSION_GET_LIST_BY_OWNER, new { OwnerUserId = ownerUserId }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetSessionsApproachingExpiryAsync(DateTime warningThreshold, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, DemoQB.SESSION_GET_APPROACHING_EXPIRY, new { WarningThreshold = warningThreshold }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetExpiredSessionsAsync(DateTime now, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, DemoQB.SESSION_GET_EXPIRED, new { Now = now }, cancellationToken: ct).ConfigureAwait(false); public async Task SaveSessionAsync(DemoSessionDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.SESSION_INSERT, dto, tx, cancellationToken: ct).ConfigureAwait(false); public async Task RequestExtensionAsync(int demoSessionId, DateTime requestedOn, string? comment, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.SESSION_REQUEST_EXTENSION, new { DemoSessionId = demoSessionId, ExtensionRequestedOn = requestedOn, ExtensionComment = comment, ModifiedById = login.UserId, ModifiedOn = DateTime.UtcNow }, tx, cancellationToken: ct).ConfigureAwait(false); public async Task ApproveExtensionAsync(int demoSessionId, DateTime newExpiresOn, int approvedById, DateTime approvedOn, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.SESSION_APPROVE_EXTENSION, new { DemoSessionId = demoSessionId, ExpiresOn = newExpiresOn, ExtensionApprovedById = approvedById, ExtensionApprovedOn = approvedOn, ModifiedById = login.UserId, ModifiedOn = DateTime.UtcNow }, tx, cancellationToken: ct).ConfigureAwait(false); public async Task RejectExtensionAsync(int demoSessionId, int rejectedById, DateTime rejectedOn, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.SESSION_REJECT_EXTENSION, new { DemoSessionId = demoSessionId, ExtensionApprovedById = rejectedById, ExtensionApprovedOn = rejectedOn, ModifiedById = login.UserId, ModifiedOn = DateTime.UtcNow }, tx, cancellationToken: ct).ConfigureAwait(false); public async Task MarkWarningIssuedAsync(int demoSessionId, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.SESSION_MARK_WARNING_ISSUED, new { DemoSessionId = demoSessionId, ModifiedById = login.UserId, ModifiedOn = DateTime.UtcNow }, tx, cancellationToken: ct).ConfigureAwait(false); public async Task MarkExpiredAsync(int demoSessionId, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.SESSION_MARK_EXPIRED, new { DemoSessionId = demoSessionId, ModifiedById = login.UserId, ModifiedOn = DateTime.UtcNow }, tx, cancellationToken: ct).ConfigureAwait(false); public async Task MarkTornDownAsync(int demoSessionId, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.SESSION_MARK_TORN_DOWN, new { DemoSessionId = demoSessionId, ModifiedById = login.UserId, ModifiedOn = DateTime.UtcNow }, tx, cancellationToken: ct).ConfigureAwait(false); public async Task InsertSessionEventAsync(DemoSessionEventDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.EVENT_INSERT, dto, tx, cancellationToken: ct).ConfigureAwait(false); public async Task> GetSessionEventsAsync(int demoSessionId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, DemoQB.EVENT_GET_FOR_SESSION, new { DemoSessionId = demoSessionId }, cancellationToken: ct).ConfigureAwait(false); public async Task GetPreferenceByClientIdAsync(int clientId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QuerySingleAsync( login, DemoQB.PREFERENCE_GET_BY_CLIENT, new { ClientId = clientId }, cancellationToken: ct).ConfigureAwait(false); public async Task SavePreferenceAsync(DemoPreferenceDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync(login, DemoQB.PREFERENCE_INSERT, dto, tx, cancellationToken: ct).ConfigureAwait(false); }