Add XBaseResourceProvider Class and Interfaces for Handle String Resource Based Services Implementation ...
This commit is contained in:
@@ -0,0 +1,349 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using xExceptions.Constants;
|
||||
using xCommons.Extensions;
|
||||
using xStringService.Interfaces;
|
||||
using xStringService.Providers;
|
||||
using xStringService.Interfaces.Entities;
|
||||
|
||||
namespace xStringService.DI
|
||||
{
|
||||
@@ -74,6 +75,18 @@ namespace xStringService.DI
|
||||
throw exception;
|
||||
}
|
||||
|
||||
//
|
||||
// Check String Repository Exists ...
|
||||
var isStringRepositoryExists = !services
|
||||
.GetRegisteredService<IXStringRepository>()
|
||||
.IsNull();
|
||||
if (!isStringRepositoryExists)
|
||||
{
|
||||
//
|
||||
Log("AddStringService failed, IXStringRepository not provided ...");
|
||||
throw exception;
|
||||
}
|
||||
|
||||
//
|
||||
services.Add(new ServiceDescriptor(typeof(IXStringProvider), typeof(XStringProvider), lifeTime));
|
||||
Log("AddStringService Succeed ...");
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Configurations;
|
||||
using xModels.Dtos;
|
||||
using xStringService.Models.Dtos;
|
||||
|
||||
namespace xStringService.Interfaces
|
||||
{
|
||||
public interface IXBaseStringResourceProvider
|
||||
{
|
||||
//
|
||||
#region Props ...
|
||||
string Prefix { get; }
|
||||
IXStringProvider Provider { get; }
|
||||
XAppConfiguration AppConfiguration { get; }
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Actions ...
|
||||
/// <summary>
|
||||
/// Prepare Resource Title by Given Data ...
|
||||
/// </summary>
|
||||
/// <param name="suffix"></param>
|
||||
/// <returns></returns>
|
||||
string GetResourceTitle(string suffix);
|
||||
|
||||
/// <summary>
|
||||
/// Add Specified Locales ...
|
||||
/// </summary>
|
||||
/// <param name="suffix"></param>
|
||||
/// <param name="locales"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XStringDto>> Add(
|
||||
string suffix,
|
||||
IEnumerable<XLocaleResourceDto> locales
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Add Or Update Specified Locals ...
|
||||
/// </summary>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="locales"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<XStringDto>> AddOrUpdate(
|
||||
string resource,
|
||||
IEnumerable<XLocaleResourceDto> locales
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Get Specified Locale ...
|
||||
/// </summary>
|
||||
/// <param name="suffix"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <returns></returns>
|
||||
Task<XLocaleResourceDto> GetLocale(
|
||||
string suffix,
|
||||
string language = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Add Specified Resource ...
|
||||
/// </summary>
|
||||
/// <param name="suffix"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
Task<XResourceDto> AddResource(
|
||||
string suffix,
|
||||
XResourceDto item
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Get Resources of Specified Resource ...
|
||||
/// </summary>
|
||||
/// <param name="suffix"></param>
|
||||
/// <returns></returns>
|
||||
Task<XResourceDto> GetResource(string suffix);
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Suffix Resources ...
|
||||
/// </summary>
|
||||
/// <param name="suffix"></param>
|
||||
/// <returns></returns>
|
||||
Task Remove(string suffix);
|
||||
|
||||
/// <summary>
|
||||
/// Remove Specified Resource ...
|
||||
/// </summary>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <returns></returns>
|
||||
Task Remove(
|
||||
string resource,
|
||||
string language
|
||||
);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,17 @@ namespace xStringService.Interfaces
|
||||
string resource,
|
||||
string language = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified Resource ...
|
||||
/// </summary>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <returns></returns>
|
||||
Task<XStringDto> Get(
|
||||
string resource,
|
||||
string language = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified Translation of Specified Resource ...
|
||||
|
||||
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Extensions;
|
||||
@@ -126,6 +125,58 @@ namespace xStringService.Providers
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified Resource ...
|
||||
/// </summary>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XStringDto> Get(
|
||||
string resource,
|
||||
string language = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Normalize ...
|
||||
if (language.IsNullOrEmpty())
|
||||
{
|
||||
language = AppConfiguration.DefaultLanguage;
|
||||
}
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
if (resource.IsNullOrEmpty() || language.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Resource Exists ...
|
||||
var isExists = await IsResourceExists(
|
||||
resource: resource,
|
||||
language: language
|
||||
);
|
||||
if (!isExists)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var entity = await Repository
|
||||
.FindOneAsync(e =>
|
||||
e.ResourceTitle == resource &&
|
||||
e.Language.ToNormalString() == language.ToNormalString()
|
||||
);
|
||||
if (entity.IsNullOrDefault())
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
return entity
|
||||
.ToDynamicObject();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Specified Translation of Specified Resource ...
|
||||
/// </summary>
|
||||
@@ -417,19 +468,19 @@ namespace xStringService.Providers
|
||||
if (totalItemsCount > 0 &&
|
||||
filteredItemsCount > 0)
|
||||
{
|
||||
//
|
||||
// Apply Paging ...
|
||||
items = items.ApplyPaging(
|
||||
query.Page,
|
||||
query.PageSize
|
||||
);
|
||||
|
||||
//
|
||||
// Apply Sorting ...
|
||||
items = items.ApplySorting(
|
||||
query.SortBy,
|
||||
query.IsAscending
|
||||
);
|
||||
|
||||
//
|
||||
// Apply Paging ...
|
||||
items = items.ApplyPaging(
|
||||
query.Page,
|
||||
query.PageSize
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user