first draft of message handler

This commit is contained in:
2020-10-06 16:00:27 +02:00
parent cced92d89b
commit 813b9c259b
19 changed files with 395 additions and 14 deletions
+90
View File
@@ -0,0 +1,90 @@
using Raven.Client.Documents;
using Raven.Client.Documents.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using zero.Core;
using zero.Core.Api;
using zero.Core.Entities;
using zero.Core.Entities.Messages;
using zero.Core.Extensions;
using zero.Core.Messages;
namespace zero.Debug.Sync
{
public abstract class BlueprintHandler<T> : IMessageHandler<EntitySavedMessage<T>> where T : IZeroEntity
{
public virtual async Task Handle(EntitySavedMessage<T> message)
{
await CreateBlueprints(message.Session, message.Model, message.IsCreate);
}
protected virtual Task OnBlueprintCreateAsync(T blueprint, T model)
{
return Task.CompletedTask;
}
protected virtual void OnBlueprintCreate(T blueprint, T model)
{
return;
}
protected virtual async Task CreateBlueprints(IAsyncDocumentSession session, T model, bool isCreate = false)
{
string sharedId = Constants.Database.SharedAppId;
if (model.AppId != sharedId)
{
return;
}
IList<T> inheritedModels = new List<T>();
IList<IApplication> apps = await session.Query<IApplication>().Where(x => x.Id != sharedId).ToListAsync();
if (!isCreate)
{
string id = model.Id;
inheritedModels = await session.Query<T>().Where(x => x.Blueprint != null && x.Blueprint.Id == id).ToListAsync();
}
foreach (IApplication app in apps)
{
bool exists = true;
T inheritedModel = inheritedModels.FirstOrDefault(x => x.AppId == app.Id);
// the model does not yet exist in the app, so we need to create it
if (inheritedModel == null)
{
exists = false;
inheritedModel = model.Clone();
inheritedModel.Id = null;
}
inheritedModel.AppId = app.Id;
inheritedModel.LastModifiedById = model.LastModifiedById;
inheritedModel.LastModifiedDate = model.LastModifiedDate;
inheritedModel.Blueprint = new BlueprintConfiguration()
{
Id = model.Id
};
// we need to override allowed properties in the inherited model
if (exists)
{
inheritedModel.Name = model.Name;
inheritedModel.Alias = Safenames.Alias(model.Name);
inheritedModel.IsActive = model.IsActive;
inheritedModel.Sort = model.Sort;
await OnBlueprintCreateAsync(model, inheritedModel);
OnBlueprintCreate(model, inheritedModel);
}
await session.StoreAsync(inheritedModel);
}
}
}
}
@@ -0,0 +1,14 @@
using Raven.Client.Documents.Session;
using zero.Core.Entities;
namespace zero.Debug.Sync
{
public class CountryBlueprintHandler : BlueprintHandler<ICountry>
{
protected override void OnBlueprintCreate(ICountry blueprint, ICountry model)
{
model.Code = blueprint.Code;
model.IsPreferred = blueprint.IsPreferred;
}
}
}
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Linq;
using zero.Commerce.Entities;
using zero.Core.Api;
namespace zero.Debug.Sync
{
public class PropertyBlueprintHandler : BlueprintHandler<IProperty>
{
protected override void OnBlueprintCreate(IProperty blueprint, IProperty model)
{
model.Description = blueprint.Description;
model.Limit = blueprint.Limit;
model.ForFilter = blueprint.ForFilter;
model.AllowText = blueprint.AllowText;
model.IconSet = blueprint.IconSet;
model.Type = blueprint.Type;
model.SortingMethod = blueprint.SortingMethod;
List<IPropertyValue> values = blueprint.Values.ToList();
string[] valueIds = values.Select(x => x.Id).ToArray();
foreach (IPropertyValue value in values)
{
string desyncKey = $"Values[{value.Id}].";
string[] desynced = model.Blueprint.Desync.Where(x => x.StartsWith(desyncKey)).ToArray();
IPropertyValue match = model.Values.FirstOrDefault(x => x.Id == value.Id);
if (match != null)
{
if (desynced.Contains(desyncKey + nameof(value.Value)))
{
value.Value = match.Value;
value.Alias = Safenames.Alias(value.Value);
}
if (value is ColorPropertyValue && desynced.Contains(desyncKey + "Hex"))
{
((ColorPropertyValue)value).Hex = ((ColorPropertyValue)match).Hex;
}
}
}
foreach (IPropertyValue value in model.Values.Where(x => !valueIds.Contains(x.Id)))
{
values.Add(value);
}
model.Values = values;
}
}
}