Browse Source

Better handling of deleted schemas for duplicates.

pull/214/head
Sebastian Stehle 8 years ago
parent
commit
d31e811413
  1. 4
      src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemaRepository.cs
  2. 24
      src/Squidex.Domain.Apps.Entities/AppProvider.cs
  3. 4
      src/Squidex.Domain.Apps.Entities/Schemas/Repositories/ISchemaRepository.cs

4
src/Squidex.Domain.Apps.Entities.MongoDb/Schemas/MongoSchemaRepository.cs

@ -34,7 +34,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas
await collection.Indexes.CreateOneAsync(Index.Ascending(x => x.Name));
}
public async Task<IReadOnlyList<Guid>> QuerySchemaIdsAsync(Guid appId, string name)
public async Task<IReadOnlyList<Guid>> QueryAllSchemaIdsAsync(Guid appId, string name)
{
var schemaEntities =
await Collection.Find(x => x.AppId == appId && x.Name == name).Only(x => x.Id).SortByDescending(x => x.Version)
@ -43,7 +43,7 @@ namespace Squidex.Domain.Apps.Entities.MongoDb.Schemas
return schemaEntities.Select(x => Guid.Parse(x["_id"].AsString)).ToList();
}
public async Task<IReadOnlyList<Guid>> QuerySchemaIdsAsync(Guid appId)
public async Task<IReadOnlyList<Guid>> QueryAllSchemaIdsAsync(Guid appId)
{
var schemaEntities =
await Collection.Find(x => x.AppId == appId).Only(x => x.Id)

24
src/Squidex.Domain.Apps.Entities/AppProvider.cs

@ -49,14 +49,14 @@ namespace Squidex.Domain.Apps.Entities
{
var app = await stateFactory.GetSingleAsync<AppDomainObject>(appId);
if (IsNotFound(app))
if (IsFound(app))
{
return (null, null);
}
var schema = await stateFactory.GetSingleAsync<SchemaDomainObject>(id);
return IsNotFound(false, schema) ? (null, null) : (app.State, schema.State);
return IsFound(schema, false) ? (null, null) : (app.State, schema.State);
}
public async Task<IAppEntity> GetAppAsync(string appName)
@ -70,36 +70,36 @@ namespace Squidex.Domain.Apps.Entities
var app = await stateFactory.GetSingleAsync<AppDomainObject>(appId);
return IsNotFound(app) ? null : app.State;
return IsFound(app) ? app.State : null;
}
public async Task<ISchemaEntity> GetSchemaAsync(Guid appId, Guid id, bool provideDeleted = false)
{
var schema = await stateFactory.GetSingleAsync<SchemaDomainObject>(id);
return IsNotFound(provideDeleted, schema) ? null : schema.State;
return IsFound(schema, provideDeleted) ? schema.State : null;
}
public async Task<ISchemaEntity> GetSchemaAsync(Guid appId, string name, bool provideDeleted = false)
{
var ids = await schemaRepository.QuerySchemaIdsAsync(appId);
var ids = await schemaRepository.QueryAllSchemaIdsAsync(appId, name);
var schemas =
await Task.WhenAll(
ids.Select(id => stateFactory.GetSingleAsync<SchemaDomainObject>(id)));
return schemas.OrderByDescending(x => x.State.LastModified).FirstOrDefault(s => IsNotFound(provideDeleted, s))?.State;
return schemas.OrderByDescending(x => x.State.LastModified).FirstOrDefault(s => IsFound(s, provideDeleted))?.State;
}
public async Task<List<ISchemaEntity>> GetSchemasAsync(Guid appId)
{
var ids = await schemaRepository.QuerySchemaIdsAsync(appId);
var ids = await schemaRepository.QueryAllSchemaIdsAsync(appId);
var schemas =
await Task.WhenAll(
ids.Select(id => stateFactory.GetSingleAsync<SchemaDomainObject>(id)));
return schemas.Where(s => !s.State.IsDeleted).Select(s => (ISchemaEntity)s.State).ToList();
return schemas.Where(s => IsFound(s)).Select(s => (ISchemaEntity)s.State).ToList();
}
public async Task<List<IRuleEntity>> GetRulesAsync(Guid appId)
@ -129,14 +129,14 @@ namespace Squidex.Domain.Apps.Entities
return appRepository.FindAppIdByNameAsync(name);
}
private static bool IsNotFound(AppDomainObject app)
private static bool IsFound(AppDomainObject app)
{
return app.Version < 0;
return app.Version >= 0;
}
private static bool IsNotFound(bool provideDeleted, SchemaDomainObject schema)
private static bool IsFound(SchemaDomainObject schema, bool provideDeleted = false)
{
return schema.Version < 0 || (schema.State.IsDeleted && !provideDeleted);
return schema.Version >= 0 && (!schema.State.IsDeleted || provideDeleted);
}
}
}

4
src/Squidex.Domain.Apps.Entities/Schemas/Repositories/ISchemaRepository.cs

@ -14,8 +14,8 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Repositories
{
public interface ISchemaRepository
{
Task<IReadOnlyList<Guid>> QuerySchemaIdsAsync(Guid appId, string name);
Task<IReadOnlyList<Guid>> QueryAllSchemaIdsAsync(Guid appId, string name);
Task<IReadOnlyList<Guid>> QuerySchemaIdsAsync(Guid appId);
Task<IReadOnlyList<Guid>> QueryAllSchemaIdsAsync(Guid appId);
}
}

Loading…
Cancel
Save