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;
using xAiApi.Data.Models.Entities;
using xCommons.Configurations;
using xCommons.Extensions;
using xCommons.Providers;
using xIdentityHelper;
using xIdentityService.Interfaces;
using xModels.Base;
using xModels.Dtos;
using xPushService.Base;
using xPushService.Constants;
namespace xAiApi.Controllers.V1.Entities
{
///
/// Simple XTest Entity Controller ...
///
public class TestsController : XIBaseV1EntityHubController
{
//
#region Constructor ...
public TestsController(
ILogger logger,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
XValidationProvider validationProvider,
IXTestRepository repository,
IHubContext> hub = null,
Func, IOrderedQueryable> defaultOrderBuilder = null,
Func, IIncludableQueryable> defaultIncludeBuilder = null
) : base(
hub: hub,
logger: logger,
repository: repository,
appConfiguration: appConfiguration,
identityProvider: identityProvider,
validationProvider: validationProvider,
defaultOrderBuilder: defaultOrderBuilder,
defaultIncludeBuilder: defaultIncludeBuilder
)
{ }
#endregion
//
#region Actions ...
//
#region Add ...
///
/// Add Specified Item ...
///
///
/// Sample request:
///
/// {
/// "title": ""
/// }
///
///
///
///
///
[HttpPost]
[Authorize(Policy = XPolicies.EnabledAdminOrAgent)]
public override async Task> Add(
[FromBody] XTest item,
CancellationToken cancellationToken = default
)
{
//
var entity = await base
.Add(
item: item,
cancellationToken: cancellationToken
);
//
// Handle Sending Push Notification ...
if (!entity.IsNullOrDefault())
{
//
await SendPush(
payLoad: entity.ToJSON(),
cancellationToken: cancellationToken,
action: XBaseEntityHubAction.Add.GetStringValue()
);
}
//
return entity;
}
///
/// Update Specified Item ...
///
///
/// Sample request:
///
/// {
/// "title": ""
/// }
///
///
///
///
///
[HttpPost("AddOrUpdate")]
[Authorize(Policy = XPolicies.EnabledAdminOrAgent)]
public override async Task> AddOrUpdate(
[FromBody] XTest item,
CancellationToken cancellationToken = default
)
{
//
var entity = await base
.AddOrUpdate(
item: item,
cancellationToken: cancellationToken
);
//
// Handle Sending Push Notification ...
if (!entity.IsNullOrDefault())
{
//
await SendPush(
payLoad: entity.ToJSON(),
cancellationToken: cancellationToken,
action: XBaseEntityHubAction.AddOrUpdate.GetStringValue()
);
}
//
return entity;
}
///
/// Add Many Specified Items ...
///
///
/// Sample request:
///
/// {
/// "items": [
/// {
/// "title": ""
/// }
/// ]
/// }
///
///
///
///
///
[HttpPost("AddMany")]
[Authorize(Policy = XPolicies.EnabledAdminOrAgent)]
public override async Task AddMany(
[FromBody] XBaseRangeRequest request,
CancellationToken cancellationToken = default
)
{
//
var result = await base
.AddMany(
request: request,
cancellationToken: cancellationToken
);
//
if (!(result as OkObjectResult).IsNull())
{
//
await SendPush(
payLoad: request.Items.ToJSON(),
cancellationToken: cancellationToken,
action: XBaseEntityHubAction.AddMany.GetStringValue()
);
}
//
return result;
}
#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,
CancellationToken cancellationToken = default
)
{
//
var entity = await base
.Update(
id: id,
item: item,
cancellationToken: cancellationToken
);
//
if (!entity.IsNullOrDefault())
{
//
await SendPush(
payLoad: entity.ToJSON(),
cancellationToken: cancellationToken,
action: XBaseEntityHubAction.Update.GetStringValue()
);
}
//
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,
CancellationToken cancellationToken = default
)
{
//
var result = await base
.UpdateMany(
request: request,
cancellationToken: cancellationToken
);
//
var resultObject = (result.Result as OkObjectResult).Value;
if (!resultObject.IsNull() && (bool)resultObject)
{
//
await SendPush(
payLoad: request.Items.ToJSON(),
cancellationToken: cancellationToken,
action: XBaseEntityHubAction.UpdateMany.GetStringValue()
);
}
//
return result;
}
#endregion
//
#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,
CancellationToken cancellationToken = default
)
{
//
return await base
.IsExists(
id: id,
cancellationToken: cancellationToken,
ignoreSoftDeleteds: ignoreSoftDeleteds
);
}
#endregion
//
#region Remove ...
///
/// Remove an Item ...
///
///
///
///
///
[HttpDelete("{id}")]
[Authorize(Policy = XPolicies.EnabledAdminOrAgent)]
public override async Task> Remove(
[FromRoute] int id,
[FromQuery] bool softDelete = true,
CancellationToken cancellationToken = default
)
{
//
var entity = await base
.Remove(
id: id,
softDelete: softDelete,
cancellationToken: cancellationToken
);
//
if (!entity.IsNullOrDefault())
{
//
await SendPush(
payLoad: entity.ToJSON(),
cancellationToken: cancellationToken,
action: XBaseEntityHubAction.Delete.GetStringValue()
);
}
//
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,
[FromQuery] bool softDelete = true,
CancellationToken cancellationToken = default
)
{
//
var result = await base
.RemoveMany(
request: request,
softDelete: softDelete,
cancellationToken: cancellationToken
);
//
if (!(result as OkObjectResult).IsNull())
{
//
await SendPush(
payLoad: request.Items.ToJSON(),
cancellationToken: cancellationToken,
action: XBaseEntityHubAction.DeleteMany.GetStringValue()
);
}
//
return result;
}
#endregion
#endregion
}
}