last ...
This commit is contained in:
+245
-24
@@ -3,13 +3,18 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using DnsClient.Protocol;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Extensions;
|
||||
using xDataService.Configuration;
|
||||
using xDataService.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xModels.Dtos;
|
||||
using xPushService.Constants;
|
||||
using xStringService.Constants;
|
||||
using xStringService.Extensions;
|
||||
using xStringService.Hubs;
|
||||
using xStringService.Interfaces;
|
||||
using xStringService.Interfaces.Entities;
|
||||
using xStringService.Models.Dtos;
|
||||
@@ -21,6 +26,7 @@ namespace xStringService.Providers
|
||||
{
|
||||
//
|
||||
#region Props ...
|
||||
public IHubContext<XStringHub> Hub { get; }
|
||||
public IXStringRepository Repository { get; }
|
||||
public XAppConfiguration AppConfiguration { get; }
|
||||
public XDataServiceConfiguration DataConfiguration { get; }
|
||||
@@ -31,13 +37,15 @@ namespace xStringService.Providers
|
||||
public XStringProvider(
|
||||
IXStringRepository repository,
|
||||
XAppConfiguration appConfiguration,
|
||||
XDataServiceConfiguration dataConfiguration
|
||||
XDataServiceConfiguration dataConfiguration,
|
||||
IHubContext<XStringHub> hub = null
|
||||
)
|
||||
{
|
||||
//
|
||||
Repository = repository;
|
||||
AppConfiguration = appConfiguration;
|
||||
DataConfiguration = dataConfiguration;
|
||||
Hub = hub;
|
||||
}
|
||||
#endregion
|
||||
|
||||
@@ -304,7 +312,7 @@ namespace xStringService.Providers
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XQueryResult<string>> QueryResourceIds(XQuery query)
|
||||
public XQueryResult<string> QueryResourceIds(XQuery query)
|
||||
{
|
||||
//
|
||||
// Normalize ...
|
||||
@@ -410,7 +418,7 @@ namespace xStringService.Providers
|
||||
TotalPages = entityResult.TotalPages,
|
||||
TotalItems = entityResult.TotalItems,
|
||||
TotalFilteredPages = entityResult.TotalFilteredPages,
|
||||
TotalFilteredItems = entityResult.TotalFilteredItems,
|
||||
TotalFilteredItems = entityResult.TotalFilteredItems,
|
||||
Items = entityResult.Items.Select(i => i.ToXStringDto()),
|
||||
};
|
||||
|
||||
@@ -562,11 +570,13 @@ namespace xStringService.Providers
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> Add(
|
||||
string resource,
|
||||
string value,
|
||||
string language = null
|
||||
string language = null,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -607,8 +617,17 @@ namespace xStringService.Providers
|
||||
TranslatedValue = value,
|
||||
ResourceTitle = resource,
|
||||
};
|
||||
var added = await Repository.AddAsync(entity);
|
||||
result = !added.IsNullOrDefault();
|
||||
entity = await Repository.AddAsync(entity);
|
||||
result = !entity.IsNullOrDefault();
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Add.GetStringValue(),
|
||||
entityId: entity.Id,
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
@@ -620,11 +639,13 @@ namespace xStringService.Providers
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> Update(
|
||||
string resource,
|
||||
string value,
|
||||
string language = null
|
||||
string language = null,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -674,6 +695,15 @@ namespace xStringService.Providers
|
||||
entity.TranslatedValue = value;
|
||||
entity = await Repository.UpdateAsync(entity.Id, entity);
|
||||
result = !entity.IsNullOrDefault();
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Update.GetStringValue(),
|
||||
entityId: entity.Id,
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
@@ -685,11 +715,13 @@ namespace xStringService.Providers
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> AddOrUpdate(
|
||||
string resource,
|
||||
string value,
|
||||
string language = null
|
||||
string language = null,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -738,8 +770,12 @@ namespace xStringService.Providers
|
||||
/// Add Specified Resource ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XStringDto> AddEntity(XStringDto item)
|
||||
public async Task<XStringDto> AddEntity(
|
||||
XStringDto item,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
@@ -787,6 +823,13 @@ namespace xStringService.Providers
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Add.GetStringValue(),
|
||||
entityId: result.Id,
|
||||
connectionId: connectionId
|
||||
);
|
||||
|
||||
//
|
||||
return result.ToXStringDto();
|
||||
}
|
||||
@@ -795,8 +838,12 @@ namespace xStringService.Providers
|
||||
/// Update Specified Entity ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XStringDto> UpdateEntity(XStringDto item)
|
||||
public async Task<XStringDto> UpdateEntity(
|
||||
XStringDto item,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
@@ -852,6 +899,11 @@ namespace xStringService.Providers
|
||||
|
||||
//
|
||||
XStringDto result = entity.ToXStringDto();
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Update.GetStringValue(),
|
||||
entityId: entity.Id,
|
||||
connectionId: connectionId
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
@@ -861,8 +913,12 @@ namespace xStringService.Providers
|
||||
/// Add Or Update Specified Entitiy ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XStringDto> AddOrUpdateEntity(XStringDto item)
|
||||
public async Task<XStringDto> AddOrUpdateEntity(
|
||||
XStringDto item,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
@@ -897,11 +953,11 @@ namespace xStringService.Providers
|
||||
XStringDto result = null;
|
||||
if (!isExists)
|
||||
{
|
||||
result = await AddEntity(item);
|
||||
result = await AddEntity(item, connectionId);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await UpdateEntity(item);
|
||||
result = await UpdateEntity(item, connectionId);
|
||||
}
|
||||
if (result.IsNullOrDefault())
|
||||
{
|
||||
@@ -917,10 +973,12 @@ namespace xStringService.Providers
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XStringDto> AddByLocaleResource(
|
||||
XLocaleResourceDto item,
|
||||
string resource
|
||||
string resource,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -954,7 +1012,7 @@ namespace xStringService.Providers
|
||||
ResourceTitle = resource,
|
||||
TranslatedValue = item.Value
|
||||
};
|
||||
result = await AddEntity(result);
|
||||
result = await AddEntity(result, connectionId);
|
||||
|
||||
//
|
||||
return result;
|
||||
@@ -965,10 +1023,12 @@ namespace xStringService.Providers
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XStringDto> UpdateByLocaleResource(
|
||||
XLocaleResourceDto item,
|
||||
string resource
|
||||
string resource,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -1015,6 +1075,13 @@ namespace xStringService.Providers
|
||||
|
||||
//
|
||||
XStringDto result = entity.ToXStringDto();
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Update.GetStringValue(),
|
||||
entityId: entity.Id,
|
||||
connectionId: connectionId
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1023,10 +1090,12 @@ namespace xStringService.Providers
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XStringDto> AddOrUpdateByLocaleResource(
|
||||
XLocaleResourceDto item,
|
||||
string resource
|
||||
string resource,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -1079,8 +1148,12 @@ namespace xStringService.Providers
|
||||
/// Add Or Update all XResourceDto(s) Locales ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XStringDto>> AddByResource(XResourceDto item)
|
||||
public async Task<IEnumerable<XStringDto>> AddByResource(
|
||||
XResourceDto item,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
@@ -1109,8 +1182,12 @@ namespace xStringService.Providers
|
||||
/// Remove all Specified Language's Resources ...
|
||||
/// </summary>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task RemoveLanguageResources(string language)
|
||||
public async Task RemoveLanguageResources(
|
||||
string language,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
@@ -1123,15 +1200,37 @@ namespace xStringService.Providers
|
||||
//
|
||||
var entities = await Repository
|
||||
.FindManyAsync(e => e.Language.ToNormalString() == language.ToNormalString());
|
||||
await Repository.RemoveRangeAsync(entities);
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
await Repository.RemoveRangeAsync(entities);
|
||||
|
||||
//
|
||||
var entitieIds = entities.Select(e => e.Id);
|
||||
await SendCustomPush(
|
||||
action: XStringHubAction.ItemsDeleted.GetStringValue(),
|
||||
payLoad: entitieIds.ToJSON(),
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all Specified Resource Ids instances ...
|
||||
/// </summary>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XStringDto>> RemoveResource(string resource)
|
||||
public async Task<IEnumerable<XStringDto>> RemoveResource(
|
||||
string resource,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
@@ -1153,6 +1252,18 @@ namespace xStringService.Providers
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
if (result.HasChild())
|
||||
{
|
||||
//
|
||||
var ids = result.Select(i => i.Id);
|
||||
await SendCustomPush(
|
||||
action: XStringHubAction.ItemsDeleted.GetStringValue(),
|
||||
payLoad: ids.ToJSON(),
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
@@ -1162,10 +1273,12 @@ namespace xStringService.Providers
|
||||
/// </summary>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> Remove(
|
||||
string resource,
|
||||
string language = null
|
||||
string language = null,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -1215,6 +1328,15 @@ namespace xStringService.Providers
|
||||
// Delete ...
|
||||
entity = await Repository.RemoveAsync(entity.Id);
|
||||
result = !entity.IsNullOrDefault();
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
await SendPush(
|
||||
action: XBaseEntityHubAction.Delete.GetStringValue(),
|
||||
entityId: entity.Id,
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
@@ -1224,8 +1346,12 @@ namespace xStringService.Providers
|
||||
/// Remove all Resource of Specified XResourceDto ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task RemoveByResource(XResourceDto item)
|
||||
public async Task RemoveByResource(
|
||||
XResourceDto item,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
bool isValid =
|
||||
@@ -1237,7 +1363,7 @@ namespace xStringService.Providers
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
foreach (var locale in item.Locales)
|
||||
{
|
||||
//
|
||||
@@ -1248,5 +1374,100 @@ namespace xStringService.Providers
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Hub Actions ...
|
||||
/// <summary>
|
||||
/// Send Specified Hub Action Notifications ...
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="entityId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SendPush(
|
||||
string action,
|
||||
int entityId,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var hubActions = new List<string>
|
||||
{
|
||||
XBaseEntityHubAction.Add.GetStringValue(),
|
||||
XBaseEntityHubAction.Delete.GetStringValue(),
|
||||
XBaseEntityHubAction.Update.GetStringValue(),
|
||||
};
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
var isValid =
|
||||
entityId > 0 &&
|
||||
!Hub.IsNull() &&
|
||||
!action.IsNullOrEmpty() &&
|
||||
hubActions.Contains(action);
|
||||
if (!isValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve and Validate Entity ...
|
||||
XString entity = null;
|
||||
try
|
||||
{
|
||||
entity = await Repository.GetAsync(entityId);
|
||||
}
|
||||
catch { }
|
||||
isValid = !entity.IsNullOrDefault();
|
||||
if (!isValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
var clients = Hub.Clients.All;
|
||||
if (!connectionId.IsNullOrEmpty())
|
||||
{
|
||||
clients = Hub.Clients.AllExcept(connectionId);
|
||||
}
|
||||
await clients.SendAsync(action, entity, connectionId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send Custom Push Message ...
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="payLoad"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SendCustomPush(
|
||||
string action,
|
||||
string payLoad,
|
||||
string connectionId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var customActions = new List<string>
|
||||
{
|
||||
XStringHubAction.ItemsDeleted.GetStringValue(),
|
||||
};
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
var isValid = !action.IsNullOrEmpty() &&
|
||||
customActions.Contains(action);
|
||||
if (!isValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
var clients = Hub.Clients.All;
|
||||
if (!connectionId.IsNullOrEmpty())
|
||||
{
|
||||
clients = Hub.Clients.AllExcept(connectionId);
|
||||
}
|
||||
await clients.SendAsync(action, payLoad, connectionId);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user