using AmpDAL.DTO.RegisterApi; using AmpDAL.Query.RegisterApi; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; namespace AmpDAL.CustomCode.RegisterApi; public class RegisterApiDAL(IQueryExecutor _qe) : IRegisterApiDAL { public async Task InsertApiAsync( RegisterApiSaveDTO dto, int apiDomainId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_API, new { dto.ApiId, TenantId = login.ClientId, dto.ApiKey, dto.ApiName, dto.Description, dto.AboutApi, dto.KeyFeatures, dto.UseCases, dto.QuickStart, dto.ApiKind, dto.ApiType, dto.Criticality, dto.LifecycleStatus, dto.VisibilityLevel, dto.ApiProviderType, dto.ExposureType, dto.IsHeadlessEnabled, dto.OwnerTeam, dto.BusinessOwner, dto.RuntimeType, dto.Technology, dto.RepoUrl, dto.PipelineRef, dto.ProviderId, dto.CategoryId, ApiDomainId = apiDomainId, dto.Geography, dto.RegulatoryImpact, dto.RiskRating, dto.SlaReference, ApiRemarks = dto.ApiRemarks, ApiSortOrder = dto.ApiSortOrder == 0 ? (short)9999 : dto.ApiSortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task InsertVersionAsync( RegisterApiSaveDTO dto, int apiVersionId, int apiId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_VERSION, new { ApiVersionId = apiVersionId, TenantId = login.ClientId, ApiId = apiId, dto.VersionNumber, dto.VersionStatus, dto.IsBreaking, VersionFromDate = dto.VersionFromDate == default ? DateTime.UtcNow : dto.VersionFromDate, dto.VersionToDate, dto.SunsetDate, dto.VersionNotes, dto.VersionRemarks, dto.VersionDescription, VersionSortOrder = dto.VersionSortOrder == 0 ? (short)9999 : dto.VersionSortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task InsertInterfaceAsync( RegisterApiSaveDTO dto, int apiInterfaceId, int apiId, int apiVersionId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_INTERFACE, new { ApiInterfaceId = apiInterfaceId, TenantId = login.ClientId, ApiId = apiId, ApiVersionId = apiVersionId, dto.InterfaceName, dto.InterfaceType, dto.SpecType, dto.InterfaceMethod, dto.ApiPath, dto.InterfaceDescription, dto.SpecLocation, dto.ContentHash, dto.LastParsedAt, dto.InterfaceSource, dto.InterfaceRemarks, ParsedSuccessfully = dto.ParsedSuccessfully, InterfaceSortOrder = dto.InterfaceSortOrder == 0 ? (short)9999 : dto.InterfaceSortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task InsertSchemaAsync( RegisterApiSaveDTO dto, int apiSchemaId, int apiId, int apiInterfaceId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_SCHEMA, new { ApiSchemaId = apiSchemaId, TenantId = login.ClientId, ApiId = apiId, ApiInterfaceId = apiInterfaceId, dto.SchemaName, dto.SchemaDescription, ApiName = dto.ApiName, dto.SpecificationSource, dto.SpecificationUrl, dto.RequestJson, dto.ResponseJson, dto.SchemaJson, dto.SchemaRemarks, SchemaSortOrder = dto.SchemaSortOrder == 0 ? (short)9999 : dto.SchemaSortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task InsertDomainAsync( RegisterApiSaveDTO dto, int apiDomainId, int apiId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_DOMAIN, new { ApiDomainId = apiDomainId, TenantId = login.ClientId, ApiId = apiId, dto.DomainName, ParentDomainId = dto.ParentDomainId == 0 ? apiDomainId : dto.ParentDomainId, dto.DomainRemarks, DomainSortOrder = dto.DomainSortOrder == 0 ? (short)9999 : dto.DomainSortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task UpdateApiDomainIdAsync(int apiId, int apiDomainId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync(login, RegisterApiQB.UPDATE_API_DOMAIN_ID, new { ApiId = apiId, TenantId = login.ClientId, ApiDomainId = apiDomainId, UserId = login.UserId }).ConfigureAwait(false); public async Task GetDomainIdByNameAsync(string domainName, int tenantId, LoginDTO login, CancellationToken ct) => await _qe.QuerySingleAsync(login, RegisterApiQB.GET_DOMAIN_ID_BY_NAME, new { DomainName = domainName, TenantId = tenantId }, cancellationToken: ct).ConfigureAwait(false); public async Task UpdateCapabilityDomainIdAsync(int apiId, int apiDomainId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync(login, RegisterApiQB.UPDATE_CAPABILITY_DOMAIN_ID, new { ApiId = apiId, TenantId = login.ClientId, ApiDomainId = apiDomainId, UserId = login.UserId }).ConfigureAwait(false); public async Task DeleteOrphanedDomainsAsync(int tenantId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_ORPHANED_DOMAINS, new { TenantId = tenantId }).ConfigureAwait(false); public async Task InsertCapabilityAsync( RegisterApiSaveDTO dto, int apiCapabilityId, int apiId, int apiDomainId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_CAPABILITY, new { ApiCapabilityId = apiCapabilityId, TenantId = login.ClientId, ApiId = apiId, ApiDomainId = apiDomainId, dto.CapabilityName, CapabilityDescription = dto.CapabilityDescription, CapabilityRemarks = dto.CapabilityRemarks, CapabilitySortOrder = dto.CapabilitySortOrder == 0 ? (short)9999 : dto.CapabilitySortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task InsertCapabilityLinkAsync( int apiCapabilityLinkId, int apiId, int apiCapabilityId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_CAPABILITY_LINK, new { ApiCapabilityLinkId = apiCapabilityLinkId, TenantId = login.ClientId, ApiId = apiId, ApiCapabilityId = apiCapabilityId, UserId = login.UserId }).ConfigureAwait(false); public async Task InsertArtifactAsync( RegisterApiSaveDTO dto, int apiArtifactId, int apiId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_ARTIFACT, new { ApiArtifactId = apiArtifactId, TenantId = login.ClientId, ApiId = apiId, dto.ArtifactType, ArtifactTitle = dto.ArtifactTitle, ArtifactLocation = dto.ArtifactLocation, ArtifactRemarks = dto.ArtifactRemarks, ArtifactSortOrder = dto.ArtifactSortOrder == 0 ? (short)9999 : dto.ArtifactSortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task InsertOperationAsync( RegisterApiSaveDTO dto, int apiOperationId, int apiId, int apiInterfaceId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_OPERATION, new { ApiOperationId = apiOperationId, TenantId = login.ClientId, ApiId = apiId, ApiInterfaceId = apiInterfaceId, dto.OperationKey, OperationMethod = dto.OperationMethod ?? "GET", dto.OperationPath, dto.OperationSummary, dto.OperationRemarks, OperationSortOrder = dto.OperationSortOrder == 0 ? (short)9999 : dto.OperationSortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task InsertParameterAsync( RegisterApiSaveDTO dto, int parameterId, int apiOperationId, int apiId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_PARAMETER, new { ParameterId = parameterId, TenantId = login.ClientId, ApiId = apiId, ApiOperationId = apiOperationId, dto.ParameterName, ParameterInLocation = dto.ParameterInLocation ?? "Query", ParameterDataType = dto.ParameterDataType ?? "string", dto.ParameterIsRequired, dto.ParameterRemarks, ParameterSortOrder = dto.ParameterSortOrder == 0 ? (short)9999 : dto.ParameterSortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task InsertExposurePolicyAsync( RegisterApiSaveDTO dto, int policyId, int apiId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync(login, RegisterApiQB.INSERT_EXPOSURE_POLICY, new { ApiExposurePolicyId = policyId, TenantId = login.ClientId, ApiId = apiId, dto.AllowPartnerAccess, dto.AllowPublicAccess, dto.BillingCategory, dto.PolicySlaReference, dto.PolicyRemarks, PolicySortOrder = dto.PolicySortOrder == 0 ? (short)9999 : dto.PolicySortOrder, UserId = login.UserId }).ConfigureAwait(false); public async Task GetAsync( int apiId, int tenantId, LoginDTO login, CancellationToken ct) => await _qe.QuerySingleAsync( login, RegisterApiQB.GET_REGISTER_API, new { ApiId = apiId, TenantId = tenantId }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetListAsync( int tenantId, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, RegisterApiQB.GET_REGISTER_API_LIST, new { TenantId = tenantId }, cancellationToken: ct).ConfigureAwait(false); return result.ToList(); } public async Task> GetSelectListAsync( int tenantId, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, RegisterApiQB.GET_SELECT_LIST, new { TenantId = tenantId }, cancellationToken: ct).ConfigureAwait(false); return result.ToList(); } public async Task DeleteAsync(int apiId, int tenantId, LoginDTO login, CancellationToken ct) { var p = new { ApiId = apiId, TenantId = tenantId }; await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_PARAMETERS, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_OPERATIONS, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_ARTIFACT, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_SCHEMA, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_INTERFACE, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_CAPABILITY_LINK, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_CAPABILITY, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_EXPOSURE_POLICY, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_VERSION, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_DOMAIN, p).ConfigureAwait(false); await _qe.ExecuteAsync(login, RegisterApiQB.DELETE_API, p).ConfigureAwait(false); } }