Files
xStringService/Providers/XBaseItemResourceProvider.cs
T
2026-06-02 23:42:49 +03:30

256 lines
7.1 KiB
C#

using System;
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 xStringService.Interfaces;
using xStringService.Interfaces.Dtos;
using xStringService.Models.Dtos;
namespace xStringService.Providers
{
public abstract class XBaseItemResourceProvider : IXItemResourceProvider
{
/// <summary>
/// Item Name for Resource Providing ...
/// </summary>
private readonly string item;
/// <summary>
/// an Implementation of Item Resource Provider
/// </summary>
/// <value></value>
private readonly XBaseResourceProvider provider;
/// <summary>
/// String Provider Service ...
/// </summary>
private readonly IXStringServiceProvider stringProvider;
//
#region Constructor ...
protected XBaseItemResourceProvider(
string item,
IXStringServiceProvider stringProvider
)
{
//
this.item = item;
this.stringProvider = stringProvider;
provider = new XBaseResourceProvider(
item,
stringProvider
);
}
#endregion
//
#region All Item Actions ...
/// <summary>
/// Retrieve a List of Exists Languages for Current Item ...
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IEnumerable<string>> GetLanguages(
CancellationToken cancellationToken = default
)
{
//
var result =
(await GetResourceLocales(cancellationToken))
.Select(x => x.Language)
.Distinct()
.ToList();
//
return result;
}
/// <summary>
/// Retrieve all Exists Items of type ...
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public 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)
);
//
return result;
}
#endregion
//
#region Single Item Actions ...
/// <summary>
/// Retrieve a List of Exists Languages for Current Item ...
/// </summary>
/// <param name="identifier"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IEnumerable<string>> GetLanguages(
object identifier,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
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();
if (!isValid)
{
XException.InvalidArgs.Throw();
}
//
// Set Identifier ...
SetResourceTitle(identifier);
//
var result =
await provider.GetResourceLocales(cancellationToken);
return result;
}
public async Task<string> AddOrUpdateItemResourcer(
object identifier,
string language,
string translation,
string connectionId = null,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default
)
{
//
var result = string.Empty;
//
// Validate Args ...
var isValid =
!identifier.IsNull() &&
!language.IsNullOrEmpty() &&
!translation.IsNullOrEmpty();
if (!isValid)
{
return result;
}
//
// Prepare Identifier ...
var resourceTitle = GetResourceTitle(identifier);
//
// Set Resource Title ...
SetResourceTitle(resourceTitle);
//
// Try to Add Or Update ...
var resource = await provider.AddOrUpdateLocale(
userInfo: userInfo,
connectionId: connectionId,
locale: new XLocaleResourceDto
{
Language = language,
Value = translation
},
cancellationToken: cancellationToken
);
//
// Validate Result ...
isValid = !resource.IsNullOrDefault();
if (isValid)
{
result = resourceTitle;
}
//
return result;
}
#endregion
//
#region Private ...
/// <summary>
/// Retrieve Resource Title ...
/// </summary>
/// <param name="identifier"></param>
/// <returns></returns>
private string GetResourceTitle(object identifier = null)
{
//
var result = item;
//
if (!identifier.IsNull())
{
result = $"{item}_{identifier}";
}
//
return result;
}
/// <summary>
/// Set Resource Title ...
/// </summary>
/// <param name="identifier"></param>
private void SetResourceTitle(object identifier = null)
{
//
var itemIdentifier = GetResourceTitle(identifier);
provider.SetField(
field: "resource",
value: $"{itemIdentifier}"
);
}
#endregion
}
}