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.
135 lines
4.0 KiB
135 lines
4.0 KiB
// ==========================================================================
|
|
// AppStateGrain.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Squidex.Domain.Apps.Core.Schemas;
|
|
using Squidex.Domain.Apps.Events.Apps;
|
|
using Squidex.Domain.Apps.Read.Apps;
|
|
using Squidex.Domain.Apps.Read.Rules;
|
|
using Squidex.Domain.Apps.Read.Schemas;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Infrastructure.CQRS.Events;
|
|
using Squidex.Infrastructure.States;
|
|
using Squidex.Infrastructure.Tasks;
|
|
|
|
namespace Squidex.Domain.Apps.Read.State.Orleans.Grains
|
|
{
|
|
public class AppStateGrain : StatefulObject<AppStateGrainState>
|
|
{
|
|
private readonly FieldRegistry fieldRegistry;
|
|
private readonly SingleThreadedDispatcher dispatcher = new SingleThreadedDispatcher();
|
|
private Exception exception;
|
|
|
|
public AppStateGrain(FieldRegistry fieldRegistry)
|
|
{
|
|
Guard.NotNull(fieldRegistry, nameof(fieldRegistry));
|
|
|
|
this.fieldRegistry = fieldRegistry;
|
|
}
|
|
|
|
public override async Task ReadStateAsync()
|
|
{
|
|
try
|
|
{
|
|
await base.ReadStateAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
exception = ex;
|
|
|
|
State = new AppStateGrainState();
|
|
}
|
|
|
|
State.SetRegistry(fieldRegistry);
|
|
}
|
|
|
|
public virtual Task<(IAppEntity, ISchemaEntity)> GetAppWithSchemaAsync(Guid id)
|
|
{
|
|
return dispatcher.DispatchAndUnwrapAsync(() =>
|
|
{
|
|
var schema = State.FindSchema(x => x.Id == id && !x.IsDeleted);
|
|
|
|
return Task.FromResult((State.GetApp(), schema));
|
|
});
|
|
}
|
|
|
|
public virtual Task<IAppEntity> GetAppAsync()
|
|
{
|
|
return dispatcher.DispatchAndUnwrapAsync(() =>
|
|
{
|
|
var value = State.GetApp();
|
|
|
|
return Task.FromResult(value);
|
|
});
|
|
}
|
|
|
|
public virtual Task<List<IRuleEntity>> GetRulesAsync()
|
|
{
|
|
return dispatcher.DispatchAndUnwrapAsync(() =>
|
|
{
|
|
var value = State.FindRules();
|
|
|
|
return Task.FromResult(value);
|
|
});
|
|
}
|
|
|
|
public virtual Task<List<ISchemaEntity>> GetSchemasAsync()
|
|
{
|
|
return dispatcher.DispatchAndUnwrapAsync(() =>
|
|
{
|
|
var value = State.FindSchemas(x => !x.IsDeleted);
|
|
|
|
return Task.FromResult(value);
|
|
});
|
|
}
|
|
|
|
public virtual Task<ISchemaEntity> GetSchemaAsync(Guid id, bool provideDeleted = false)
|
|
{
|
|
return dispatcher.DispatchAndUnwrapAsync(() =>
|
|
{
|
|
var value = State.FindSchema(x => x.Id == id && (!x.IsDeleted || provideDeleted));
|
|
|
|
return Task.FromResult(value);
|
|
});
|
|
}
|
|
|
|
public virtual Task<ISchemaEntity> GetSchemaAsync(string name, bool provideDeleted = false)
|
|
{
|
|
return dispatcher.DispatchAndUnwrapAsync(() =>
|
|
{
|
|
var value = State.FindSchema(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) && (!x.IsDeleted || provideDeleted));
|
|
|
|
return Task.FromResult(value);
|
|
});
|
|
}
|
|
|
|
public virtual Task HandleAsync(Envelope<IEvent> message)
|
|
{
|
|
return dispatcher.DispatchAndUnwrapAsync(() =>
|
|
{
|
|
if (exception != null)
|
|
{
|
|
if (message.Payload is AppCreated)
|
|
{
|
|
exception = null;
|
|
}
|
|
else
|
|
{
|
|
throw exception;
|
|
}
|
|
}
|
|
|
|
State.Apply(message);
|
|
|
|
return WriteStateAsync();
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|