Initial ...
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Providers;
|
||||
using xIdentityService.Controllers;
|
||||
using xIdentityService.Interfaces;
|
||||
using xServices.TermsConditions.Interfaces;
|
||||
|
||||
namespace xServices.TermsConditions.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// a Controller base Class which implement based Actions to Provides Terms and Conditions Provider Access ...
|
||||
/// </summary>
|
||||
public abstract class XTermsConditionsControllerActions : XIBaseProviderController, IXTermsConditionsControllerActions
|
||||
{
|
||||
//
|
||||
#region Props ...
|
||||
public IXTermsComditionsProvider TermsProvider { get; }
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Constructor ...
|
||||
public XTermsConditionsControllerActions(
|
||||
ILogger logger,
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
XValidationProvider validationProvider,
|
||||
IXTermsComditionsProvider termsProvider
|
||||
) : base(
|
||||
logger,
|
||||
appConfiguration,
|
||||
identityProvider,
|
||||
validationProvider
|
||||
)
|
||||
{
|
||||
//
|
||||
TermsProvider = termsProvider;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Actions ...
|
||||
/// <summary>
|
||||
/// Retrieved all Exists Languages Terms and Conditions ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Terms/Languages")]
|
||||
public async Task<ActionResult<IEnumerable<string>>> TermsLanguages()
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Retrieve User Info ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await TermsProvider.TermsLanguages();
|
||||
|
||||
//
|
||||
return Ok(result
|
||||
.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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")]
|
||||
public async Task<ActionResult<bool>> HasTerms(
|
||||
[FromRoute] string language = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Retrieve User Info ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await TermsProvider.HasTerms(
|
||||
language: language
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Terms and Conditions for Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language">if null, used Default Language ...</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Terms/{language}")]
|
||||
public async Task<ActionResult<string>> GetTerms(
|
||||
[FromRoute] string language = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Retrieve User Info ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await TermsProvider.GetTerms(
|
||||
language: language
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Terms and Conditions for Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="terms"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("Terms/{language}")]
|
||||
public async Task<ActionResult<string>> AddTerms(
|
||||
[FromRoute] string language,
|
||||
[FromQuery] string terms
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Retrieve User Info ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await TermsProvider.AddTerms(
|
||||
language: language,
|
||||
terms: terms,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Terms and Conditions of Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("Terms/{language}")]
|
||||
public async Task<ActionResult<bool>> RemoveTerms(
|
||||
[FromRoute] string language
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Retrieve User Info ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await TermsProvider.RemoveTerms(
|
||||
language: language,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update Terms and Conditions of Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="terms"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("Terms/{language}")]
|
||||
public async Task<ActionResult<bool>> UpdateTerms(
|
||||
[FromRoute] string language,
|
||||
[FromQuery] string terms
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Retrieve User Info ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await TermsProvider.UpdateTerms(
|
||||
language: language,
|
||||
terms: terms,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region NonActions ...
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xServices.TermsConditions.Interfaces;
|
||||
using xServices.TermsConditions.Provides;
|
||||
using xStringService.Interfaces.Entities;
|
||||
|
||||
namespace xServices.TermsConditions.DI
|
||||
{
|
||||
public static class TermsConditionsExtensions
|
||||
{
|
||||
/// <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<IXStringRepository>()
|
||||
.IsNull();
|
||||
if (!isDependenciesPassed)
|
||||
{
|
||||
//
|
||||
Log("AddTermsConditions failed due Dependencies Issues, IXStringRepository or XAppConfiguration not provided ...");
|
||||
throw exception;
|
||||
}
|
||||
|
||||
//
|
||||
// Register Resource Providers ...
|
||||
services.Add(new ServiceDescriptor(typeof(IXTermsComditionsProvider), typeof(XTermsComditionsProvider), lifeTime));
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using xIdentityModels.Models;
|
||||
|
||||
namespace xServices.TermsConditions.Interfaces
|
||||
{
|
||||
public interface IXTermsComditionsProvider
|
||||
{
|
||||
//
|
||||
#region Actions ...
|
||||
/// <summary>
|
||||
/// Retrieved all Exists Languages Terms and Conditions ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<string>> TermsLanguages();
|
||||
|
||||
/// <summary>
|
||||
/// Check Has Terms and Conditions based on Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language">if null, used Default Language ...</param>
|
||||
/// <returns></returns>
|
||||
Task<bool> HasTerms(string language = null);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Terms and Conditions for Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language">if null, used Default Language ...</param>
|
||||
/// <returns></returns>
|
||||
Task<string> GetTerms(string language = null);
|
||||
|
||||
/// <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>
|
||||
/// <returns></returns>
|
||||
Task<string> AddTerms(
|
||||
string language,
|
||||
string terms,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Remove Terms and Conditions of Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> RemoveTerms(
|
||||
string language,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null
|
||||
);
|
||||
|
||||
/// <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>
|
||||
/// <returns></returns>
|
||||
Task<bool> UpdateTerms(
|
||||
string language,
|
||||
string terms,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null
|
||||
);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace xServices.TermsConditions.Interfaces
|
||||
{
|
||||
public interface IXTermsConditionsControllerActions
|
||||
{
|
||||
/// <summary>
|
||||
/// Terms and Conditions Provider ...
|
||||
/// </summary>
|
||||
IXTermsComditionsProvider TermsProvider { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Retrieved all Exists Languages Terms and Conditions ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<IEnumerable<string>>> TermsLanguages();
|
||||
|
||||
/// <summary>
|
||||
/// Check Has Terms and Conditions based on Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language">if null, used Default Language ...</param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<bool>> HasTerms(
|
||||
[FromRoute] string language = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Terms and Conditions for Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language">if null, used Default Language ...</param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<string>> GetTerms(
|
||||
[FromRoute] string language = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Add Terms and Conditions for Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="terms"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<string>> AddTerms(
|
||||
[FromRoute] string language,
|
||||
[FromQuery] string terms
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Remove Terms and Conditions of Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<bool>> RemoveTerms(
|
||||
[FromRoute] string language
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Update Terms and Conditions of Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="terms"></param>
|
||||
/// <returns></returns>
|
||||
Task<ActionResult<bool>> UpdateTerms(
|
||||
[FromRoute] string language,
|
||||
[FromQuery] string terms
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,411 @@
|
||||
using xStringService.Interfaces.Entities;
|
||||
using xServices.TermsConditions.Interfaces;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Configurations;
|
||||
using xStringService.Models.Entities;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityModels.Models;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using xServices.TermsConditions.Push;
|
||||
using System.Collections.Generic;
|
||||
using xPushService.Constants;
|
||||
|
||||
namespace xServices.TermsConditions.Provides
|
||||
{
|
||||
public class XTermsComditionsProvider : IXTermsComditionsProvider
|
||||
{
|
||||
//
|
||||
#region Props ...
|
||||
private readonly string termsId;
|
||||
private readonly IHubContext<XTermsHub> hub;
|
||||
private readonly XAppConfiguration appConfiguration;
|
||||
private readonly IXStringRepository stringRepository;
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Constructor ...
|
||||
public XTermsComditionsProvider(
|
||||
XAppConfiguration appConfiguration,
|
||||
IXStringRepository stringRepository,
|
||||
IHubContext<XTermsHub> hub = null,
|
||||
string termsId = "terms"
|
||||
)
|
||||
{
|
||||
//
|
||||
this.termsId = termsId;
|
||||
this.stringRepository = stringRepository;
|
||||
this.hub = hub;
|
||||
this.appConfiguration = appConfiguration;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Actions ...
|
||||
/// <summary>
|
||||
/// Retrieved all Exists Languages Terms and Conditions ...
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<string>> TermsLanguages()
|
||||
{
|
||||
//
|
||||
var result = (await stringRepository
|
||||
.FindManyAsync(x => x.ResourceTitle == termsId))
|
||||
.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>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> HasTerms(string language = null)
|
||||
{
|
||||
//
|
||||
// Normalize ...
|
||||
if (language.IsNullOrEmpty())
|
||||
{
|
||||
language = appConfiguration.DefaultLanguage;
|
||||
}
|
||||
|
||||
//
|
||||
var result = !(await GetString(language: language))
|
||||
.IsNullOrDefault();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Terms and Conditions for Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="language">if null, used Default Language ...</param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> GetTerms(string language = null)
|
||||
{
|
||||
//
|
||||
var result = string.Empty;
|
||||
var entity = await GetString(language: language);
|
||||
|
||||
//
|
||||
if (!entity.IsNullOrDefault())
|
||||
{
|
||||
result = entity.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>
|
||||
/// <returns></returns>
|
||||
public async Task<string> AddTerms(
|
||||
string language,
|
||||
string terms,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!language.IsNullOrEmpty() &&
|
||||
!terms.IsNullOrEmpty() &&
|
||||
!userInfo.IsNullOrDefault() &&
|
||||
language != appConfiguration.DefaultLanguage;
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Exists ...
|
||||
isValid = await HasTerms(language: language);
|
||||
if (isValid)
|
||||
{
|
||||
XException.Duplicate.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Permission ...
|
||||
isValid =
|
||||
!userInfo.IsNullOrDefault() &&
|
||||
userInfo.Roles.Any(r => r.ToNormalString().Contains("admin"));
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Add Terms ...
|
||||
XString entity = null;
|
||||
try
|
||||
{
|
||||
//
|
||||
entity = new XString
|
||||
{
|
||||
Language = language,
|
||||
ResourceTitle = termsId,
|
||||
TranslatedValue = terms,
|
||||
};
|
||||
entity = await stringRepository.AddAsync(entity);
|
||||
}
|
||||
catch { }
|
||||
|
||||
//
|
||||
var result = string.Empty;
|
||||
if (!entity.IsNullOrDefault())
|
||||
{
|
||||
result = entity.TranslatedValue;
|
||||
}
|
||||
|
||||
//
|
||||
if (!result.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Add.GetStringValue(),
|
||||
payLoad: entity.ToJSON(camelCase: true),
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
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>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> RemoveTerms(
|
||||
string language,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var isValid =
|
||||
!language.IsNullOrEmpty() &&
|
||||
!userInfo.IsNullOrDefault() &&
|
||||
language != appConfiguration.DefaultLanguage;
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Exists ...
|
||||
XString entity = await GetString(language);
|
||||
isValid = !entity.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Permission ...
|
||||
isValid =
|
||||
!userInfo.IsNullOrDefault() &&
|
||||
userInfo.Roles.Any(r => r.ToNormalString().Contains("admin"));
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
entity = await stringRepository.RemoveAsync(entity.Id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
entity = null;
|
||||
}
|
||||
|
||||
//
|
||||
var result = !entity.IsNullOrDefault();
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Delete.GetStringValue(),
|
||||
payLoad: entity.ToJSON(camelCase: true),
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
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>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> UpdateTerms(
|
||||
string language,
|
||||
string terms,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var isValid =
|
||||
!terms.IsNullOrEmpty() &&
|
||||
!language.IsNullOrEmpty() &&
|
||||
!userInfo.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Exists ...
|
||||
XString entity = await GetString(language);
|
||||
isValid = !entity.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Permission ...
|
||||
isValid =
|
||||
!userInfo.IsNullOrDefault() &&
|
||||
userInfo.Roles.Any(r => r.ToNormalString().Contains("admin"));
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
entity.TranslatedValue = terms;
|
||||
entity = await stringRepository.UpdateAsync(entity.Id, entity);
|
||||
}
|
||||
catch
|
||||
{
|
||||
entity = null;
|
||||
}
|
||||
|
||||
//
|
||||
var result = !entity.IsNullOrDefault();
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Update.GetStringValue(),
|
||||
payLoad: entity.ToJSON(camelCase: true),
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Hub Actions ...
|
||||
/// <summary>
|
||||
/// Send Custom Push Message ...
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="payLoad"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SendPush(
|
||||
string action,
|
||||
string payLoad,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
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);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
/// <summary>
|
||||
/// Retrieve Terms and Conditions XString Entity ...
|
||||
/// </summary>
|
||||
/// <param name="language"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<XString> GetString(string language = null)
|
||||
{
|
||||
//
|
||||
// Normalize ...
|
||||
if (language.IsNullOrEmpty())
|
||||
{
|
||||
language = appConfiguration.DefaultLanguage;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve ...
|
||||
XString result = null;
|
||||
try
|
||||
{
|
||||
//
|
||||
result = await stringRepository.FindOneAsync(x =>
|
||||
x.Language == language &&
|
||||
x.ResourceTitle == this.termsId
|
||||
);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xCommons.Extensions;
|
||||
using xPushService.Base;
|
||||
using xPushService.Constants;
|
||||
using xStringService.Models.Entities;
|
||||
|
||||
namespace xServices.TermsConditions.Push
|
||||
{
|
||||
public class XTermsHub : XBaseHub
|
||||
{
|
||||
public XTermsHub(ILogger<XBaseHub> logger) : base(logger)
|
||||
{
|
||||
}
|
||||
|
||||
//
|
||||
#region Hub Actions ...
|
||||
/// <summary>
|
||||
/// Add Specified Terms and Conditions ...
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Add(XString entity)
|
||||
{
|
||||
//
|
||||
var connectionId = Context.ConnectionId;
|
||||
if (!connectionId.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
await Clients
|
||||
.AllExcept(connectionId)
|
||||
.SendAsync(
|
||||
XBaseEntityHubAction.Add.GetStringValue(),
|
||||
entity.ToJSON(camelCase: true),
|
||||
connectionId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deleted Specified Terms and Conditions ...
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Delete(XString entity)
|
||||
{
|
||||
//
|
||||
var connectionId = Context.ConnectionId;
|
||||
if (!connectionId.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
await Clients
|
||||
.AllExcept(connectionId)
|
||||
.SendAsync(
|
||||
XBaseEntityHubAction.Delete.GetStringValue(),
|
||||
entity.ToJSON(camelCase: true),
|
||||
connectionId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Notify an Entity Updated ...
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Update(XString entity)
|
||||
{
|
||||
//
|
||||
var connectionId = Context.ConnectionId;
|
||||
if (!connectionId.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
await Clients
|
||||
.AllExcept(connectionId)
|
||||
.SendAsync(
|
||||
XBaseEntityHubAction.Update.GetStringValue(),
|
||||
entity.ToJSON(camelCase: true),
|
||||
connectionId
|
||||
);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user