mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.0 KiB
97 lines
3.0 KiB
// ==========================================================================
|
|
// MongoRuleRepository_EventHandling.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using System.Threading.Tasks;
|
|
using MongoDB.Driver;
|
|
using Squidex.Domain.Apps.Events.Rules;
|
|
using Squidex.Domain.Apps.Events.Rules.Utils;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Infrastructure.CQRS.Events;
|
|
using Squidex.Infrastructure.Dispatching;
|
|
|
|
namespace Squidex.Domain.Apps.Read.MongoDb.Rules
|
|
{
|
|
public partial class MongoRuleRepository
|
|
{
|
|
public string Name
|
|
{
|
|
get { return GetType().Name; }
|
|
}
|
|
|
|
public string EventsFilter
|
|
{
|
|
get { return "^rule-"; }
|
|
}
|
|
|
|
public Task On(Envelope<IEvent> @event)
|
|
{
|
|
return this.DispatchActionAsync(@event.Payload, @event.Headers);
|
|
}
|
|
|
|
protected async Task On(RuleCreated @event, EnvelopeHeaders headers)
|
|
{
|
|
await EnsureRulesLoadedAsync();
|
|
|
|
await Collection.CreateAsync(@event, headers, w =>
|
|
{
|
|
w.Rule = RuleEventDispatcher.Create(@event);
|
|
|
|
inMemoryRules.GetOrAddNew(w.AppId).RemoveAll(x => x.Id == w.Id);
|
|
inMemoryRules.GetOrAddNew(w.AppId).Add(w);
|
|
});
|
|
}
|
|
|
|
protected async Task On(RuleUpdated @event, EnvelopeHeaders headers)
|
|
{
|
|
await EnsureRulesLoadedAsync();
|
|
|
|
await Collection.UpdateAsync(@event, headers, w =>
|
|
{
|
|
w.Rule.Apply(@event);
|
|
|
|
inMemoryRules.GetOrAddNew(w.AppId).RemoveAll(x => x.Id == w.Id);
|
|
inMemoryRules.GetOrAddNew(w.AppId).Add(w);
|
|
});
|
|
}
|
|
|
|
protected async Task On(RuleEnabled @event, EnvelopeHeaders headers)
|
|
{
|
|
await EnsureRulesLoadedAsync();
|
|
|
|
await Collection.UpdateAsync(@event, headers, w =>
|
|
{
|
|
w.Rule.Apply(@event);
|
|
|
|
inMemoryRules.GetOrAddNew(w.AppId).RemoveAll(x => x.Id == w.Id);
|
|
inMemoryRules.GetOrAddNew(w.AppId).Add(w);
|
|
});
|
|
}
|
|
|
|
protected async Task On(RuleDisabled @event, EnvelopeHeaders headers)
|
|
{
|
|
await EnsureRulesLoadedAsync();
|
|
|
|
await Collection.UpdateAsync(@event, headers, w =>
|
|
{
|
|
w.Rule.Apply(@event);
|
|
|
|
inMemoryRules.GetOrAddNew(w.AppId).RemoveAll(x => x.Id == w.Id);
|
|
inMemoryRules.GetOrAddNew(w.AppId).Add(w);
|
|
});
|
|
}
|
|
|
|
protected async Task On(RuleDeleted @event, EnvelopeHeaders headers)
|
|
{
|
|
await EnsureRulesLoadedAsync();
|
|
|
|
inMemoryRules.GetOrAddNew(@event.AppId.Id).RemoveAll(x => x.Id == @event.RuleId);
|
|
|
|
await Collection.DeleteManyAsync(x => x.Id == @event.RuleId);
|
|
}
|
|
}
|
|
}
|
|
|