1183 lines
33 KiB
C#
1183 lines
33 KiB
C#
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;
|
|
using xDataService.Configuration;
|
|
using xDataService.Extensions;
|
|
using xExceptions.Constants;
|
|
using xModels.Dtos;
|
|
using xStringService.Extensions;
|
|
using xStringService.Interfaces;
|
|
using xStringService.Interfaces.Entities;
|
|
using xStringService.Models.Dtos;
|
|
using xStringService.Models.Entities;
|
|
|
|
namespace xStringService.Providers
|
|
{
|
|
public class XStringProvider : IXStringProvider
|
|
{
|
|
//
|
|
#region Props ...
|
|
public IXStringRepository Repository { get; }
|
|
public XAppConfiguration AppConfiguration { get; }
|
|
public XDataServiceConfiguration DataConfiguration { get; }
|
|
#endregion
|
|
|
|
//
|
|
#region Constructor ...
|
|
public XStringProvider(
|
|
IXStringRepository repository,
|
|
XAppConfiguration appConfiguration,
|
|
XDataServiceConfiguration dataConfiguration
|
|
)
|
|
{
|
|
//
|
|
Repository = repository;
|
|
AppConfiguration = appConfiguration;
|
|
DataConfiguration = dataConfiguration;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Retrievers ...
|
|
/// <summary>
|
|
/// Retrieve all Exists Languages ...
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<string>> GetLanguages()
|
|
{
|
|
//
|
|
var result = (await Repository.GetAllAsync())
|
|
.AsQueryable()
|
|
.Select(e => e.Language)
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
//
|
|
// Check Configuration Default Language Exists ...
|
|
var hasDefaultLanguage = !AppConfiguration.DefaultLanguage.IsNullOrEmpty();
|
|
if (hasDefaultLanguage)
|
|
{
|
|
//
|
|
// Find Default Language on Result ...
|
|
hasDefaultLanguage = result.Any(e => e.ToNormalString() == AppConfiguration.DefaultLanguage.ToNormalString());
|
|
if (!hasDefaultLanguage)
|
|
{
|
|
result.Add(AppConfiguration.DefaultLanguage);
|
|
}
|
|
}
|
|
|
|
//
|
|
result = result
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Specified Resource Exists or not ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> IsResourceExists(
|
|
string resource,
|
|
string language = null
|
|
)
|
|
{
|
|
//
|
|
var result = false;
|
|
|
|
//
|
|
// Validate ...
|
|
result = !resource.IsNullOrEmpty();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
XStringDto entity = null;
|
|
if (language.IsNullOrEmpty())
|
|
{
|
|
//
|
|
entity = await Repository
|
|
.FindOneAsync(e => e.ResourceTitle == resource)
|
|
.ToDynamicObject();
|
|
}
|
|
else
|
|
{
|
|
//
|
|
entity = await Repository
|
|
.FindOneAsync(e => e.ResourceTitle == resource &&
|
|
e.Language.ToNormalString() == language.ToNormalString()
|
|
)
|
|
.ToDynamicObject();
|
|
}
|
|
result = !entity.IsNullOrDefault();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Specified Translation of Specified Resource ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> GetResourceValue(
|
|
string resource,
|
|
string language = null
|
|
)
|
|
{
|
|
//
|
|
var result = "";
|
|
|
|
//
|
|
// Normalize ...
|
|
if (language.IsNullOrEmpty())
|
|
{
|
|
language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
|
|
//
|
|
// Validate ...
|
|
var isValid = !resource.IsNullOrEmpty() &&
|
|
!language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Langauage Exists ...
|
|
var languages = await GetLanguages();
|
|
isValid = languages.Any(l => l.ToNormalString() == language.ToNormalString());
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Resource Exists ...
|
|
isValid = await IsResourceExists(resource);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var entity = await Repository.FindOneAsync(e =>
|
|
e.ResourceTitle == resource &&
|
|
e.Language.ToNormalString() == language.ToNormalString()
|
|
);
|
|
isValid = !entity.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
result = entity.TranslatedValue;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve an Specified Resource Items ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XStringDto>> GetResources(string resource)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = await IsResourceExists(resource);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var result = (await Repository
|
|
.FindManyAsync(e => e.ResourceTitle == resource))
|
|
.ToXStringDtos();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Specified Resource Items as XResourceDto Presentation ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <returns></returns>
|
|
public async Task<XResourceDto> GetResource(string resource)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isExists = await IsResourceExists(resource);
|
|
if (!isExists)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve String Dto (s) ...
|
|
var resources = await GetResources(resource);
|
|
if (!resource.HasChild())
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var result = new XResourceDto
|
|
{
|
|
Resource = resource,
|
|
Locales = resources.ToXLocaleResourceDtos()
|
|
};
|
|
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Query ...
|
|
/// <summary>
|
|
/// Query Resource IDs ...
|
|
/// </summary>
|
|
/// <param name="query"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<string>> QueryResourceIds(XQuery query)
|
|
{
|
|
//
|
|
// Normalize ...
|
|
query = query.NormalizeQuery(DataConfiguration);
|
|
|
|
//
|
|
// Read Resource IDs ...
|
|
var items = Repository
|
|
.AsQueryable()
|
|
.Select(e => e.ResourceTitle)
|
|
.Distinct();
|
|
|
|
//
|
|
var totalItemsCount = items.Count();
|
|
|
|
//
|
|
// Apply Filter ...
|
|
if (totalItemsCount > 0 &&
|
|
!query.Filter.IsNullOrEmpty())
|
|
{
|
|
//
|
|
items = items
|
|
.Where(e => e.ToNormalString().Contains(query.Filter.ToNormalString()));
|
|
}
|
|
|
|
//
|
|
var filteredItemsCount = items.Count();
|
|
var totalPagesCount = query.CountPages(totalItemsCount);
|
|
var filteredPagesCount = query.CountPages(filteredItemsCount);
|
|
|
|
//
|
|
// Apply Paging / Sorting ...
|
|
if (totalItemsCount > 0 &&
|
|
filteredItemsCount > 0)
|
|
{
|
|
//
|
|
items = items.ApplyPaging(
|
|
query.Page,
|
|
query.PageSize
|
|
);
|
|
}
|
|
|
|
//
|
|
// Prepare result ...
|
|
var result = new XQueryResult<string>
|
|
{
|
|
Page = query.Page,
|
|
PageSize = query.PageSize,
|
|
TotalPages = totalPagesCount,
|
|
TotalItems = totalItemsCount,
|
|
TotalFilteredPages = filteredPagesCount,
|
|
TotalFilteredItems = filteredItemsCount
|
|
};
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Query Specified Language Resources ...
|
|
/// </summary>
|
|
/// <param name="query"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XStringDto>> QueryLanguageResources(
|
|
XQuery query,
|
|
string language = null //
|
|
)
|
|
{
|
|
//
|
|
// Normalize ...
|
|
query = query.NormalizeQuery(DataConfiguration);
|
|
|
|
//
|
|
// Validate ...
|
|
if (query.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
XQueryResult<XStringDto> result = null;
|
|
if (language.IsNullOrEmpty())
|
|
{
|
|
//
|
|
result = (await Repository.QueryAsync(query))
|
|
.ToDynamicObject();
|
|
}
|
|
else
|
|
{
|
|
//
|
|
result = (await Repository.ConditionalQueryAsync(
|
|
e => e.Language.ToNormalString() == language.ToNormalString(),
|
|
query
|
|
))
|
|
.ToDynamicObject();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Query Locale Resources ...
|
|
/// </summary>
|
|
/// <param name="query"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XLocaleResourceDto>> QueryLocaleResources(
|
|
XQuery query,
|
|
string language = null //
|
|
)
|
|
{
|
|
//
|
|
// Normalize ...
|
|
query.NormalizeQuery(DataConfiguration);
|
|
|
|
//
|
|
// Validate ...
|
|
if (query.IsNull())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
IEnumerable<XLocaleResourceDto> items = null;
|
|
if (!language.IsNullOrEmpty())
|
|
{
|
|
items = (await Repository
|
|
.FindManyAsync(e => e.Language.ToNormalString() == language.ToNormalString()))
|
|
.ToXLocaleResourceDtos();
|
|
}
|
|
else
|
|
{
|
|
items = (await Repository.GetAllAsync())
|
|
.ToXLocaleResourceDtos();
|
|
}
|
|
|
|
//
|
|
var totalItemsCount = items.Count();
|
|
|
|
//
|
|
// Apply Filter ...
|
|
if (totalItemsCount > 0 &&
|
|
!query.Filter.IsNullOrEmpty())
|
|
{
|
|
//
|
|
items = items.ApplyFilter(query.Filter);
|
|
}
|
|
|
|
//
|
|
var filteredItemsCount = items.Count();
|
|
var totalPagesCount = query.CountPages(totalItemsCount);
|
|
var filteredPagesCount = query.CountPages(filteredItemsCount);
|
|
|
|
//
|
|
// Apply Paging/Sorting ...
|
|
if (totalItemsCount > 0 &&
|
|
filteredItemsCount > 0)
|
|
{
|
|
//
|
|
// Apply Paging ...
|
|
items = items.ApplyPaging(
|
|
query.Page,
|
|
query.PageSize
|
|
);
|
|
|
|
//
|
|
// Apply Sorting ...
|
|
items = items.ApplySorting(
|
|
query.SortBy,
|
|
query.IsAscending
|
|
);
|
|
}
|
|
|
|
//
|
|
// Prepare Result ...
|
|
var result = new XQueryResult<XLocaleResourceDto>
|
|
{
|
|
Page = query.Page,
|
|
PageSize = query.PageSize,
|
|
TotalPages = totalPagesCount,
|
|
TotalItems = totalItemsCount,
|
|
TotalFilteredPages = filteredPagesCount,
|
|
TotalFilteredItems = filteredItemsCount
|
|
};
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Conditional Query Resources ...
|
|
/// </summary>
|
|
/// <param name="query"></param>
|
|
/// <param name="condition"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<XQueryResult<XStringDto>> ConditionalQueryResources(
|
|
XQuery query,
|
|
Expression<Func<XString, bool>> condition,
|
|
string language = null
|
|
)
|
|
{
|
|
//
|
|
// Normalize ...
|
|
query = query.NormalizeQuery(DataConfiguration);
|
|
|
|
//
|
|
if (!language.IsNullOrEmpty())
|
|
{
|
|
//
|
|
// Renew and Update Condition by Language Checking ...
|
|
var compiledCondition = condition.Compile();
|
|
condition =
|
|
e => compiledCondition(e) &&
|
|
e.Language.ToNormalString() == language.ToNormalString();
|
|
}
|
|
|
|
//
|
|
var result = (await Repository.ConditionalQueryAsync(condition, query))
|
|
.ToDynamicObject();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Add/Update Actions ...
|
|
/// <summary>
|
|
/// Add Specified Resource ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Add(
|
|
string resource,
|
|
string value,
|
|
string language = null
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Normalize ...
|
|
if (language.IsNullOrEmpty())
|
|
{
|
|
language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
|
|
//
|
|
// Validate ...
|
|
result =
|
|
!value.IsNullOrEmpty() &&
|
|
!resource.IsNullOrEmpty() &&
|
|
!language.IsNullOrEmpty();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check Exists ...
|
|
bool isExists = await IsResourceExists(resource, language);
|
|
result = !isExists;
|
|
if (!result)
|
|
{
|
|
XException.Duplicate.Throw();
|
|
}
|
|
|
|
//
|
|
// Prepare Entity to Add ...
|
|
XString entity = new XString
|
|
{
|
|
Language = language,
|
|
TranslatedValue = value,
|
|
ResourceTitle = resource,
|
|
};
|
|
var added = await Repository.AddAsync(entity);
|
|
result = !added.IsNullOrDefault();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update Resource ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Update(
|
|
string resource,
|
|
string value,
|
|
string language = null
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Normalize ...
|
|
if (language.IsNullOrEmpty())
|
|
{
|
|
language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
|
|
//
|
|
// Validate ...
|
|
result =
|
|
!value.IsNullOrEmpty() &&
|
|
!language.IsNullOrEmpty() &&
|
|
!resource.IsNullOrEmpty();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check Value Must Exists ...
|
|
result = await IsResourceExists(
|
|
resource: resource,
|
|
language: language
|
|
);
|
|
if (!result)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var entity = await Repository.FindOneAsync(e =>
|
|
e.ResourceTitle == resource &&
|
|
e.Language.ToNormalString() == language.ToNormalString()
|
|
);
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Update Entity ...
|
|
entity.TranslatedValue = value;
|
|
entity = await Repository.UpdateAsync(entity.Id, entity);
|
|
result = !entity.IsNullOrDefault();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Or Update Resource ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> AddOrUpdate(
|
|
string resource,
|
|
string value,
|
|
string language = null
|
|
)
|
|
{
|
|
//
|
|
var result = false;
|
|
|
|
//
|
|
// Normalize ...
|
|
if (language.IsNullOrEmpty())
|
|
{
|
|
language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
|
|
//
|
|
// Check Existsance ...
|
|
bool isExists = await IsResourceExists(
|
|
resource: resource,
|
|
language: language
|
|
);
|
|
|
|
//
|
|
// Act Based on Existance ...
|
|
if (isExists)
|
|
{
|
|
//
|
|
result = await Update(
|
|
value: value,
|
|
resource: resource,
|
|
language: language
|
|
);
|
|
}
|
|
else
|
|
{
|
|
//
|
|
result = await Add(
|
|
value: value,
|
|
resource: resource,
|
|
language: language
|
|
);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Specified Resource ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public async Task<XStringDto> AddEntity(XStringDto item)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid = !item.IsNull() &&
|
|
!item.ResourceTitle.IsNullOrEmpty() &&
|
|
!item.TranslatedValue.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Normalize ...
|
|
if (item.Language.IsNullOrEmpty())
|
|
{
|
|
item.Language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
isValid = !item.Language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Resource Doesn't Exists ...
|
|
isValid = await IsResourceExists(
|
|
resource: item.ResourceTitle,
|
|
language: item.Language
|
|
);
|
|
if (isValid)
|
|
{
|
|
XException.Duplicate.Throw();
|
|
}
|
|
|
|
//
|
|
var result = new XString
|
|
{
|
|
Language = item.Language,
|
|
ResourceTitle = item.ResourceTitle,
|
|
TranslatedValue = item.TranslatedValue
|
|
};
|
|
result = await Repository.AddAsync(result);
|
|
if (result.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
return result.ToDynamicObject();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update Specified Entity ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public async Task<XStringDto> UpdateEntity(XStringDto item)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid = !item.IsNull() &&
|
|
!item.ResourceTitle.IsNullOrEmpty() &&
|
|
!item.TranslatedValue.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Normalize ...
|
|
if (item.Language.IsNullOrEmpty())
|
|
{
|
|
item.Language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
isValid = !item.Language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Resource Exists ...
|
|
isValid = await IsResourceExists(
|
|
resource: item.ResourceTitle,
|
|
language: item.Language
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var entity = await Repository.FindOneAsync(e =>
|
|
e.ResourceTitle == item.ResourceTitle &&
|
|
e.Language.ToNormalString() == item.Language.ToNormalString()
|
|
);
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Update Entity ...
|
|
entity.TranslatedValue = item.TranslatedValue;
|
|
entity = await Repository.UpdateAsync(entity.Id, entity);
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
XStringDto result = entity.ToDynamicObject();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Or Update Specified Entitiy ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public async Task<XStringDto> AddOrUpdateEntity(XStringDto item)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid = !item.IsNull() &&
|
|
!item.ResourceTitle.IsNullOrEmpty() &&
|
|
!item.TranslatedValue.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Normalize ...
|
|
if (item.Language.IsNullOrEmpty())
|
|
{
|
|
item.Language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
isValid = !item.Language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Resource Exists ...
|
|
var isExists = await IsResourceExists(
|
|
resource: item.ResourceTitle,
|
|
language: item.Language
|
|
);
|
|
|
|
//
|
|
XStringDto result = null;
|
|
if (!isExists)
|
|
{
|
|
result = await AddEntity(item);
|
|
}
|
|
else
|
|
{
|
|
result = await UpdateEntity(item);
|
|
}
|
|
if (result.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Specified Locale Resource ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="resource"></param>
|
|
/// <returns></returns>
|
|
public async Task<XStringDto> AddByLocaleResource(
|
|
XLocaleResourceDto item,
|
|
string resource
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid =
|
|
!item.IsNull() &&
|
|
!resource.IsNullOrEmpty() &&
|
|
!item.Value.IsNullOrEmpty() &&
|
|
!item.Language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Specified Resource / Language Exists ...
|
|
isValid = await IsResourceExists(
|
|
resource: resource,
|
|
language: item.Language
|
|
);
|
|
if (isValid)
|
|
{
|
|
XException.Duplicate.Throw();
|
|
}
|
|
|
|
//
|
|
// Prepare Item to Add ...
|
|
var result = new XStringDto
|
|
{
|
|
Language = item.Language,
|
|
ResourceTitle = resource,
|
|
TranslatedValue = item.Value
|
|
};
|
|
result = await AddEntity(result);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update Specified Locale Resource ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="resource"></param>
|
|
/// <returns></returns>
|
|
public async Task<XStringDto> UpdateByLocaleResource(
|
|
XLocaleResourceDto item,
|
|
string resource
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid =
|
|
!item.IsNull() &&
|
|
!resource.IsNullOrEmpty() &&
|
|
!item.Value.IsNullOrEmpty() &&
|
|
!item.Language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Specified Resource / Language Exists ...
|
|
isValid = await IsResourceExists(
|
|
resource: resource,
|
|
language: item.Language
|
|
);
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var entity = await Repository.FindOneAsync(e =>
|
|
e.ResourceTitle == resource &&
|
|
e.Language.ToNormalString() == item.Language.ToNormalString()
|
|
);
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
// Update ...
|
|
entity.TranslatedValue = item.Value;
|
|
entity = await Repository.UpdateAsync(entity.Id, entity);
|
|
if (entity.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
XStringDto result = entity.ToDynamicObject();
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Or Update Resource By Locale ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="resource"></param>
|
|
/// <returns></returns>
|
|
public async Task<XStringDto> AddOrUpdateByLocaleResource(
|
|
XLocaleResourceDto item,
|
|
string resource
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid =
|
|
!item.IsNull() &&
|
|
!resource.IsNullOrEmpty() &&
|
|
!item.Value.IsNullOrEmpty() &&
|
|
!item.Language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Check Specified Resource / Language Exists ...
|
|
isValid = await IsResourceExists(
|
|
resource: resource,
|
|
language: item.Language
|
|
);
|
|
|
|
//
|
|
XStringDto result = null;
|
|
if (!isValid)
|
|
{
|
|
//
|
|
result = await AddByLocaleResource(
|
|
item: item,
|
|
resource: resource
|
|
);
|
|
}
|
|
else
|
|
{
|
|
//
|
|
result = await UpdateByLocaleResource(
|
|
item: item,
|
|
resource: resource
|
|
);
|
|
}
|
|
if (result.IsNullOrDefault())
|
|
{
|
|
XException.ActionFailed.Throw();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Or Update all XResourceDto(s) Locales ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XStringDto>> AddByResource(XResourceDto item)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid =
|
|
!item.IsNull() &&
|
|
item.Locales.HasChild() &&
|
|
!item.Resource.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await Task
|
|
.WhenAll(item.Locales
|
|
.Select(async e => await AddOrUpdateByLocaleResource(e, item.Resource)));
|
|
|
|
//
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Remove Actions ...
|
|
/// <summary>
|
|
/// Remove all Specified Language's Resources ...
|
|
/// </summary>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task RemoveLanguageResources(string language)
|
|
{
|
|
//
|
|
// Validate ...
|
|
bool isValid = !language.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
var entities = await Repository
|
|
.FindManyAsync(e => e.Language.ToNormalString() == language.ToNormalString());
|
|
await Repository.RemoveRangeAsync(entities);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove all Specified Resource Ids instances ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<XStringDto>> RemoveResource(string resource)
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (resource.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var result = new List<XStringDto>();
|
|
var entities = await Repository.FindManyAsync(e => e.ResourceTitle == resource);
|
|
foreach (var entity in entities)
|
|
{
|
|
//
|
|
var removed = await Repository.RemoveAsync(entity);
|
|
if (!removed.IsNullOrDefault())
|
|
{
|
|
result.Add(removed.ToXStringDto());
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove Specified Resource ...
|
|
/// </summary>
|
|
/// <param name="resource"></param>
|
|
/// <param name="language"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Remove(
|
|
string resource,
|
|
string language = null
|
|
)
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
// Normalize ...
|
|
if (language.IsNullOrEmpty())
|
|
{
|
|
language = AppConfiguration.DefaultLanguage;
|
|
}
|
|
|
|
//
|
|
// Validate ...
|
|
result =
|
|
!language.IsNullOrEmpty() &&
|
|
!resource.IsNullOrEmpty();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Check Resource Exists ...
|
|
result = await IsResourceExists(
|
|
language: language,
|
|
resource: resource
|
|
);
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Retrieve Entity ...
|
|
var entity = await Repository.FindOneAsync(e =>
|
|
e.ResourceTitle == resource &&
|
|
e.Language.ToNormalString() == language.ToNormalString()
|
|
);
|
|
result = !entity.IsNullOrDefault();
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Delete ...
|
|
entity = await Repository.RemoveAsync(entity.Id);
|
|
result = !entity.IsNullOrDefault();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove all Resource of Specified XResourceDto ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public async Task RemoveByResource(XResourceDto item)
|
|
{
|
|
//
|
|
bool isValid =
|
|
!item.IsNullOrDefault() &&
|
|
item.Locales.HasChild() &&
|
|
!item.Resource.IsNullOrEmpty();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
foreach (var locale in item.Locales)
|
|
{
|
|
//
|
|
await Remove(
|
|
resource: item.Resource,
|
|
language: locale.Language
|
|
);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |