diff --git a/Base/XIBaseV1Controller.cs b/Base/XIBaseV1Controller.cs deleted file mode 100644 index cf02bd4..0000000 --- a/Base/XIBaseV1Controller.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; -using xCommons.Attributes; -using xCommons.Configurations; -using xCommons.Providers; -using xIdentityService.Controllers; -using xIdentityService.Interfaces; - -namespace xAiApi.Base -{ - [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/Base/XIBaseV1EntityController.cs b/Base/XIBaseV1EntityController.cs deleted file mode 100644 index d53bec7..0000000 --- a/Base/XIBaseV1EntityController.cs +++ /dev/null @@ -1,120 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.SignalR; -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 xAiApi.Base -{ - [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 - ) : base( - logger, - appConfiguration, - identityProvider, - validationProvider, - repository - ) - { } - #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 - ) : base( - logger, - appConfiguration, - identityProvider, - validationProvider, - repository - ) - { - Hub = hub; - } - #endregion - - // - #region Hub ... - [NonAction] - public async Task SendPush( - string action, - string payLoad - ) - { - // - 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); - } - #endregion - } -} \ No newline at end of file diff --git a/Controllers/V1/Entities/TestsController.cs b/Controllers/V1/Entities/TestsController.cs index 1aad41f..dd4e350 100644 --- a/Controllers/V1/Entities/TestsController.cs +++ b/Controllers/V1/Entities/TestsController.cs @@ -1,8 +1,12 @@ +using System; using System.Collections.Generic; +using System.Linq; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore.Query; using Microsoft.Extensions.Logging; using xAiApi.Base; using xAiApi.Data.Interfaces; @@ -32,102 +36,53 @@ namespace xAiApi.Controllers.V1.Entities IXIdentityProvider identityProvider, XValidationProvider validationProvider, IXTestRepository repository, - IHubContext> hub = null + IHubContext> hub = null, + Func, IOrderedQueryable> defaultOrderBuilder = null, + Func, IIncludableQueryable> defaultIncludeBuilder = null ) : base( - logger, - appConfiguration, - identityProvider, - validationProvider, - repository, - hub + hub: hub, + logger: logger, + repository: repository, + appConfiguration: appConfiguration, + identityProvider: identityProvider, + validationProvider: validationProvider, + defaultOrderBuilder: defaultOrderBuilder, + defaultIncludeBuilder: defaultIncludeBuilder ) { } #endregion // - #region Interface Implementations ... - // - #region Retrieve ... - [HttpGet("{id}")] - [Authorize(Policy = XPolicies.EnabledUser)] - public override async Task> Get( - [FromRoute] int id, [FromQuery] bool ignoreSoftDeleteds = true, [FromQuery] bool containsDetail = false - ) - { - return await base - .Get( - id: id, - containsDetail: containsDetail, - ignoreSoftDeleteds: ignoreSoftDeleteds - ); - } - - [HttpGet] - [Authorize(Policy = XPolicies.EnabledUser)] - public override async Task>> GetAll( - [FromQuery] bool ignoreSoftDeleteds = true, [FromQuery] bool containsDetail = false - ) - { - return await base - .GetAll( - containsDetail: containsDetail, - ignoreSoftDeleteds: ignoreSoftDeleteds - ); - } - - [HttpGet("FindOne/{query}")] - [Authorize(Policy = XPolicies.EnabledUser)] - public override async Task> FindOne( - [FromRoute] string query, [FromQuery] bool ignoreSoftDeleteds = true, [FromQuery] bool containsDetail = false - ) - { - return await base - .FindOne( - query: query, - containsDetail: containsDetail, - ignoreSoftDeleteds: ignoreSoftDeleteds - ); - } - - [HttpGet("FindMany/{query}")] - [Authorize(Policy = XPolicies.EnabledUser)] - public override async Task>> FindMany( - [FromRoute] string query, [FromQuery] bool ignoreSoftDeleteds = true, [FromQuery] bool containsDetail = false - ) - { - return await base - .FindMany( - query: query, - containsDetail: containsDetail, - ignoreSoftDeleteds: ignoreSoftDeleteds - ); - } - - [HttpGet("Query")] - [Authorize(Policy = XPolicies.EnabledUser)] - public override async Task>> Query( - [FromQuery] XQuery query, [FromQuery] bool ignoreSoftDeleteds = true - ) - { - return await base - .Query( - query: query, - ignoreSoftDeleteds: ignoreSoftDeleteds - ); - } - #endregion - + #region Actions ... // #region Add ... + /// + /// Add Specified Item ... + /// + /// + /// Sample request: + /// + /// { + /// "title": "" + /// } + /// + /// + /// + /// + /// [HttpPost] [Authorize(Policy = XPolicies.EnabledAdminOrAgent)] public override async Task> Add( - [FromBody] XTest item + [FromBody] XTest item, + CancellationToken cancellationToken = default ) { // var entity = await base - .Add(item); + .Add( + item: item, + cancellationToken: cancellationToken + ); // // Handle Sending Push Notification ... @@ -135,8 +90,9 @@ namespace xAiApi.Controllers.V1.Entities { // await SendPush( - action: XBaseEntityHubAction.Add.GetStringValue(), - payLoad: entity.ToJSON() + payLoad: entity.ToJSON(), + cancellationToken: cancellationToken, + action: XBaseEntityHubAction.Add.GetStringValue() ); } @@ -144,15 +100,33 @@ namespace xAiApi.Controllers.V1.Entities return entity; } + /// + /// Update Specified Item ... + /// + /// + /// Sample request: + /// + /// { + /// "title": "" + /// } + /// + /// + /// + /// + /// [HttpPost("AddOrUpdate")] [Authorize(Policy = XPolicies.EnabledAdminOrAgent)] public override async Task> AddOrUpdate( - [FromBody] XTest item + [FromBody] XTest item, + CancellationToken cancellationToken = default ) { // var entity = await base - .AddOrUpdate(item); + .AddOrUpdate( + item: item, + cancellationToken: cancellationToken + ); // // Handle Sending Push Notification ... @@ -160,8 +134,9 @@ namespace xAiApi.Controllers.V1.Entities { // await SendPush( - action: XBaseEntityHubAction.AddOrUpdate.GetStringValue(), - payLoad: entity.ToJSON() + payLoad: entity.ToJSON(), + cancellationToken: cancellationToken, + action: XBaseEntityHubAction.AddOrUpdate.GetStringValue() ); } @@ -169,23 +144,46 @@ namespace xAiApi.Controllers.V1.Entities return entity; } + /// + /// Add Many Specified Items ... + /// + /// + /// Sample request: + /// + /// { + /// "items": [ + /// { + /// "title": "" + /// } + /// ] + /// } + /// + /// + /// + /// + /// [HttpPost("AddMany")] [Authorize(Policy = XPolicies.EnabledAdminOrAgent)] public override async Task AddMany( - [FromBody] XBaseRangeRequest request + [FromBody] XBaseRangeRequest request, + CancellationToken cancellationToken = default ) { // var result = await base - .AddMany(request); + .AddMany( + request: request, + cancellationToken: cancellationToken + ); // if (!(result as OkObjectResult).IsNull()) { // await SendPush( - action: XBaseEntityHubAction.AddMany.GetStringValue(), - payLoad: request.Items.ToJSON() + payLoad: request.Items.ToJSON(), + cancellationToken: cancellationToken, + action: XBaseEntityHubAction.AddMany.GetStringValue() ); } @@ -194,19 +192,181 @@ namespace xAiApi.Controllers.V1.Entities } #endregion + // + #region Retrieve ... + /// + /// Get Specified Item by Providing Identifier ... + /// + /// + /// + /// + /// + /// + [HttpGet("{id}")] + [Authorize(Policy = XPolicies.EnabledUser)] + public override async Task> Get( + [FromRoute] int id, + [FromQuery] bool ignoreSoftDeleteds = true, + [FromQuery] bool useDefaultIncludes = false, + CancellationToken cancellationToken = default + ) + { + // + return await base + .Get( + id: id, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds, + useDefaultIncludes: useDefaultIncludes + ); + } + + /// + /// Get all Exists Items ... + /// + /// + /// + /// + /// + /// + [HttpGet] + [Authorize(Policy = XPolicies.EnabledUser)] + public override async Task>> GetAll( + [FromQuery] bool ignoreSoftDeleteds = true, + [FromQuery] bool useDefaultOrders = false, + [FromQuery] bool useDefaultIncludes = false, + CancellationToken cancellationToken = default + ) + { + // + return await base + .GetAll( + useDefaultOrders: useDefaultOrders, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds, + useDefaultIncludes: useDefaultIncludes + ); + } + + /// + /// Find Item based on Provided Query .... + /// + /// + /// + /// + /// + /// + [HttpGet("FindOne/{query}")] + [Authorize(Policy = XPolicies.EnabledUser)] + public override async Task> FindOne( + [FromRoute] string query, + [FromQuery] bool ignoreSoftDeleteds = true, + [FromQuery] bool useDefaultIncludes = false, + CancellationToken cancellationToken = default + ) + { + // + return await base + .FindOne( + query: query, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds, + useDefaultIncludes: useDefaultIncludes + ); + } + + /// + /// Find Items based on Provided Query ... + /// + /// + /// + /// + /// + /// + /// + [HttpGet("FindMany/{query}")] + [Authorize(Policy = XPolicies.EnabledUser)] + public override async Task>> FindMany( + [FromRoute] string query, + [FromQuery] bool ignoreSoftDeleteds = true, + [FromQuery] bool useDefaultOrders = false, + [FromQuery] bool useDefaultIncludes = false, + CancellationToken cancellationToken = default + ) + { + // + return await base + .FindMany( + query: query, + useDefaultOrders: useDefaultOrders, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds, + useDefaultIncludes: useDefaultIncludes + ); + } + + /// + /// Retrieve Items based on Query Mechanism ... + /// + /// + /// + /// + /// + /// + /// + [HttpGet("Query")] + [Authorize(Policy = XPolicies.EnabledUser)] + public override async Task>> Query( + [FromQuery] XQuery query, + [FromQuery] bool ignoreSoftDeleteds = true, + [FromQuery] bool useDefaultOrders = false, + [FromQuery] bool useDefaultIncludes = false, + CancellationToken cancellationToken = default + ) + { + // + return await base + .Query( + query: query, + useDefaultOrders: useDefaultOrders, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds, + useDefaultIncludes: useDefaultIncludes + ); + } + #endregion + // #region Update ... + /// + /// Update an Item ... + /// + /// + /// Sample request: + /// + /// { + /// "title": "" + /// } + /// + /// + /// + /// + /// + /// [HttpPut("{id}")] [Authorize(Policy = XPolicies.EnabledAdminOrAgent)] public override async Task> Update( - [FromRoute] int id, [FromBody] XTest item + [FromRoute] int id, + [FromBody] XTest item, + CancellationToken cancellationToken = default ) { // var entity = await base .Update( - id, - item + id: id, + item: item, + cancellationToken: cancellationToken ); // @@ -214,8 +374,9 @@ namespace xAiApi.Controllers.V1.Entities { // await SendPush( - action: XBaseEntityHubAction.Update.GetStringValue(), - payLoad: entity.ToJSON() + payLoad: entity.ToJSON(), + cancellationToken: cancellationToken, + action: XBaseEntityHubAction.Update.GetStringValue() ); } @@ -223,15 +384,38 @@ namespace xAiApi.Controllers.V1.Entities return entity; } + /// + /// Update a Collection of Items ... + /// + /// + /// Sample request: + /// + /// { + /// "items": [ + /// { + /// "id": 1, + /// "title": "Hi there ..." + /// } + /// ] + /// } + /// + /// + /// + /// + /// [HttpPost("UpdateMany")] [Authorize(Policy = XPolicies.EnabledAdminOrAgent)] public override async Task> UpdateMany( - [FromBody] XBaseRangeRequest request + [FromBody] XBaseRangeRequest request, + CancellationToken cancellationToken = default ) { // var result = await base - .UpdateMany(request); + .UpdateMany( + request: request, + cancellationToken: cancellationToken + ); // var resultObject = (result.Result as OkObjectResult).Value; @@ -239,8 +423,9 @@ namespace xAiApi.Controllers.V1.Entities { // await SendPush( - action: XBaseEntityHubAction.UpdateMany.GetStringValue(), - payLoad: request.Items.ToJSON() + payLoad: request.Items.ToJSON(), + cancellationToken: cancellationToken, + action: XBaseEntityHubAction.UpdateMany.GetStringValue() ); } @@ -251,15 +436,26 @@ namespace xAiApi.Controllers.V1.Entities // #region Exists ... + /// + /// Check an Item Exists or not ... + /// + /// + /// + /// + /// [HttpGet("{id}/IsExists")] [Authorize(Policy = XPolicies.EnabledUser)] public override async Task> IsExists( - [FromRoute] int id, [FromQuery] bool ignoreSoftDeleteds = true + [FromRoute] int id, + [FromQuery] bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default ) { + // return await base .IsExists( id: id, + cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); } @@ -267,17 +463,27 @@ namespace xAiApi.Controllers.V1.Entities // #region Remove ... + /// + /// Remove an Item ... + /// + /// + /// + /// + /// [HttpDelete("{id}")] [Authorize(Policy = XPolicies.EnabledAdminOrAgent)] public override async Task> Remove( - [FromRoute] int id, bool softDelete = true + [FromRoute] int id, + [FromQuery] bool softDelete = true, + CancellationToken cancellationToken = default ) { // var entity = await base .Remove( id: id, - softDelete: softDelete + softDelete: softDelete, + cancellationToken: cancellationToken ); // @@ -285,8 +491,9 @@ namespace xAiApi.Controllers.V1.Entities { // await SendPush( - action: XBaseEntityHubAction.Delete.GetStringValue(), - payLoad: entity.ToJSON() + payLoad: entity.ToJSON(), + cancellationToken: cancellationToken, + action: XBaseEntityHubAction.Delete.GetStringValue() ); } @@ -294,17 +501,40 @@ namespace xAiApi.Controllers.V1.Entities return entity; } + /// + /// Remove Many Items ... + /// + /// + /// Sample request: + /// + /// { + /// "items": [ + /// { + /// "id": 1, + /// "title": "Hi there ..." + /// } + /// ] + /// } + /// + /// + /// + /// + /// + /// [HttpPost("RemoveMany")] [Authorize(Policy = XPolicies.EnabledAdminOrAgent)] public override async Task RemoveMany( - [FromBody] XBaseRangeRequest request, bool softDelete = true + [FromBody] XBaseRangeRequest request, + [FromQuery] bool softDelete = true, + CancellationToken cancellationToken = default ) { // var result = await base .RemoveMany( request: request, - softDelete: softDelete + softDelete: softDelete, + cancellationToken: cancellationToken ); // @@ -312,8 +542,9 @@ namespace xAiApi.Controllers.V1.Entities { // await SendPush( - action: XBaseEntityHubAction.DeleteMany.GetStringValue(), - payLoad: request.Items.ToJSON() + payLoad: request.Items.ToJSON(), + cancellationToken: cancellationToken, + action: XBaseEntityHubAction.DeleteMany.GetStringValue() ); } diff --git a/xAiApi.csproj b/xAiApi.csproj index 0efa366..c164f5e 100644 --- a/xAiApi.csproj +++ b/xAiApi.csproj @@ -19,21 +19,22 @@ + + - - + -