using System.Collections.Generic;
using System.Linq;
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
{
///
/// a Base Class for Handling Resources Based on Specified Types on
/// String Resources Use ...
/// such as:
/// - Terms and Conditions;
/// - Contents;
/// - News;
/// - etc ...
///
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 ...
///
/// Prepare Resource Title by Given Data ...
///
///
///
public string GetResourceTitle(string suffix)
{
//
string result = "";
//
if (suffix.IsNullOrEmpty())
{
return result;
}
//
result = $"{Prefix}_${suffix}";
//
return result;
}
///
/// Add Specified Locales ...
///
///
///
///
public async Task> Add(
string suffix,
IEnumerable 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;
}
///
/// Add Or Update Specified Locals ...
///
///
///
///
public async Task> AddOrUpdate(
string resource,
IEnumerable 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;
}
///
/// Get Specified Locale ...
///
///
///
///
public async Task 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;
}
///
/// Add Specified Resource ...
///
///
///
///
public async Task 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;
}
///
/// Get Resources of Specified Resource ...
///
///
///
public async Task 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;
}
///
/// Remove Specified Suffix Resources ...
///
///
///
public async Task Remove(string suffix)
{
//
// Validate ...
if (suffix.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
//
var resourceID = GetResourceTitle(suffix);
await Provider.RemoveResource(resourceID);
}
///
/// Remove Specified Resource ...
///
///
///
///
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
}
}