using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using xCommons.Extensions;
using xExceptions.Constants;
using xStringService.Interfaces;
using xStringService.Interfaces.Dtos;
using xStringService.Models.Dtos;
namespace xStringService.Providers
{
public abstract class XBaseItemResourceProvider : IXItemResourceProvider
{
///
/// Item Name for Resource Providing ...
///
private readonly string item;
///
/// an Implementation of Item Resource Provider
///
///
private readonly XBaseResourceProvider provider;
///
/// String Provider Service ...
///
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 ...
///
/// Retrieve a List of Exists Languages for Current Item ...
///
///
///
public async Task> GetLanguages(
CancellationToken cancellationToken = default
)
{
//
var result =
(await GetResourceLocales(cancellationToken))
.Select(x => x.Language)
.Distinct()
.ToList();
//
return result;
}
///
/// Retrieve all Exists Items of type ...
///
///
///
public async Task> 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 ...
///
/// Retrieve a List of Exists Languages for Current Item ...
///
///
///
///
public async Task> 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;
}
///
/// Retrieve all Exists Items of type ...
///
///
///
///
public async Task> 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;
}
#endregion
//
#region Private ...
private string GetResourceTitle(object identifier = null)
{
//
var result = item;
//
if (!identifier.IsNull())
{
result = $"{item}_{identifier}";
}
//
return result;
}
private void SetResourceTitle(object identifier = null)
{
//
var itemIdentifier = GetResourceTitle(identifier);
provider.SetField(
field: "resource",
value: $"{itemIdentifier}"
);
}
#endregion
}
}