From 6a6eca46084a718b561bad03fb0088ff1deae881 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Sat, 26 Mar 2022 16:52:37 +0800 Subject: [PATCH] feat: add webHooks definition queries that are available --- .../IWebhookSubscriptionAppService.cs | 3 ++ .../WebhooksAvailableDto.cs | 8 +++++ .../WebhookSubscriptionAppService.cs | 30 ++++++++++++++++++- .../WebhooksManagement/WebhookSubscription.cs | 7 ++++- .../WebhookSubscriptionController.cs | 7 +++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhooksAvailableDto.cs diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionAppService.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionAppService.cs index f25ede15b..f250b6418 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionAppService.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/IWebhookSubscriptionAppService.cs @@ -1,4 +1,6 @@ using System; +using System.Threading.Tasks; +using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; namespace LINGYUN.Abp.WebhooksManagement; @@ -11,4 +13,5 @@ public interface IWebhookSubscriptionAppService : WebhookSubscriptionCreateInput, WebhookSubscriptionUpdateInput> { + Task> GetAllAvailableWebhooksAsync(); } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhooksAvailableDto.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhooksAvailableDto.cs new file mode 100644 index 000000000..47b254b64 --- /dev/null +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application.Contracts/LINGYUN/Abp/WebhooksManagement/WebhooksAvailableDto.cs @@ -0,0 +1,8 @@ +namespace LINGYUN.Abp.WebhooksManagement; + +public class WebhooksAvailableDto +{ + public string Name { get; set; } + public string DisplayName { get; set; } + public string Description { get; set; } +} diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs index 7f984fc4d..6502b4725 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Application/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionAppService.cs @@ -1,8 +1,10 @@ -using LINGYUN.Abp.WebhooksManagement.Authorization; +using LINGYUN.Abp.Webhooks; +using LINGYUN.Abp.WebhooksManagement.Authorization; using LINGYUN.Abp.WebhooksManagement.Extensions; using Microsoft.AspNetCore.Authorization; using Newtonsoft.Json; using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Volo.Abp; @@ -13,11 +15,14 @@ namespace LINGYUN.Abp.WebhooksManagement; [Authorize(WebhooksManagementPermissions.WebhookSubscription.Default)] public class WebhookSubscriptionAppService : WebhooksManagementAppServiceBase, IWebhookSubscriptionAppService { + protected IWebhookDefinitionManager WebhookDefinitionManager { get; } protected IWebhookSubscriptionRepository SubscriptionRepository { get; } public WebhookSubscriptionAppService( + IWebhookDefinitionManager webhookDefinitionManager, IWebhookSubscriptionRepository subscriptionRepository) { + WebhookDefinitionManager = webhookDefinitionManager; SubscriptionRepository = subscriptionRepository; } @@ -85,9 +90,11 @@ public class WebhookSubscriptionAppService : WebhooksManagementAppServiceBase, I await CheckSubscribedAsync(input); } + subscription.SetSecret(input.Secret); subscription.SetWebhookUri(input.WebhookUri); subscription.SetWebhooks(input.ToSubscribedWebhooksString()); subscription.SetHeaders(input.ToWebhookHeadersString()); + subscription.IsActive = input.IsActive; await SubscriptionRepository.UpdateAsync(subscription); @@ -96,6 +103,27 @@ public class WebhookSubscriptionAppService : WebhooksManagementAppServiceBase, I return subscription.ToWebhookSubscriptionDto(); } + public async virtual Task> GetAllAvailableWebhooksAsync() + { + var webhooks = WebhookDefinitionManager.GetAll(); + var definitions = new List(); + + foreach (var webhookDefinition in webhooks) + { + if (await WebhookDefinitionManager.IsAvailableAsync(CurrentTenant.Id, webhookDefinition.Name)) + { + definitions.Add(new WebhooksAvailableDto + { + Name = webhookDefinition.Name, + Description = webhookDefinition.Description?.Localize(StringLocalizerFactory), + DisplayName = webhookDefinition.DisplayName?.Localize(StringLocalizerFactory) + }); + } + } + + return new ListResultDto(definitions.OrderBy(d => d.Name).ToList()); + } + protected async virtual Task CheckSubscribedAsync(WebhookSubscriptionCreateOrUpdateInput input) { foreach (var webhookName in input.Webhooks) diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSubscription.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSubscription.cs index 4d637e88a..50476fce4 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSubscription.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.Domain/LINGYUN/Abp/WebhooksManagement/WebhookSubscription.cs @@ -23,7 +23,7 @@ public class WebhookSubscription : CreationAuditedEntity string headers, Guid? tenantId = null) : base(id) { - Secret = Check.NotNullOrWhiteSpace(secret, nameof(secret), WebhookSubscriptionConsts.MaxSecretLength); + SetSecret(secret); SetWebhookUri(webhookUri); SetWebhooks(webhooks); SetHeaders(headers); @@ -32,6 +32,11 @@ public class WebhookSubscription : CreationAuditedEntity IsActive = true; } + public void SetSecret(string secret) + { + Secret = Check.NotNullOrWhiteSpace(secret, nameof(secret), WebhookSubscriptionConsts.MaxSecretLength); + } + public void SetWebhookUri(string webhookUri) { WebhookUri = Check.NotNullOrWhiteSpace(webhookUri, nameof(webhookUri), WebhookSubscriptionConsts.MaxWebhookUriLength); diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.HttpApi/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionController.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.HttpApi/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionController.cs index 5b7d91e6c..8a94ce6fb 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.HttpApi/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionController.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.WebhooksManagement.HttpApi/LINGYUN/Abp/WebhooksManagement/WebhookSubscriptionController.cs @@ -56,4 +56,11 @@ public class WebhookSubscriptionController : WebhooksManagementControllerBase, I { return SubscriptionAppService.UpdateAsync(id, input); } + + [HttpGet] + [Route("availables")] + public Task> GetAllAvailableWebhooksAsync() + { + return SubscriptionAppService.GetAllAvailableWebhooksAsync(); + } }