using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Threading; using System.Threading.Tasks; using FrameworkDAL.CustomCode.EntityViewer.DTOs; using FrameworkDAL.Query.EntityViewer; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; namespace FrameworkDAL.CustomCode.EntityViewer { public class EntityLayoutDAL : IEntityLayoutDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public EntityLayoutDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } public async Task SaveLayout(EntityLayoutDTO dto, LoginDTO loginDTO, CancellationToken ct = default) { try { dto.CreatedById = loginDTO.UserId; dto.ModifiedById = loginDTO.UserId; return await _QueryExecutor.ExecuteIdentityAsync(loginDTO, EntityLayoutQB.SAVE_LAYOUT, dto); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage)); } } public async Task UpdateLayout(EntityLayoutDTO dto, LoginDTO loginDTO, CancellationToken ct = default) { try { dto.ModifiedById = loginDTO.UserId; return await _QueryExecutor.ExecuteAsync(loginDTO, EntityLayoutQB.UPDATE_LAYOUT_HEADER, dto, cancellationToken: ct); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage)); } } public async Task DeleteLayout( int entityLayoutId, int modifiedById, LoginDTO loginDTO, CancellationToken ct = default) { try { return await _QueryExecutor.ExecuteAsync(loginDTO, EntityLayoutQB.DELETE_LAYOUT_HEADER, new { EntityLayoutId = entityLayoutId, ModifiedById = modifiedById }, cancellationToken: ct); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage)); } } public async Task GetLayoutHeaderById( int entityLayoutId, LoginDTO loginDTO, CancellationToken ct = default) { try { return await _QueryExecutor.QuerySingleAsync( loginDTO, EntityLayoutQB.GET_LAYOUT_HEADER_BY_ID, new { EntityLayoutId = entityLayoutId }, cancellationToken: ct); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.GetFetchingMessge)); } } public async Task GetActiveLayout( string entityCode, int tenantId, LoginDTO loginDTO, CancellationToken ct = default) { try { return await _QueryExecutor.QuerySingleAsync( loginDTO, EntityLayoutQB.GET_ACTIVE_LAYOUT, new { EntityCode = entityCode, TenantId = tenantId }, cancellationToken: ct); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.GetFetchingMessge)); } } public async Task SaveLayoutVersion( EntityLayoutVersionDTO dto, LoginDTO loginDTO, CancellationToken ct = default) { try { dto.CreatedById = loginDTO.UserId; dto.ModifiedById = loginDTO.UserId; return await _QueryExecutor.ExecuteIdentityAsync(loginDTO, EntityLayoutQB.SAVE_LAYOUT_VERSION, dto); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage)); } } public async Task UpdateLayoutVersion( EntityLayoutVersionDTO dto, LoginDTO loginDTO, CancellationToken ct = default) { try { dto.ModifiedById = loginDTO.UserId; return await _QueryExecutor.ExecuteAsync(loginDTO, EntityLayoutQB.UPDATE_LAYOUT_VERSION, dto, cancellationToken: ct); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage)); } } public async Task DeleteLayoutVersion( int entityLayoutVersionId, int modifiedById, LoginDTO loginDTO, CancellationToken ct = default) { try { return await _QueryExecutor.ExecuteAsync(loginDTO, EntityLayoutQB.DELETE_LAYOUT_VERSION, new { EntityLayoutVersionId = entityLayoutVersionId, ModifiedById = modifiedById }, cancellationToken: ct); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage)); } } public async Task> GetLayoutVersionList( int entityLayoutId, LoginDTO loginDTO, CancellationToken ct = default) { try { var result = await _QueryExecutor.QueryAsync( loginDTO, EntityLayoutQB.GET_LAYOUT_VERSION_LIST, new { EntityLayoutId = entityLayoutId }, cancellationToken: ct); return (result ?? []).ToList(); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.GetFetchingMessge)); } } public async Task> GetLayoutList( bool isComponent, LoginDTO loginDTO, CancellationToken ct = default) { try { var result = await _QueryExecutor.QueryAsync( loginDTO, EntityLayoutQB.GET_LAYOUT_LIST, new { IsComponent = isComponent }, cancellationToken: ct); return (result ?? []).ToList(); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.GetFetchingMessge)); } } public async Task ArchiveActiveVersion( int entityLayoutId, int modifiedById, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct = default) { try { await _QueryExecutor.ExecuteAsync(loginDTO, EntityLayoutQB.ARCHIVE_ACTIVE_VERSION, new { EntityLayoutId = entityLayoutId, ModifiedById = modifiedById }, tx, ct); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage)); } } public async Task ActivateVersion( int entityLayoutVersionId, int modifiedById, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct = default) { try { await _QueryExecutor.ExecuteAsync(loginDTO, EntityLayoutQB.ACTIVATE_VERSION, new { EntityLayoutVersionId = entityLayoutVersionId, ModifiedById = modifiedById }, tx, ct); } catch (Exception ex) { throw new Exception(await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage)); } } } }