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.
83 lines
2.8 KiB
83 lines
2.8 KiB
// ==========================================================================
|
|
// MongoSchemaRepository.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 MongoDB.Driver;
|
|
using Squidex.Domain.Apps.Core.Schemas;
|
|
using Squidex.Domain.Apps.Core.Schemas.Json;
|
|
using Squidex.Domain.Apps.Read.Schemas;
|
|
using Squidex.Domain.Apps.Read.Schemas.Repositories;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Infrastructure.CQRS.Events;
|
|
using Squidex.Infrastructure.MongoDb;
|
|
|
|
namespace Squidex.Domain.Apps.Read.MongoDb.Schemas
|
|
{
|
|
public partial class MongoSchemaRepository : MongoRepositoryBase<MongoSchemaEntity>, ISchemaRepository, IEventConsumer
|
|
{
|
|
private readonly SchemaJsonSerializer serializer;
|
|
private readonly FieldRegistry registry;
|
|
|
|
public MongoSchemaRepository(IMongoDatabase database, SchemaJsonSerializer serializer, FieldRegistry registry)
|
|
: base(database)
|
|
{
|
|
Guard.NotNull(registry, nameof(registry));
|
|
Guard.NotNull(serializer, nameof(serializer));
|
|
|
|
this.registry = registry;
|
|
|
|
this.serializer = serializer;
|
|
}
|
|
|
|
protected override string CollectionName()
|
|
{
|
|
return "Projections_Schemas";
|
|
}
|
|
|
|
protected override Task SetupCollectionAsync(IMongoCollection<MongoSchemaEntity> collection)
|
|
{
|
|
return collection.Indexes.CreateOneAsync(Index.Ascending(x => x.Name));
|
|
}
|
|
|
|
public async Task<IReadOnlyList<ISchemaEntity>> QueryAllAsync(Guid appId)
|
|
{
|
|
var schemaEntities =
|
|
await Collection.Find(s => s.AppId == appId && !s.IsDeleted)
|
|
.ToListAsync();
|
|
|
|
schemaEntities.ForEach(x => x.DeserializeSchema(serializer));
|
|
|
|
return schemaEntities.OfType<ISchemaEntity>().ToList();
|
|
}
|
|
|
|
public async Task<ISchemaEntity> FindSchemaAsync(Guid appId, string name)
|
|
{
|
|
var schemaEntity =
|
|
await Collection.Find(s => s.Name == name && s.AppId == appId && !s.IsDeleted)
|
|
.FirstOrDefaultAsync();
|
|
|
|
schemaEntity?.DeserializeSchema(serializer);
|
|
|
|
return schemaEntity;
|
|
}
|
|
|
|
public async Task<ISchemaEntity> FindSchemaAsync(Guid schemaId)
|
|
{
|
|
var schemaEntity =
|
|
await Collection.Find(s => s.Id == schemaId)
|
|
.FirstOrDefaultAsync();
|
|
|
|
schemaEntity?.DeserializeSchema(serializer);
|
|
|
|
return schemaEntity;
|
|
}
|
|
}
|
|
}
|
|
|