add base requirements ...

This commit is contained in:
2026-05-27 13:55:24 +03:30
parent 0a464af693
commit ac168ed7c1
6 changed files with 874 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using xCommons.Configurations;
using xCommons.Extensions;
using xExceptions.Constants;
using xStringService.Interfaces.Dtos;
using xTermsAndConditionsService.Interfaces;
using xTermsAndConditionsService.Providers;
namespace xTermsAndConditionsService.DI
{
public static class XDIHelperExtension
{
/// <summary>
/// Register TermsConditions Provider ...
/// </summary>
/// <param name="services"></param>
/// <param name="lifeTime"></param>
public static void AddXTermsConditionsService(
this IServiceCollection services,
ServiceLifetime lifeTime = ServiceLifetime.Transient
)
{
//
AddTermsConditionsService(
services: services,
lifeTime: lifeTime
);
}
//
#region Private ...
/// <summary>
/// a LogTag for Service ...
/// </summary>
private static string XLogTag = "XTermsConditionsService";
/// <summary>
/// print a log in Console ...
/// </summary>
/// <param name="message"></param>
private static void Log(string message)
{
Console.WriteLine($"{XLogTag} => {message}");
}
/// <summary>
/// Add Terms and Conditions Provider Service ...
/// </summary>
/// <param name="services"></param>
/// <param name="lifeTime"></param>
private static void AddTermsConditionsService(
IServiceCollection services,
ServiceLifetime lifeTime = ServiceLifetime.Transient
)
{
//
var exception = XException.InvalidArgs.ToException(); ;
//
// Services ...
var isServicesExists = !services.IsNull();
if (!isServicesExists)
{
//
Log("AddTermsConditions failed, services not provided ...");
throw exception;
}
//
// Lifetime ...
var isLifetimeExists = !lifeTime.IsNull();
if (!isLifetimeExists)
{
//
Log("AddTermsConditions failed, lifetime not provided ...");
throw exception;
}
//
// Check Dependencies Exists ...
var isDependenciesPassed = !services
.GetRegisteredService<XAppConfiguration>()
.IsNull() &&
!services.GetRegisteredService<IXStringRepositoryService>()
.IsNull();
if (!isDependenciesPassed)
{
//
Log("AddTermsConditions failed due Dependencies Issues, IXStringRepository or XAppConfiguration not provided ...");
throw exception;
}
//
// Register Resource Providers ...
services.Add(new ServiceDescriptor(typeof(IXTermsAndComditionsProvider), typeof(XTermsAndComditionsProvider), lifeTime));
}
#endregion
}
}
View File
+107
View File
@@ -0,0 +1,107 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using xCommons.Extensions;
using xPushService.Base;
using xPushService.Constants;
using xStringService.Models.Dtos;
namespace xTermsAndConditionsService.Hubs
{
/// <summary>
/// a Hub for Notifing Terms and Conditions Events to Connected Clients ...
/// </summary>
public class XTermsHub : XBaseHub
{
public XTermsHub(
ILogger<XTermsHub> logger
) : base(logger)
{
}
//
#region Hub Actions ...
/// <summary>
/// Add Specified Terms and Conditions ...
/// </summary>
/// <param name="entity"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task Add(
XStringDto entity,
CancellationToken cancellationToken = default
)
{
//
var connectionId = Context.ConnectionId;
if (!connectionId.IsNullOrEmpty())
{
//
await Clients
.AllExcept(connectionId)
.SendAsync(
XBaseEntityHubAction.Add.GetStringValue(),
entity.ToJSON(camelCase: true),
connectionId,
cancellationToken
);
}
}
/// <summary>
/// Deleted Specified Terms and Conditions ...
/// </summary>
/// <param name="entity"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task Delete(
XStringDto entity,
CancellationToken cancellationToken = default
)
{
//
var connectionId = Context.ConnectionId;
if (!connectionId.IsNullOrEmpty())
{
//
await Clients
.AllExcept(connectionId)
.SendAsync(
XBaseEntityHubAction.Delete.GetStringValue(),
entity.ToJSON(camelCase: true),
connectionId,
cancellationToken
);
}
}
/// <summary>
/// Notify an Entity Updated ...
/// </summary>
/// <param name="entity"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task Update(
XStringDto entity,
CancellationToken cancellationToken = default
)
{
//
var connectionId = Context.ConnectionId;
if (!connectionId.IsNullOrEmpty())
{
//
await Clients
.AllExcept(connectionId)
.SendAsync(
XBaseEntityHubAction.Update.GetStringValue(),
entity.ToJSON(camelCase: true),
connectionId,
cancellationToken
);
}
}
#endregion
}
}
@@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using xModels.Base;
namespace xTermsAndConditionsService.Interfaces
{
/// <summary>
/// an Interface for Describing Terms and Conditions Management Controller Actions ...
/// </summary>
public interface IXTermsAndComditionsController
{
/// <summary>
/// Retrieved all Exists Languages Terms and Conditions ...
/// </summary>
/// <returns></returns>
[HttpGet("Terms/Languages")]
Task<ActionResult<IEnumerable<string>>> TermsLanguages(
CancellationToken cancellationToken = default
);
/// <summary>
/// Check Has Terms and Conditions based on Specified Language ...
/// </summary>
/// <param name="language">if null, used Default Language ...</param>
/// <returns></returns>
[HttpGet("Terms/{language}/Has")]
Task<ActionResult<bool>> HasTerms(
[FromRoute] string language = null,
CancellationToken cancellationToken = default
);
/// <summary>
/// Retrieve Terms and Conditions for Specified Language ...
/// </summary>
/// <param name="language">if null, used Default Language ...</param>
/// <returns></returns>
[HttpGet("Terms/{language}")]
Task<ActionResult<string>> GetTerms(
[FromRoute] string language = null,
CancellationToken cancellationToken = default
);
/// <summary>
/// Add Terms and Conditions for Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="terms"></param>
/// <returns></returns>
[HttpPost("Terms/{language}")]
Task<ActionResult<string>> AddTerms(
[FromRoute] string language,
[FromBody] XBaseValueContainer<string> terms,
CancellationToken cancellationToken = default
);
/// <summary>
/// Remove Terms and Conditions of Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <returns></returns>
[HttpDelete("Terms/{language}")]
Task<ActionResult<bool>> RemoveTerms(
[FromRoute] string language,
CancellationToken cancellationToken = default
);
/// <summary>
/// Update Terms and Conditions of Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="terms"></param>
/// <returns></returns>
[HttpPut("Terms/{language}")]
Task<ActionResult<bool>> UpdateTerms(
[FromRoute] string language,
[FromBody] XBaseValueContainer<string> terms,
CancellationToken cancellationToken = default
);
}
}
@@ -0,0 +1,95 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using xIdentityModels.Models;
namespace xTermsAndConditionsService.Interfaces
{
/// <summary>
/// an Interface for Describing Terms and Conditions Management Provided Actions ...
/// </summary>
public interface IXTermsAndComditionsProvider
{
#region Actions ...
/// <summary>
/// Retrieved all Exists Languages Terms and Conditions ...
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IEnumerable<string>> TermsLanguages(
CancellationToken cancellationToken = default
);
/// <summary>
/// Check Has Terms and Conditions based on Specified Language ...
/// </summary>
/// <param name="language">if null, used Default Language ...</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<bool> HasTerms(
string language = null,
CancellationToken cancellationToken = default
);
/// <summary>
/// Retrieve Terms and Conditions for Specified Language ...
/// </summary>
/// <param name="language">if null, used Default Language ...</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<string> GetTerms(
string language = null,
CancellationToken cancellationToken = default
);
/// <summary>
/// Add Terms and Conditions for Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="terms"></param>
/// <param name="userInfo"></param>
/// <param name="connectionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<string> AddTerms(
string language,
string terms,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
);
/// <summary>
/// Remove Terms and Conditions of Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="userInfo"></param>
/// <param name="connectionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<bool> RemoveTerms(
string language,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
);
/// <summary>
/// Update Terms and Conditions of Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="terms"></param>
/// <param name="userInfo"></param>
/// <param name="connectionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<bool> UpdateTerms(
string language,
string terms,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
);
#endregion
}
}
+490
View File
@@ -0,0 +1,490 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using xCommons.Configurations;
using xCommons.Extensions;
using xExceptions.Constants;
using xIdentityModels.Extensions;
using xIdentityModels.Models;
using xPushService.Constants;
using xStringService.Interfaces.Dtos;
using xStringService.Models.Dtos;
using xTermsAndConditionsService.Hubs;
using xTermsAndConditionsService.Interfaces;
namespace xTermsAndConditionsService.Providers
{
/// <summary>
/// an Implementation of Terms and Conditions Management Provided Actions ...
/// </summary>
public class XTermsAndComditionsProvider : IXTermsAndComditionsProvider
{
//
#region Props ...
private readonly string termsId;
private readonly string[] allowedRoles;
private readonly IHubContext<XTermsHub> hub;
private readonly XAppConfiguration appConfiguration;
private readonly IXStringServiceProvider stringProvider;
#endregion
//
#region Constructor ...
public XTermsAndComditionsProvider(
XAppConfiguration appConfiguration,
IXStringServiceProvider stringProvider,
IHubContext<XTermsHub> hub = null,
string termsId = "terms"
)
{
//
this.hub = hub;
this.termsId = termsId;
this.stringProvider = stringProvider;
this.appConfiguration = appConfiguration;
//
// Specified roles for Handling Dangerouse Actions ...
this.allowedRoles = new string[]
{
"admin"
};
}
#endregion
//
#region Actions ...
/// <summary>
/// Retrieved all Exists Languages Terms and Conditions ...
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IEnumerable<string>> TermsLanguages(
CancellationToken cancellationToken = default
)
{
//
var result = (await stringProvider
.FindManyAsync(
includeBuilder: null,
ignoreSoftDeleteds: true,
cancellationToken: cancellationToken,
predicate: x => x.ResourceTitle == termsId,
orderBuilder: x => x.OrderBy(y => y.ResourceTitle)
))
.Select(x => x.Language);
return result;
}
/// <summary>
/// Check Has Terms and Conditions based on Specified Language ...
/// </summary>
/// <param name="language">if null, used Default Language ...</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<bool> HasTerms(
string language = null,
CancellationToken cancellationToken = default
)
{
//
// Normalize ...
if (language.IsNullOrEmpty())
{
language = appConfiguration.DefaultLanguage;
}
//
var result = !(await GetString(
language: language,
cancellationToken: cancellationToken
))
.IsNullOrDefault();
//
return result;
}
/// <summary>
/// Retrieve Terms and Conditions for Specified Language ...
/// </summary>
/// <param name="language">if null, used Default Language ...</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<string> GetTerms(
string language = null,
CancellationToken cancellationToken = default
)
{
//
var result = string.Empty;
var item = await GetString(
language: language,
cancellationToken: cancellationToken
);
//
if (!item.IsNullOrDefault())
{
result = item.TranslatedValue;
}
//
return result;
}
/// <summary>
/// Add Terms and Conditions for Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="terms"></param>
/// <param name="userInfo"></param>
/// <param name="connectionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<string> AddTerms(
string language,
string terms,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
var isValid =
!language.IsNullOrEmpty() &&
!terms.IsNullOrEmpty() &&
!userInfo.IsNullOrDefault() &&
language != appConfiguration.DefaultLanguage;
if (!isValid)
{
XException.InvalidArgs.Throw();
}
//
// Check Exists ...
isValid = await HasTerms(
language: language,
cancellationToken: cancellationToken
);
if (isValid)
{
XException.Duplicate.Throw();
}
//
// Check Permission ...
isValid = userInfo.HasAccess(allowedRoles);
if (!isValid)
{
XException.NotAllowed.Throw();
}
//
// Add Terms ...
XStringDto item = null;
try
{
//
item = new XStringDto
{
Language = language,
ResourceTitle = termsId,
TranslatedValue = terms,
};
item = await stringProvider.AddAsync(
item: item,
saveChanges: true,
cancellationToken: cancellationToken
);
}
catch { }
//
var result = string.Empty;
if (!item.IsNullOrDefault())
{
result = item.TranslatedValue;
}
//
if (!result.IsNullOrEmpty())
{
//
await SendPush(
connectionId: connectionId,
cancellationToken: cancellationToken,
payLoad: item.ToJSON(camelCase: true),
action: XBaseEntityHubAction.Add.GetStringValue()
);
}
//
return result;
}
/// <summary>
/// Remove Terms and Conditions of Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="userInfo"></param>
/// <param name="userInfo"></param>
/// <param name="connectionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<bool> RemoveTerms(
string language,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
)
{
//
var isValid =
!language.IsNullOrEmpty() &&
!userInfo.IsNullOrDefault() &&
language != appConfiguration.DefaultLanguage;
if (!isValid)
{
XException.InvalidArgs.Throw();
}
//
// Check Exists ...
var item = await GetString(
language: language,
cancellationToken: cancellationToken
);
isValid = !item.IsNullOrDefault();
if (!isValid)
{
XException.NotFound.Throw();
}
//
// Check Permission ...
isValid = userInfo.HasAccess(allowedRoles);
if (!isValid)
{
XException.NotAllowed.Throw();
}
//
try
{
//
item = await stringProvider.RemoveAsync(
id: item.Id,
softDelete: false,
saveChanges: true,
cancellationToken: cancellationToken
);
}
catch
{
item = null;
}
//
var result = !item.IsNullOrDefault();
if (result)
{
//
await SendPush(
connectionId: connectionId,
cancellationToken: cancellationToken,
payLoad: item.ToJSON(camelCase: true),
action: XBaseEntityHubAction.Delete.GetStringValue()
);
}
//
return result;
}
/// <summary>
/// Update Terms and Conditions of Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="terms"></param>
/// <param name="userInfo"></param>
/// <param name="connectionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<bool> UpdateTerms(
string language,
string terms,
XUserClaimsInfoDto userInfo = null,
string connectionId = null,
CancellationToken cancellationToken = default
)
{
//
var isValid =
!terms.IsNullOrEmpty() &&
!language.IsNullOrEmpty() &&
!userInfo.IsNullOrDefault();
if (!isValid)
{
XException.InvalidArgs.Throw();
}
//
// Check Exists ...
var item = await GetString(
language: language,
cancellationToken: cancellationToken
);
isValid = !item.IsNullOrDefault();
if (!isValid)
{
XException.NotFound.Throw();
}
//
// Check Permission ...
isValid = userInfo.HasAccess(allowedRoles);
if (!isValid)
{
XException.NotAllowed.Throw();
}
//
try
{
//
item.TranslatedValue = terms;
item = await stringProvider.UpdateAsync(
id: item.Id,
item: item,
saveChanges: true,
cancellationToken: cancellationToken
);
}
catch
{
item = null;
}
//
var result = !item.IsNullOrDefault();
if (result)
{
//
await SendPush(
connectionId: connectionId,
cancellationToken: cancellationToken,
payLoad: item.ToJSON(camelCase: true),
action: XBaseEntityHubAction.Update.GetStringValue()
);
}
//
return result;
}
#endregion
//
#region Hub Actions ...
/// <summary>
/// Send Custom Push Message ...
/// </summary>
/// <param name="action"></param>
/// <param name="payLoad"></param>
/// <param name="connectionId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task SendPush(
string action,
string payLoad,
string connectionId = null,
CancellationToken cancellationToken = default
)
{
//
var actions = new List<string>
{
XBaseEntityHubAction.Add.GetStringValue(),
XBaseEntityHubAction.Update.GetStringValue(),
XBaseEntityHubAction.Delete.GetStringValue(),
};
//
// Validate ...
var isValid =
!hub.IsNull() &&
!action.IsNullOrEmpty() &&
actions.Contains(action);
if (!isValid)
{
return;
}
//
var clients = hub.Clients.All;
if (!connectionId.IsNullOrEmpty())
{
clients = hub.Clients.AllExcept(connectionId);
}
try
{
//
await clients.SendAsync(
action,
payLoad,
connectionId,
cancellationToken
);
}
catch { }
}
#endregion
//
#region Private ...
/// <summary>
/// Retrieve Terms and Conditions XString Entity ...
/// </summary>
/// <param name="language"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<XStringDto> GetString(
string language = null,
CancellationToken cancellationToken = default
)
{
//
// Normalize ...
if (language.IsNullOrEmpty())
{
language = appConfiguration.DefaultLanguage;
}
//
// Retrieve ...
XStringDto result = null;
try
{
//
result = await stringProvider.FindOneAsync(
includeBuilder: null,
ignoreSoftDeleteds: true,
predicate: x =>
x.Language == language &&
x.ResourceTitle == termsId,
cancellationToken: cancellationToken
);
}
catch
{
}
//
return result;
}
#endregion
}
}