last ...
This commit is contained in:
@@ -5,7 +5,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityModels.Models;
|
||||
using xModels.Dtos;
|
||||
using xStringService.Interfaces;
|
||||
using xStringService.Interfaces.Dtos;
|
||||
@@ -15,6 +14,8 @@ namespace xStringService.Providers
|
||||
{
|
||||
public abstract class XBaseItemResourceProvider : IXItemResourceProvider
|
||||
{
|
||||
//
|
||||
#region Props ...
|
||||
/// <summary>
|
||||
/// Item Name for Resource Providing ...
|
||||
/// </summary>
|
||||
@@ -30,6 +31,7 @@ namespace xStringService.Providers
|
||||
/// String Provider Service ...
|
||||
/// </summary>
|
||||
private readonly IXStringServiceProvider stringProvider;
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Constructor ...
|
||||
@@ -49,22 +51,20 @@ namespace xStringService.Providers
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region All Item Actions ...
|
||||
#region Group Actions ...
|
||||
/// <summary>
|
||||
/// Retrieve a List of Exists Languages for Current Item ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<string>> GetLanguages(
|
||||
public virtual async Task<IEnumerable<string>> GetLanguages(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result =
|
||||
(await GetResourceLocales(cancellationToken))
|
||||
var result = (await ExtractItems(cancellationToken))
|
||||
.Select(x => x.Language)
|
||||
.Distinct()
|
||||
.ToList();
|
||||
.Distinct();
|
||||
|
||||
//
|
||||
return result;
|
||||
@@ -75,145 +75,439 @@ namespace xStringService.Providers
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XStringDto>> GetResourceLocales(
|
||||
public virtual async Task<IEnumerable<XStringDto>> GetResourceLocales(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var identifier = GetResourceTitle();
|
||||
var result = await stringProvider
|
||||
.FindManyAsync(
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
orderBuilder:
|
||||
x => x
|
||||
.OrderBy(y => y.Language)
|
||||
.ThenBy(y => y.ResourceTitle),
|
||||
predicate: x =>
|
||||
x.ResourceTitle.StartsWith(identifier, StringComparison.InvariantCultureIgnoreCase)
|
||||
var result = await ExtractItems(cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all Exists Locales which Related to this Provider ...
|
||||
/// </summary>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task RemoveResourceLocales(
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var isDelete = false;
|
||||
XStringDto deleted = null;
|
||||
var items = await ExtractItems(cancellationToken);
|
||||
foreach (var item in items)
|
||||
{
|
||||
//
|
||||
// Check Cancellation ...
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
// Remove Item ...
|
||||
deleted = await stringProvider.RemoveAsync(
|
||||
id: item.Id,
|
||||
softDelete: false,
|
||||
saveChanges: true,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Check Removation Succeed ...
|
||||
isDelete = !deleted.IsNullOrDefault();
|
||||
if (!isDelete)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Item Actions ...
|
||||
/// <summary>
|
||||
/// Get all Exists Items for Specified identitifer ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<IEnumerable<XStringDto>> GetResourceLocales(
|
||||
object identifier,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !identifier.IsNull();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Resource Title ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
// Retrieve Resource List ...
|
||||
var result = await provider
|
||||
.GetResourceLocales(cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all Exists Items for Specified identitifer ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<IEnumerable<XLocaleResourceDto>> GetLocales(
|
||||
object identifier,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !identifier.IsNull();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Resource Title ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
// Retrieve Resource List ...
|
||||
var result = await provider
|
||||
.GetLocales(cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all Exists Locales Representaion based on Specified Identitifer
|
||||
/// as XResourceDto ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<XResourceDto> GetResource(
|
||||
object identifier,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !identifier.IsNull();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Resource Title ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
// Retrieve Resource List ...
|
||||
var result = await provider
|
||||
.GetResource(cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Or Update Locale for Specified Identitifer ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="locale"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<XResourceDto> AddOrUpdateLocale(
|
||||
object identifier,
|
||||
XLocaleResourceDto locale,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!identifier.IsNull() &&
|
||||
!locale.IsNullOrDefault() &&
|
||||
!locale.Value.IsNullOrEmpty() &&
|
||||
!locale.Language.IsNullOrEmpty();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Resource Title ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
// Retrieve Resource List ...
|
||||
var result = await provider
|
||||
.AddOrUpdateLocale(
|
||||
locale: locale,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Single Item Actions ...
|
||||
/// <summary>
|
||||
/// Retrieve a List of Exists Languages for Current Item ...
|
||||
/// Update a Resource State ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<string>> GetLanguages(
|
||||
public virtual async Task<XResourceDto> AddOrUpdateResource(
|
||||
object identifier,
|
||||
XResourceDto resource,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !identifier.IsNull();
|
||||
var isValid =
|
||||
!identifier.IsNull();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Set Identifier ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
var result =
|
||||
await provider.GetLanguages(cancellationToken);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all Exists Items of type ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XStringDto>> GetResourceLocales(
|
||||
object identifier,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !identifier.IsNull();
|
||||
// Retrieve Identified Resource Title ...
|
||||
var identifiedResource = GetResourceTitle(identifier);
|
||||
isValid =
|
||||
!resource.IsNullOrDefault() &&
|
||||
(resource.Resource.IsNullOrEmpty() ||
|
||||
resource.Resource == identifiedResource);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
if (resource.Resource.IsNullOrEmpty())
|
||||
{
|
||||
resource.Resource = identifiedResource;
|
||||
}
|
||||
|
||||
//
|
||||
// Set Identifier ...
|
||||
// Setting Resource Title ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
var result =
|
||||
await provider.GetResourceLocales(cancellationToken);
|
||||
// Retrieve Resource List ...
|
||||
var result = await provider
|
||||
.AddOrUpdateResource(
|
||||
resource: resource,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<string> AddOrUpdateItemResourcer(
|
||||
/// <summary>
|
||||
/// Remove Specified Language Locale of Specified Identifier ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<XResourceDto> RemoveLocale(
|
||||
object identifier,
|
||||
string language,
|
||||
string translation,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = string.Empty;
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!identifier.IsNull() &&
|
||||
!language.IsNullOrEmpty() &&
|
||||
!translation.IsNullOrEmpty();
|
||||
!language.IsNullOrEmpty();
|
||||
if (!isValid)
|
||||
{
|
||||
return result;
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Locale Exists ...
|
||||
isValid = await HasLocale(
|
||||
language: language,
|
||||
identifier: identifier,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Identifier ...
|
||||
var resourceTitle = GetResourceTitle(identifier);
|
||||
var locale = (await GetLocales(
|
||||
identifier: identifier,
|
||||
cancellationToken: cancellationToken
|
||||
))
|
||||
.FirstOrDefault(x =>
|
||||
x.Language.Equals(language, StringComparison.InvariantCultureIgnoreCase));
|
||||
isValid = !locale.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Set Resource Title ...
|
||||
SetResourceTitle(resourceTitle);
|
||||
// Setting Resource Title ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
// Try to Add Or Update ...
|
||||
var resource = await provider.AddOrUpdateLocale(
|
||||
userInfo: userInfo,
|
||||
var result = await provider.RemoveLocale(
|
||||
locale: locale,
|
||||
connectionId: connectionId,
|
||||
locale: new XLocaleResourceDto
|
||||
{
|
||||
Language = language,
|
||||
Value = translation
|
||||
},
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Validate Result ...
|
||||
isValid = !resource.IsNullOrDefault();
|
||||
if (isValid)
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all Exists Locales for Specified Identifier ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task RemoveLocales(
|
||||
object identifier,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!identifier.IsNull();
|
||||
if (!isValid)
|
||||
{
|
||||
result = resourceTitle;
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var resource = await GetResource(
|
||||
identifier: identifier,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
isValid = !resource.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Resource Title ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
await provider.RemoveResource(
|
||||
resource: resource,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check Specified ocale Exists for Specified Identifier ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<bool> HasLocale(
|
||||
object identifier,
|
||||
string language,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
!identifier.IsNull() &&
|
||||
!language.IsNullOrEmpty();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Resource Title ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
var result = await provider.HasLocale(
|
||||
language: language,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get all Exists Languages for Specified Identifier ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<IEnumerable<string>> GetLanguages(
|
||||
object identifier,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !identifier.IsNull();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Resource Title ...
|
||||
SetResourceTitle(identifier);
|
||||
|
||||
//
|
||||
var result = await provider
|
||||
.GetLanguages(cancellationToken);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
@@ -251,6 +545,29 @@ namespace xStringService.Providers
|
||||
value: $"{itemIdentifier}"
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// xtract all Items which Belongs to Provider ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<IEnumerable<XStringDto>> ExtractItems(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await stringProvider
|
||||
.FindManyAsync(
|
||||
includeBuilder: null,
|
||||
cancellationToken: cancellationToken,
|
||||
orderBuilder: x => x.OrderBy(y => y.Id),
|
||||
predicate: x =>
|
||||
x.ResourceTitle.StartsWith(
|
||||
item,
|
||||
StringComparison.InvariantCultureIgnoreCase)
|
||||
);
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityModels.Models;
|
||||
using xModels.Dtos;
|
||||
using xStringService.Extensions;
|
||||
using xStringService.Interfaces;
|
||||
@@ -181,13 +180,11 @@ namespace xStringService.Providers
|
||||
/// Add or Update a Resource Model ...
|
||||
/// </summary>
|
||||
/// <param name="resource">a XResourceDto Model ...</param>
|
||||
/// <param name="userInfo">Actor User for Permissions ...</param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<XResourceDto> AddOrUpdateResource(
|
||||
XResourceDto resource,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
@@ -209,7 +206,6 @@ namespace xStringService.Providers
|
||||
{
|
||||
await RemoveResource(
|
||||
resource: result,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
@@ -248,7 +244,6 @@ namespace xStringService.Providers
|
||||
/// <returns></returns>
|
||||
public virtual async Task<XResourceDto> AddOrUpdateLocale(
|
||||
XLocaleResourceDto locale,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
@@ -297,11 +292,11 @@ namespace xStringService.Providers
|
||||
/// Remove Locale ...
|
||||
/// </summary>
|
||||
/// <param name="locale"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<XResourceDto> RemoveLocale(
|
||||
XLocaleResourceDto locale,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
@@ -334,17 +329,26 @@ namespace xStringService.Providers
|
||||
/// Remove Specified Resource Model ...
|
||||
/// </summary>
|
||||
/// <param name="resource">a XResourceDto Model ...</param>
|
||||
/// <param name="userInfo">Actor User for Permissions ...</param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task RemoveResource(
|
||||
XResourceDto resource,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Resource ...
|
||||
var isValid =
|
||||
!resource.IsNullOrDefault() &&
|
||||
!resource.Resource.IsNullOrEmpty() &&
|
||||
resource.Resource == this.resource;
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var items = await ExtractItems(cancellationToken);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user