392 lines
12 KiB
C#
392 lines
12 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xIdentityModels.Models;
|
|
using xModels.Dtos;
|
|
using xPushService.Providers;
|
|
using xStringService.Extensions;
|
|
using xStringService.Interfaces;
|
|
using xStringService.Interfaces.Dtos;
|
|
using xStringService.Models.Dtos;
|
|
using xStringService.Models.Entities;
|
|
using xStringService.Providers.Dtos;
|
|
|
|
namespace xStringService.Providers
|
|
{
|
|
public class XStringProvider : XBaseProvided<XString, XStringDto, int, XStringDtoHub>, IXStringProvider
|
|
{
|
|
public XStringProvider(
|
|
IXStringServiceProvider provider
|
|
) : base(
|
|
provider,
|
|
permittedRoles: new string[] { }
|
|
)
|
|
{ }
|
|
|
|
public override bool IsOwned(
|
|
XStringDto item,
|
|
XUserClaimsInfoDto userInfo
|
|
)
|
|
{
|
|
return !item.IsNullOrDefault();
|
|
}
|
|
|
|
public override bool IsOwned(
|
|
XString item,
|
|
XUserClaimsInfoDto userInfo
|
|
)
|
|
{
|
|
return !item.IsNullOrDefault();
|
|
}
|
|
|
|
//
|
|
#region Tools ...
|
|
/// <summary>
|
|
/// Retrieve a List of Exists Languages ...
|
|
/// </summary>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<string>> GetLanguages(
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = (await GetAll(
|
|
includeBuilder: null,
|
|
cancellationToken: cancellationToken,
|
|
orderBuilder: x => x.OrderBy(y => y.Language)
|
|
))
|
|
.Select(x => x.Language)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve a List of Resource Titles ...
|
|
/// </summary>
|
|
/// <param name="language">if provided, returns all language Specified resource titles, if not, all Resource Titles</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<string>> GetResourceTitles(
|
|
string language = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = (await FindMany(
|
|
includeBuilder: null,
|
|
cancellationToken: cancellationToken,
|
|
orderBuilder: x => x.OrderBy(y => y.ResourceTitle),
|
|
predicate: x =>
|
|
string.IsNullOrWhiteSpace(language) ||
|
|
x.Language.Equals(language, System.StringComparison.InvariantCultureIgnoreCase)
|
|
))
|
|
.Select(x => x.ResourceTitle)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve all Exists Items which belongs to Specified Resource Title ...
|
|
/// </summary>
|
|
/// <param name="resource">Specified Resource Title</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XStringDto>> GetResourceLocales(
|
|
string resource,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await FindMany(
|
|
includeBuilder: null,
|
|
cancellationToken: cancellationToken,
|
|
orderBuilder: x => x.OrderBy(y => y.Language),
|
|
predicate: x =>
|
|
!string.IsNullOrWhiteSpace(resource) &&
|
|
resource.Equals(x.ResourceTitle, System.StringComparison.InvariantCultureIgnoreCase)
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Translation of a Resource in Specified Language ...
|
|
/// </summary>
|
|
/// <param name="language">Specified Language ...</param>
|
|
/// <param name="resource">Specified Resource Title ...</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> GetResourceTranslation(
|
|
string language,
|
|
string resource,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = (await FindOne(
|
|
includeBuilder: null,
|
|
cancellationToken: cancellationToken,
|
|
predicate: x =>
|
|
!string.IsNullOrWhiteSpace(language) &&
|
|
!string.IsNullOrWhiteSpace(resource) &&
|
|
x.Language.Equals(language, System.StringComparison.InvariantCultureIgnoreCase) &&
|
|
x.ResourceTitle.Equals(resource, System.StringComparison.InvariantCultureIgnoreCase)
|
|
))?
|
|
.TranslatedValue ?? "";
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve a Resource as Locale Model ...
|
|
/// </summary>
|
|
/// <param name="language">Specified Language ...</param>
|
|
/// <param name="resource">Specified Resource Title ...</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XLocaleResourceDto> GetLocale(
|
|
string language,
|
|
string resource,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = (await FindOne(
|
|
includeBuilder: null,
|
|
cancellationToken: cancellationToken,
|
|
predicate: x =>
|
|
!string.IsNullOrWhiteSpace(language) &&
|
|
!string.IsNullOrWhiteSpace(resource) &&
|
|
x.Language.Equals(language, System.StringComparison.InvariantCultureIgnoreCase) &&
|
|
x.ResourceTitle.Equals(resource, System.StringComparison.InvariantCultureIgnoreCase)
|
|
))?
|
|
.ToXLocaleResourceDto() ?? null;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve all Exists Resource Instances as Locale Models ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XLocaleResourceDto>> GetLocales(
|
|
string resource,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = (await GetResourceLocales(
|
|
resource: resource,
|
|
cancellationToken: cancellationToken
|
|
))
|
|
.ToList()
|
|
.Select(x => x.ToXLocaleResourceDto())
|
|
.ToList();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve all Locales of Specified Resource and Represent as XResourceDto ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XResourceDto> GetResource(
|
|
string resource,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XResourceDto result = null;
|
|
|
|
//
|
|
// Validate Resource ...
|
|
if (resource.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Extract Locales of Resource ...
|
|
var locales = await GetLocales(
|
|
resource: resource,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
// Prepare Result ...
|
|
result = new XResourceDto
|
|
{
|
|
Locales = locales,
|
|
Resource = resource,
|
|
};
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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 async Task<XResourceDto> AddOrUpdateResource(
|
|
XResourceDto resource,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
XResourceDto result = null;
|
|
|
|
//
|
|
// Validate Args ...
|
|
var isValid =
|
|
!resource.IsNullOrDefault() &&
|
|
!resource.Resource.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Permissions ...
|
|
isValid =
|
|
userInfo.IsNullOrDefault() ||
|
|
HasPermision(userInfo);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
// Remove all Resources if Exists ...
|
|
result = await GetResource(
|
|
resource: resource.Resource,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
var isExists = !result.IsNullOrDefault();
|
|
if (isExists)
|
|
{
|
|
//
|
|
await RemoveResource(
|
|
resource: result,
|
|
userInfo: userInfo,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
//
|
|
// Loop through all Exists Locales and Add them ...
|
|
foreach (var locale in resource.Locales)
|
|
{
|
|
//
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
//
|
|
var item = new XStringDto
|
|
{
|
|
Language = locale.Language,
|
|
TranslatedValue = locale.Value,
|
|
ResourceTitle = resource.Resource,
|
|
};
|
|
await Add(
|
|
item: item,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
//
|
|
// Reload Result after Updates ...
|
|
result = await GetResource(
|
|
resource: resource.Resource,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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 async Task RemoveResource(
|
|
XResourceDto resource,
|
|
XUserClaimsInfoDto userInfo = null,
|
|
string connectionId = null,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = !resource.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
var items = await GetResourceLocales(
|
|
resource: resource.Resource,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
isValid =
|
|
!items.IsNullOrDefault() &&
|
|
items.HasChild();
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
foreach (var item in items)
|
|
{
|
|
//
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
//
|
|
await Remove(
|
|
id: item.Id,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |