mirror of https://github.com/Squidex/squidex.git
18 changed files with 418 additions and 32 deletions
@ -0,0 +1,17 @@ |
|||||
|
// ==========================================================================
|
||||
|
// IHistoryEventEntity.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
namespace Squidex.Read.History |
||||
|
{ |
||||
|
public interface IHistoryEventEntity : IEntity |
||||
|
{ |
||||
|
string Channel { get; } |
||||
|
|
||||
|
string Message { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
// ==========================================================================
|
||||
|
// IHistoryEventRepository.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Squidex.Read.History.Repositories |
||||
|
{ |
||||
|
public interface IHistoryEventRepository |
||||
|
{ |
||||
|
Task<List<IHistoryEventEntity>> FindHistoryByChannel(string channelPrefix, int count); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Messages.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
namespace Squidex.Store.MongoDb.History |
||||
|
{ |
||||
|
public static class Messages |
||||
|
{ |
||||
|
public const string AppCreated = "AppCreated"; |
||||
|
|
||||
|
public const string AppContributor = "SchemaDeleted"; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,30 @@ |
|||||
|
// ==========================================================================
|
||||
|
// MessagesEN.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using Squidex.Events.Apps; |
||||
|
using Squidex.Infrastructure; |
||||
|
|
||||
|
namespace Squidex.Store.MongoDb.History |
||||
|
{ |
||||
|
public static class MessagesEN |
||||
|
{ |
||||
|
public static readonly IReadOnlyDictionary<string, string> Texts = |
||||
|
new Dictionary<string, string> |
||||
|
{ |
||||
|
{ |
||||
|
TypeNameRegistry.GetName<AppContributorAssigned>(), |
||||
|
"[User] assigned [Contributor] to app with permission [Permission]" |
||||
|
}, |
||||
|
{ |
||||
|
TypeNameRegistry.GetName<AppContributorRemoved>(), |
||||
|
"[User] removed [Contributor] from app" |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
// ==========================================================================
|
||||
|
// MongoHistoryEventEntity.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using MongoDB.Bson.Serialization.Attributes; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.CQRS; |
||||
|
using Squidex.Store.MongoDb.Utils; |
||||
|
|
||||
|
namespace Squidex.Store.MongoDb.History |
||||
|
{ |
||||
|
public sealed class MongoHistoryEventEntity : MongoEntity |
||||
|
{ |
||||
|
[BsonRequired] |
||||
|
[BsonElement] |
||||
|
public string Channel { get; set; } |
||||
|
|
||||
|
[BsonRequired] |
||||
|
[BsonElement] |
||||
|
public string Message { get; set; } |
||||
|
|
||||
|
[BsonRequired] |
||||
|
[BsonElement] |
||||
|
public UserToken User { get; set; } |
||||
|
|
||||
|
[BsonRequired] |
||||
|
[BsonElement] |
||||
|
public Dictionary<string, string> Parameters { get; set; } |
||||
|
|
||||
|
public MongoHistoryEventEntity() |
||||
|
{ |
||||
|
Parameters = new Dictionary<string, string>(); |
||||
|
} |
||||
|
|
||||
|
public MongoHistoryEventEntity Setup<T>(EnvelopeHeaders headers, string channel) |
||||
|
{ |
||||
|
Channel = channel; |
||||
|
|
||||
|
if (headers.Contains(CommonHeaders.User)) |
||||
|
{ |
||||
|
AddParameter("User", headers[CommonHeaders.User].ToString()); |
||||
|
} |
||||
|
|
||||
|
Message = TypeNameRegistry.GetName<T>(); |
||||
|
|
||||
|
return this; |
||||
|
} |
||||
|
|
||||
|
public MongoHistoryEventEntity AddParameter(string key, string value) |
||||
|
{ |
||||
|
Parameters.Add(key, value); |
||||
|
|
||||
|
return this; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,79 @@ |
|||||
|
// ==========================================================================
|
||||
|
// MongoHistoryEventRepository.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using MongoDB.Driver; |
||||
|
using Squidex.Events.Apps; |
||||
|
using Squidex.Infrastructure.CQRS; |
||||
|
using Squidex.Infrastructure.CQRS.Events; |
||||
|
using Squidex.Infrastructure.Dispatching; |
||||
|
using Squidex.Read.History; |
||||
|
using Squidex.Read.History.Repositories; |
||||
|
using Squidex.Store.MongoDb.Utils; |
||||
|
using System.Linq; |
||||
|
|
||||
|
namespace Squidex.Store.MongoDb.History |
||||
|
{ |
||||
|
public class MongoHistoryEventRepository : MongoRepositoryBase<MongoHistoryEventEntity>, IHistoryEventRepository, ICatchEventConsumer |
||||
|
{ |
||||
|
public MongoHistoryEventRepository(IMongoDatabase database) |
||||
|
: base(database) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
protected override string CollectionName() |
||||
|
{ |
||||
|
return "Projections_History"; |
||||
|
} |
||||
|
|
||||
|
protected override Task SetupCollectionAsync(IMongoCollection<MongoHistoryEventEntity> collection) |
||||
|
{ |
||||
|
return Task.WhenAll( |
||||
|
collection.Indexes.CreateOneAsync(IndexKeys.Ascending(x => x.Channel)), |
||||
|
collection.Indexes.CreateOneAsync(IndexKeys.Ascending(x => x.Created), new CreateIndexOptions { ExpireAfter = TimeSpan.FromDays(365) })); |
||||
|
} |
||||
|
|
||||
|
public async Task<List<IHistoryEventEntity>> FindHistoryByChannel(string channelPrefix, int count) |
||||
|
{ |
||||
|
var entities = |
||||
|
await Collection.Find(x => x.Channel.StartsWith(channelPrefix)).Limit(count).ToListAsync(); |
||||
|
|
||||
|
return entities.Select(x => (IHistoryEventEntity)new ParsedHistoryEvent(x, MessagesEN.Texts)).ToList(); |
||||
|
} |
||||
|
|
||||
|
protected Task On(AppContributorAssigned @event, EnvelopeHeaders headers) |
||||
|
{ |
||||
|
return Collection.CreateAsync(headers, x => |
||||
|
{ |
||||
|
var channel = $"Apps.{headers.AggregateId()}.Contributors"; |
||||
|
|
||||
|
x.Setup<AppContributorAssigned>(headers, channel) |
||||
|
.AddParameter("Contributor", @event.ContributorId) |
||||
|
.AddParameter("Permission", @event.Permission.ToString()); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
protected Task On(AppContributorRemoved @event, EnvelopeHeaders headers) |
||||
|
{ |
||||
|
return Collection.CreateAsync(headers, x => |
||||
|
{ |
||||
|
var channel = $"Apps.{headers.AggregateId()}.Contributors"; |
||||
|
|
||||
|
x.Setup<AppContributorRemoved>(headers, channel) |
||||
|
.AddParameter("Contributor", @event.ContributorId); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
public Task On(Envelope<IEvent> @event) |
||||
|
{ |
||||
|
return this.DispatchActionAsync(@event.Payload, @event.Headers); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,62 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ParsedHistoryEvent.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using Squidex.Read.History; |
||||
|
|
||||
|
namespace Squidex.Store.MongoDb.History |
||||
|
{ |
||||
|
internal sealed class ParsedHistoryEvent : IHistoryEventEntity |
||||
|
{ |
||||
|
private readonly MongoHistoryEventEntity inner; |
||||
|
private readonly Lazy<string> message; |
||||
|
|
||||
|
public Guid Id |
||||
|
{ |
||||
|
get { return inner.Id; } |
||||
|
} |
||||
|
|
||||
|
public DateTime Created |
||||
|
{ |
||||
|
get { return inner.Created; } |
||||
|
} |
||||
|
|
||||
|
public DateTime LastModified |
||||
|
{ |
||||
|
get { return inner.LastModified; } |
||||
|
} |
||||
|
|
||||
|
public string Channel |
||||
|
{ |
||||
|
get { return inner.Channel; } |
||||
|
} |
||||
|
|
||||
|
public string Message |
||||
|
{ |
||||
|
get { return message.Value; } |
||||
|
} |
||||
|
|
||||
|
public ParsedHistoryEvent(MongoHistoryEventEntity inner, IReadOnlyDictionary<string, string> texts) |
||||
|
{ |
||||
|
this.inner = inner; |
||||
|
|
||||
|
message = new Lazy<string>(() => |
||||
|
{ |
||||
|
var result = texts[inner.Message]; |
||||
|
|
||||
|
foreach (var kvp in inner.Parameters) |
||||
|
{ |
||||
|
result = result.Replace("[" + kvp.Key + "]", kvp.Value); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
|
Before Width: | Height: | Size: 6.8 KiB After Width: | Height: | Size: 9.0 KiB |
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue