Headless CMS and Content Managment Hub
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.
 
 
 
 
 

51 lines
1.7 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
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;
namespace Squidex.Domain.Users.MongoDb
{
public sealed class MongoXmlRepository : IXmlRepository
{
private static readonly ReplaceOptions UpsertReplace = new ReplaceOptions { IsUpsert = true };
private readonly IMongoCollection<MongoXmlEntity> collection;
public MongoXmlRepository(IMongoDatabase mongoDatabase)
{
Guard.NotNull(mongoDatabase, nameof(mongoDatabase));
collection = mongoDatabase.GetCollection<MongoXmlEntity>("States_Repository");
}
public IReadOnlyCollection<XElement> GetAllElements()
{
var documents = collection.Find(new BsonDocument()).ToList();
var elements = documents.Select(x => XElement.Parse(x.Xml)).ToList();
return elements;
}
public void StoreElement(XElement element, string friendlyName)
{
var document = new MongoXmlEntity
{
FriendlyName = friendlyName
};
document.Xml = element.ToString();
collection.ReplaceOne(x => x.FriendlyName == friendlyName, document, UpsertReplace);
}
}
}