Initial ...
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user