mirror of https://github.com/Squidex/squidex.git
12 changed files with 85 additions and 136 deletions
@ -0,0 +1,25 @@ |
|||||
|
// ==========================================================================
|
||||
|
// MongoXmlDocument.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using MongoDB.Bson; |
||||
|
using MongoDB.Bson.Serialization.Attributes; |
||||
|
|
||||
|
namespace Squidex.Domain.Users.MongoDb |
||||
|
{ |
||||
|
public sealed class MongoXmlDocument |
||||
|
{ |
||||
|
[BsonId] |
||||
|
[BsonElement] |
||||
|
[BsonRepresentation(BsonType.String)] |
||||
|
public string Id { get; set; } |
||||
|
|
||||
|
[BsonRequired] |
||||
|
[BsonElement] |
||||
|
public string Xml { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
// ==========================================================================
|
||||
|
// MongoXmlRepository.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Xml.Linq; |
||||
|
using Microsoft.AspNetCore.DataProtection.Repositories; |
||||
|
using MongoDB.Bson; |
||||
|
using MongoDB.Driver; |
||||
|
using Squidex.Infrastructure.MongoDb; |
||||
|
|
||||
|
namespace Squidex.Domain.Users.MongoDb |
||||
|
{ |
||||
|
public sealed class MongoXmlRepository : MongoRepositoryBase<MongoXmlDocument>, IXmlRepository |
||||
|
{ |
||||
|
private static readonly UpdateOptions Upsert = new UpdateOptions { IsUpsert = true }; |
||||
|
|
||||
|
public MongoXmlRepository(IMongoDatabase database) |
||||
|
: base(database) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
protected override string CollectionName() |
||||
|
{ |
||||
|
return "Identity_XmlRepository"; |
||||
|
} |
||||
|
|
||||
|
public IReadOnlyCollection<XElement> GetAllElements() |
||||
|
{ |
||||
|
var elements = Collection.Find(new BsonDocument()).ToList(); |
||||
|
|
||||
|
return elements.Select(x => XElement.Parse(x.Xml)).ToList(); |
||||
|
} |
||||
|
|
||||
|
public void StoreElement(XElement element, string friendlyName) |
||||
|
{ |
||||
|
Collection.UpdateOne(Filter.Eq(x => x.Id, friendlyName), |
||||
|
Update.Set(x => x.Xml, element.ToString()), |
||||
|
Upsert); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,20 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// IXmlRepositoryGrain.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System.Threading.Tasks; |
|
||||
using Orleans; |
|
||||
|
|
||||
namespace Squidex.Domain.Users.DataProtection.Orleans.Grains |
|
||||
{ |
|
||||
public interface IXmlRepositoryGrain : IGrainWithStringKey |
|
||||
{ |
|
||||
Task<string[]> GetAllElementsAsync(); |
|
||||
|
|
||||
Task StoreElementAsync(string element, string friendlyName); |
|
||||
} |
|
||||
} |
|
||||
@ -1,55 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// XmlRepositoryGrain.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Linq; |
|
||||
using System.Threading.Tasks; |
|
||||
using Orleans.Runtime; |
|
||||
using Squidex.Infrastructure.Log; |
|
||||
using Squidex.Infrastructure.Orleans; |
|
||||
|
|
||||
namespace Squidex.Domain.Users.DataProtection.Orleans.Grains.Implementations |
|
||||
{ |
|
||||
public sealed class XmlRepositoryGrain : GrainV2<Dictionary<string, string>>, IXmlRepositoryGrain |
|
||||
{ |
|
||||
private readonly ISemanticLog log; |
|
||||
|
|
||||
public XmlRepositoryGrain(IGrainRuntime runtime, ISemanticLog log) |
|
||||
: base(runtime) |
|
||||
{ |
|
||||
this.log = log; |
|
||||
} |
|
||||
|
|
||||
protected override async Task ReadStateAsync() |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
await base.ReadStateAsync(); |
|
||||
} |
|
||||
catch (Exception ex) |
|
||||
{ |
|
||||
State = new Dictionary<string, string>(); |
|
||||
|
|
||||
log.LogError(ex, w => w.WriteProperty("action", "LoadXmlRepository")); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public Task<string[]> GetAllElementsAsync() |
|
||||
{ |
|
||||
return Task.FromResult(State.Values.ToArray()); |
|
||||
} |
|
||||
|
|
||||
public Task StoreElementAsync(string element, string friendlyName) |
|
||||
{ |
|
||||
State[friendlyName] = element; |
|
||||
|
|
||||
return WriteStateAsync(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,41 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// OrleansXmlRepository.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Linq; |
|
||||
using System.Xml.Linq; |
|
||||
using Microsoft.AspNetCore.DataProtection.Repositories; |
|
||||
using Orleans; |
|
||||
using Squidex.Domain.Users.DataProtection.Orleans.Grains; |
|
||||
using Squidex.Infrastructure; |
|
||||
|
|
||||
namespace Squidex.Domain.Users.DataProtection.Orleans |
|
||||
{ |
|
||||
public sealed class OrleansXmlRepository : IXmlRepository |
|
||||
{ |
|
||||
private readonly Lazy<IXmlRepositoryGrain> grain; |
|
||||
|
|
||||
public OrleansXmlRepository(IClusterClient orleans) |
|
||||
{ |
|
||||
Guard.NotNull(orleans, nameof(orleans)); |
|
||||
|
|
||||
grain = new Lazy<IXmlRepositoryGrain>(() => orleans.GetGrain<IXmlRepositoryGrain>("Default")); |
|
||||
} |
|
||||
|
|
||||
public IReadOnlyCollection<XElement> GetAllElements() |
|
||||
{ |
|
||||
return grain.Value.GetAllElementsAsync().ContinueWith(x => x.Result.Select(XElement.Parse).ToList()).Result; |
|
||||
} |
|
||||
|
|
||||
public void StoreElement(XElement element, string friendlyName) |
|
||||
{ |
|
||||
grain.Value.StoreElementAsync(element.ToString(), friendlyName).Wait(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue