349 lines
9.3 KiB
C#
349 lines
9.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Configurations;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xModels.Dtos;
|
|
using xStringService.Extensions;
|
|
using xStringService.Interfaces;
|
|
using xStringService.Models.Dtos;
|
|
|
|
namespace xStringService.Base
|
|
{
|
|
/// <summary>
|
|
/// a Base Class for Handling Resources Based on Specified Types on
|
|
/// String Resources Use ...
|
|
/// such as:
|
|
/// - Terms and Conditions;
|
|
/// - Contents;
|
|
/// - News;
|
|
/// - etc ...
|
|
/// </summary>
|
|
public abstract class XBaseStringResourceProvider : IXBaseStringResourceProvider
|
|
{
|
|
//
|
|
#region Props ...
|
|
public string Prefix { get; }
|
|
public IXStringProvider Provider { get; }
|
|
public XAppConfiguration AppConfiguration { get; }
|
|
#endregion
|
|
|
|
//
|
|
#region Constructor ...
|
|
public XBaseStringResourceProvider(
|
|
string prefix,
|
|
IXStringProvider provider,
|
|
XAppConfiguration appConfiguration
|
|
)
|
|
{
|
|
//
|
|
Prefix = prefix;
|
|
Provider = provider;
|
|
AppConfiguration = appConfiguration;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Actions ...
|
|
/// <summary>
|
|
/// Prepare Resource Title by Given Data ...
|
|
/// </summary>
|
|
/// <param name="suffix"></param>
|
|
/// <returns></returns>
|
|
public string GetResourceTitle(string suffix)
|
|
{
|
|
//
|
|
string result = "";
|
|
|
|
//
|
|
if (suffix.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
result = $"{Prefix}_${suffix}";
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Specified Locales ...
|
|
/// </summary>
|
|
/// <param name="suffix"></param>
|
|
/// <param name="locales"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XStringDto>> Add(
|
|
string suffix,
|
|
IEnumerable<XLocaleResourceDto> locales
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var has =
|
|
locales.HasChild() &&
|
|
!suffix.IsNullOrEmpty();
|
|
if (!has)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Prepare Resource ID ...
|
|
string resourceID = GetResourceTitle(suffix);
|
|
var result = await locales.SelectAsync(async l =>
|
|
{
|
|
//
|
|
// Normalize Language ...
|
|
if (l.Language.IsNullOrEmpty())
|
|
{
|
|
l.Language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
|
|
//
|
|
// Prepare Model for Add ...
|
|
var model = new XStringDto
|
|
{
|
|
Language = l.Language,
|
|
TranslatedValue = l.Value,
|
|
ResourceTitle = resourceID,
|
|
};
|
|
model = await Provider.AddEntity(model);
|
|
return model;
|
|
});
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Or Update Specified Locals ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="locales"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XStringDto>> AddOrUpdate(
|
|
string resource,
|
|
IEnumerable<XLocaleResourceDto> locales
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid =
|
|
locales.HasChild() &&
|
|
!resource.IsNullOrEmpty() &&
|
|
locales.All(l => !l.Value.IsNullOrEmpty() && !l.Language.IsNullOrEmpty());
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Resource ...
|
|
isValid = resource.Contains(Prefix);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await locales.SelectAsync(async l =>
|
|
await Provider.AddOrUpdateByLocaleResource(
|
|
item: l,
|
|
resource: resource
|
|
)
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Specified Locale ...
|
|
/// </summary>
|
|
/// <param name="suffix"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<XLocaleResourceDto> GetLocale(
|
|
string suffix,
|
|
string language = null
|
|
)
|
|
{
|
|
//
|
|
// Normalize ...
|
|
if (language.IsNullOrEmpty())
|
|
{
|
|
language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
|
|
//
|
|
// Validate ...
|
|
if (suffix.IsNullOrEmpty() || language.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Prepare Resource Title ...
|
|
string resource = GetResourceTitle(suffix);
|
|
|
|
//
|
|
// Check Resource Exists ...
|
|
var isExists = await Provider.IsResourceExists(
|
|
resource: resource,
|
|
language: language
|
|
);
|
|
if (!isExists)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve and Validate Dto Model ...
|
|
var model = await Provider.Get(
|
|
resource: resource,
|
|
language: language
|
|
);
|
|
if (model.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
// Prepare Result ...
|
|
var result = model
|
|
.ToXLocaleResourceDto();
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Specified Resource ...
|
|
/// </summary>
|
|
/// <param name="suffix"></param>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public async Task<XResourceDto> AddResource(
|
|
string suffix,
|
|
XResourceDto item
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (suffix.IsNullOrEmpty() ||
|
|
item.IsNull() ||
|
|
!item.Locales.HasChild() ||
|
|
!item.Locales.All(l => !l.Value.IsNullOrEmpty()))
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Prepare Resource ID ...
|
|
var addedLocals = await Add(suffix, item.Locales);
|
|
if (!addedLocals.HasChild())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await GetResource(suffix);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Resources of Specified Resource ...
|
|
/// </summary>
|
|
/// <param name="suffix"></param>
|
|
/// <returns></returns>
|
|
public async Task<XResourceDto> GetResource(string suffix)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (suffix.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Prepare Resource ID ...
|
|
string resourceID = GetResourceTitle(suffix);
|
|
|
|
//
|
|
// Prepare ...
|
|
var result = await Provider.GetResource(resourceID);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Suffix Resources ...
|
|
/// </summary>
|
|
/// <param name="suffix"></param>
|
|
/// <returns></returns>
|
|
public async Task Remove(string suffix)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (suffix.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var resourceID = GetResourceTitle(suffix);
|
|
await Provider.RemoveResource(resourceID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Resource ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task Remove(
|
|
string resource,
|
|
string language
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid =
|
|
!resource.IsNullOrEmpty() &&
|
|
!language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check resource ID Contains Prefix ...
|
|
isValid = resource.Contains(Prefix);
|
|
if (!isValid)
|
|
{
|
|
XException.NotAllowed.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Exists ...
|
|
var isExists = await Provider.IsResourceExists(
|
|
resource: resource,
|
|
language: language
|
|
);
|
|
if (!isExists)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Remove Resource ...
|
|
await Provider.Remove(
|
|
resource: resource,
|
|
language: language
|
|
);
|
|
}
|
|
#endregion
|
|
}
|
|
} |