Esempio n. 1
0
        private async Task <ValueConverter?> CleanReferencesAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
        {
            if (!context.ShouldSkipCleanup())
            {
                var ids = new HashSet <DomainId>();

                foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
                {
                    var schema = await schemas(group.Key);

                    foreach (var content in group)
                    {
                        content.Data.AddReferencedIds(schema.SchemaDef, ids);
                    }
                }

                if (ids.Count > 0)
                {
                    var(assets, refContents) = await AsyncHelper.WhenAll(
                        QueryAssetIdsAsync(context, ids),
                        QueryContentIdsAsync(context, ids));

                    var foundIds = assets.Union(refContents).ToHashSet();

                    return(ValueReferencesConverter.CleanReferences(foundIds));
                }
            }

            return(null);
        }
Esempio n. 2
0
        private async Task <ValueConverter?> CleanReferencesAsync(Context context, IEnumerable <ContentEntity> contents, ProvideSchema schemas)
        {
            if (context.ShouldCleanup())
            {
                var ids = new HashSet <Guid>();

                foreach (var group in contents.GroupBy(x => x.SchemaId.Id))
                {
                    var schema = await schemas(group.Key);

                    foreach (var content in group)
                    {
                        content.Data.AddReferencedIds(schema.SchemaDef, ids);
                    }
                }

                if (ids.Count > 0)
                {
                    var taskForAssets   = QueryAssetIdsAsync(context, ids);
                    var taskForContents = QueryContentIdsAsync(context, ids);

                    await Task.WhenAll(taskForAssets, taskForContents);

                    var foundIds = new HashSet <Guid>(taskForAssets.Result.Union(taskForContents.Result));

                    return(ValueReferencesConverter.CleanReferences(foundIds));
                }
            }

            return(null);
        }
Esempio n. 3
0
 public static NamedContentData FromMongoModel(this IdContentData result, Schema schema, List <Guid> deletedIds)
 {
     return(result.ConvertId2Name(schema,
                                  FieldConverters.ForValues(
                                      ValueConverters.DecodeJson(),
                                      ValueReferencesConverter.CleanReferences(deletedIds)),
                                  FieldConverters.ForNestedId2Name(
                                      ValueConverters.DecodeJson(),
                                      ValueReferencesConverter.CleanReferences(deletedIds))));
 }
Esempio n. 4
0
        public void Should_remove_deleted_ids_from_field(IField field)
        {
            var id1 = DomainId.NewGuid();
            var id2 = DomainId.NewGuid();

            var value = CreateValue(id1, id2);

            var result = ValueReferencesConverter.CleanReferences(HashSet.Of(id1))(value, field, null);

            Assert.Equal(CreateValue(id1), result);
        }
        public void Should_cleanup_deleted_ids()
        {
            var id1 = Guid.NewGuid();
            var id2 = Guid.NewGuid();

            var input =
                new IdContentData()
                .AddField(5,
                          new ContentFieldData()
                          .AddValue("iv", JsonValue.Array(id1.ToString(), id2.ToString())));

            var converter = FieldConverters.ForValues(ValueReferencesConverter.CleanReferences(new[] { id2 }));

            var actual = input.ConvertId2Id(schema, converter);

            var cleanedValue = (JsonArray)actual[5] !["iv"];
Esempio n. 6
0
        public void Should_cleanup_deleted_ids()
        {
            var id1 = DomainId.NewGuid();
            var id2 = DomainId.NewGuid();

            var source =
                new NamedContentData()
                .AddField("references",
                          new ContentFieldData()
                          .AddJsonValue(JsonValue.Array(id1, id2)))
                .AddField("assets",
                          new ContentFieldData()
                          .AddJsonValue(JsonValue.Array(id1)))
                .AddField("array",
                          new ContentFieldData()
                          .AddJsonValue(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("nested", JsonValue.Array(id1, id2)))));

            var expected =
                new NamedContentData()
                .AddField("references",
                          new ContentFieldData()
                          .AddJsonValue(JsonValue.Array(id2)))
                .AddField("assets",
                          new ContentFieldData()
                          .AddJsonValue(JsonValue.Array()))
                .AddField("array",
                          new ContentFieldData()
                          .AddJsonValue(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("nested", JsonValue.Array(id2)))));

            var cleaner = ValueReferencesConverter.CleanReferences(new HashSet <DomainId> {
                id2
            });
            var cleanNested = ValueConverters.ForNested(cleaner);

            var converter = FieldConverters.ForValues(cleaner, cleanNested);

            var actual = source.ConvertName2Name(schema, converter);

            Assert.Equal(expected, actual);
        }
Esempio n. 7
0
        public void Should_return_same_value_from_field_when_value_is_json_null(IField field)
        {
            var result = ValueReferencesConverter.CleanReferences(RandomIds())(JsonValue.Null, field, null);

            Assert.Equal(JsonValue.Null, result);
        }
Esempio n. 8
0
        public void Should_cleanup_deleted_ids()
        {
            var id1 = DomainId.NewGuid();
            var id2 = DomainId.NewGuid();

            var source =
                new ContentData()
                .AddField("references",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array(id1, id2)))
                .AddField("assets1",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array(id1)))
                .AddField("array",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("nested", JsonValue.Array(id1, id2)))))
                .AddField("component",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Object()
                              .Add("references",
                                   JsonValue.Array(id1, id2))
                              .Add("assets1",
                                   JsonValue.Array(id1))
                              .Add("array",
                                   JsonValue.Array(
                                       JsonValue.Object()
                                       .Add("nested", JsonValue.Array(id1, id2))))
                              .Add(Component.Discriminator, DomainId.Empty)))
                .AddField("components",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("references",
                                       JsonValue.Array(id1, id2))
                                  .Add("assets1",
                                       JsonValue.Array(id1))
                                  .Add("array",
                                       JsonValue.Array(
                                           JsonValue.Object()
                                           .Add("nested", JsonValue.Array(id1, id2))))
                                  .Add(Component.Discriminator, DomainId.Empty))));

            var expected =
                new ContentData()
                .AddField("references",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array(id2)))
                .AddField("assets1",
                          new ContentFieldData()
                          .AddInvariant(JsonValue.Array()))
                .AddField("array",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("nested", JsonValue.Array(id2)))))
                .AddField("component",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Object()
                              .Add("references",
                                   JsonValue.Array(id2))
                              .Add("assets1",
                                   JsonValue.Array())
                              .Add("array",
                                   JsonValue.Array(
                                       JsonValue.Object()
                                       .Add("nested", JsonValue.Array(id2))))
                              .Add(Component.Discriminator, DomainId.Empty)))
                .AddField("components",
                          new ContentFieldData()
                          .AddInvariant(
                              JsonValue.Array(
                                  JsonValue.Object()
                                  .Add("references",
                                       JsonValue.Array(id2))
                                  .Add("assets1",
                                       JsonValue.Array())
                                  .Add("array",
                                       JsonValue.Array(
                                           JsonValue.Object()
                                           .Add("nested", JsonValue.Array(id2))))
                                  .Add(Component.Discriminator, DomainId.Empty))));

            var converter =
                FieldConverters.ForValues(
                    ValueReferencesConverter.CleanReferences(new HashSet <DomainId> {
                id2
            }));

            var actual = source.Convert(schema, converter);

            Assert.Equal(expected, actual);
        }