diff --git a/Controllers/XIBaseV1Controller.cs b/Controllers/XIBaseV1Controller.cs new file mode 100644 index 0000000..54869c9 --- /dev/null +++ b/Controllers/XIBaseV1Controller.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using xCommons.Attributes; +using xCommons.Configurations; +using xCommons.Providers; +using xIdentityService.Controllers; +using xIdentityService.Interfaces; + +namespace xIdentityHelper.Controllers +{ + [ApiController] + [ApiVersion("1.0")] + [RequireXPowered(true)] + [Route("api/v{version:apiVersion}/[controller]")] + public abstract class XIBaseV1Controller : XIBaseController + { + // + #region Constructor ... + protected XIBaseV1Controller( + ILogger logger, + XAppConfiguration appConfiguration, + IXIdentityProvider identityProvider, + XValidationProvider validationProvider + ) : base( + logger, + appConfiguration, + identityProvider, + validationProvider + ) + { } + #endregion + } +} \ No newline at end of file diff --git a/Controllers/XIBaseV1DomainController.cs b/Controllers/XIBaseV1DomainController.cs new file mode 100644 index 0000000..b35345a --- /dev/null +++ b/Controllers/XIBaseV1DomainController.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore.Query; +using Microsoft.Extensions.Logging; +using xCommons.Attributes; +using xCommons.Configurations; +using xCommons.Extensions; +using xCommons.Providers; +using xDataService.Interfaces; +using xIdentityService.Controllers; +using xIdentityService.Interfaces; +using xModels.Base; +using xPushService.Base; +using xPushService.Constants; + +namespace xIdentityHelper.Controllers +{ + [ApiController] + [ApiVersion("1.0")] + [RequireXPowered(true)] + [Route("api/v{version:apiVersion}/domains/[controller]")] + public abstract class XIBaseV1DomainController : XIBaseDomainController + where TEntity : XBaseEntity + where TDto : XBaseEntityDto + { + // + #region Constructor ... + protected XIBaseV1DomainController( + ILogger> logger, + XAppConfiguration appConfiguration, + IXIdentityProvider identityProvider, + XValidationProvider validationProvider, + IXBaseRepositoryService repositoryService, + Func, IOrderedQueryable> defaultOrderBuilder = null, + Func, IIncludableQueryable> defaultIncludeBuilder = null + ) : base( + logger: logger, + appConfiguration: appConfiguration, + identityProvider: identityProvider, + repositoryService: repositoryService, + validationProvider: validationProvider, + defaultOrderBuilder: defaultOrderBuilder, + defaultIncludeBuilder: defaultIncludeBuilder + ) + { } + #endregion + } + + public abstract class XIBaseV1DomainHubController : XIBaseV1EntityController + where TEntity : XBaseEntity + { + // + #region Props ... + public IHubContext> Hub { get; } + #endregion + + // + #region Constructor ... + protected XIBaseV1EntityHubController( + ILogger> logger, + XAppConfiguration appConfiguration, + IXIdentityProvider identityProvider, + XValidationProvider validationProvider, + IXBaseRepository repository, + IHubContext> hub = null, + Func, IOrderedQueryable> defaultOrderBuilder = null, + Func, IIncludableQueryable> defaultIncludeBuilder = null + ) : base( + logger: logger, + repository: repository, + appConfiguration: appConfiguration, + identityProvider: identityProvider, + validationProvider: validationProvider, + defaultOrderBuilder: defaultOrderBuilder, + defaultIncludeBuilder: defaultIncludeBuilder + ) + { + Hub = hub; + } + #endregion + + // + #region Hub ... + [NonAction] + public async Task SendPush( + string action, + string payLoad, + CancellationToken cancellationToken = default + ) + { + // + var actions = new List + { + XBaseEntityHubAction.Add.GetStringValue(), + XBaseEntityHubAction.Update.GetStringValue(), + XBaseEntityHubAction.Delete.GetStringValue(), + XBaseEntityHubAction.AddMany.GetStringValue(), + XBaseEntityHubAction.DeleteMany.GetStringValue(), + XBaseEntityHubAction.UpdateMany.GetStringValue(), + XBaseEntityHubAction.AddOrUpdate.GetStringValue(), + }; + + // + // Validate ... + var isValid = + !Hub.IsNull() && + !action.IsNullOrEmpty() && + !payLoad.IsNullOrEmpty() && + actions.Contains(action); + if (!isValid) + { + return; + } + + // + // Retrieve Connection Id ... + var connectionId = GetConnectionId(); + var clients = Hub.Clients.All; + if (!connectionId.IsNullOrEmpty()) + { + clients = Hub.Clients.AllExcept(connectionId); + } + + // + await clients.SendAsync( + action, + payLoad, + connectionId, + cancellationToken + ); + } + #endregion + } +} \ No newline at end of file diff --git a/Controllers/XIBaseV1EntityController.cs b/Controllers/XIBaseV1EntityController.cs new file mode 100644 index 0000000..ef5c054 --- /dev/null +++ b/Controllers/XIBaseV1EntityController.cs @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore.Query; +using Microsoft.Extensions.Logging; +using xCommons.Attributes; +using xCommons.Configurations; +using xCommons.Extensions; +using xCommons.Providers; +using xDataService.Interfaces; +using xIdentityService.Controllers; +using xIdentityService.Interfaces; +using xModels.Base; +using xPushService.Base; +using xPushService.Constants; + +namespace xIdentityHelper.Controllers +{ + [ApiController] + [ApiVersion("1.0")] + [RequireXPowered(true)] + [Route("api/v{version:apiVersion}/entities/[controller]")] + public abstract class XIBaseV1EntityController : XIBaseEntityController + where TEntity : XBaseEntity + { + // + #region Constructor ... + protected XIBaseV1EntityController( + ILogger> logger, + XAppConfiguration appConfiguration, + IXIdentityProvider identityProvider, + XValidationProvider validationProvider, + IXBaseRepository repository, + Func, IOrderedQueryable> defaultOrderBuilder = null, + Func, IIncludableQueryable> defaultIncludeBuilder = null + ) : base( + logger: logger, + repository: repository, + appConfiguration: appConfiguration, + identityProvider: identityProvider, + validationProvider: validationProvider, + defaultOrderBuilder: defaultOrderBuilder, + defaultIncludeBuilder: defaultIncludeBuilder + ) + { } + #endregion + } + + public abstract class XIBaseV1EntityHubController : XIBaseV1EntityController + where TEntity : XBaseEntity + { + // + #region Props ... + public IHubContext> Hub { get; } + #endregion + + // + #region Constructor ... + protected XIBaseV1EntityHubController( + ILogger> logger, + XAppConfiguration appConfiguration, + IXIdentityProvider identityProvider, + XValidationProvider validationProvider, + IXBaseRepository repository, + IHubContext> hub = null, + Func, IOrderedQueryable> defaultOrderBuilder = null, + Func, IIncludableQueryable> defaultIncludeBuilder = null + ) : base( + logger: logger, + repository: repository, + appConfiguration: appConfiguration, + identityProvider: identityProvider, + validationProvider: validationProvider, + defaultOrderBuilder: defaultOrderBuilder, + defaultIncludeBuilder: defaultIncludeBuilder + ) + { + Hub = hub; + } + #endregion + + // + #region Hub ... + [NonAction] + public async Task SendPush( + string action, + string payLoad, + CancellationToken cancellationToken = default + ) + { + // + var actions = new List + { + XBaseEntityHubAction.Add.GetStringValue(), + XBaseEntityHubAction.Update.GetStringValue(), + XBaseEntityHubAction.Delete.GetStringValue(), + XBaseEntityHubAction.AddMany.GetStringValue(), + XBaseEntityHubAction.DeleteMany.GetStringValue(), + XBaseEntityHubAction.UpdateMany.GetStringValue(), + XBaseEntityHubAction.AddOrUpdate.GetStringValue(), + }; + + // + // Validate ... + var isValid = + !Hub.IsNull() && + !action.IsNullOrEmpty() && + !payLoad.IsNullOrEmpty() && + actions.Contains(action); + if (!isValid) + { + return; + } + + // + // Retrieve Connection Id ... + var connectionId = GetConnectionId(); + var clients = Hub.Clients.All; + if (!connectionId.IsNullOrEmpty()) + { + clients = Hub.Clients.AllExcept(connectionId); + } + + // + await clients.SendAsync( + action, + payLoad, + connectionId, + cancellationToken + ); + } + #endregion + } +} \ No newline at end of file diff --git a/xIdentityHelper.csproj b/xIdentityHelper.csproj index e6ebd84..df2bb1d 100644 --- a/xIdentityHelper.csproj +++ b/xIdentityHelper.csproj @@ -23,6 +23,7 @@ + \ No newline at end of file