Exemple #1
0
        public ContentResponseMessage SaveContent([FromBody] SaveContentRequestBody data)
        {
            if (!ModelState.IsValid)
            {
                return(ContentResponseMessage.CreateFrom(ModelState));
            }

            var contentType = ContentTypeProvider.Get(data.ContentTypeId);

            var b = (IContent)JsonConvert.DeserializeObject(data.Content, contentType.Type, PolymorphicFormConverter);

            if (b.Id != null)
            {
                var a = (IContent)typeof(IContainerSpecificContentGetter).GetMethod(nameof(ContainerSpecificContentGetter.Get)).MakeGenericMethod(contentType.Type).Invoke(ContainerSpecificContentGetter, new[] { data.Id, null, contentType.Container });

                foreach (var coreInterface in ContentTypeCoreInterfaceProvider.GetFor(contentType.Id))
                {
                    foreach (var propertyDefinition in coreInterface.PropertyDefinitions)
                    {
                        var display = propertyDefinition.Attributes.OfType <DisplayAttribute>().FirstOrDefault();

                        if (display != null && display.GetAutoGenerateField() == false)
                        {
                            continue;
                        }

                        propertyDefinition.Setter(a, propertyDefinition.Getter(b));
                    }
                }

                foreach (var propertyDefinition in PropertyDefinitionProvider.GetFor(contentType.Id))
                {
                    var display = propertyDefinition.Attributes.OfType <DisplayAttribute>().FirstOrDefault();

                    if (display != null && display.GetAutoGenerateField() == false)
                    {
                        continue;
                    }

                    propertyDefinition.Setter(a, propertyDefinition.Getter(b));
                }

                ContainerSpecificContentUpdater.Update(a, contentType.Container);

                return(new ContentResponseMessage(true, "Updated"));
            }
            else
            {
                ContainerSpecificContentCreator.Create(b, contentType.Container);

                return(new ContentResponseMessage(true, "Created"));
            }
        }
        public async Task <ContentResponseMessage> SaveContent([FromBody] SaveContentRequestBody data)
        {
            if (!ModelState.IsValid)
            {
                return(ContentResponseMessage.CreateFrom(ModelState));
            }

            var contentType = ContentTypeProvider.Get(data.ContentTypeId);

            var b = JsonConvert.DeserializeObject(data.Content, contentType.Type, PolymorphicFormConverter);

            if (!TryValidateModel(b))
            {
                return(ContentResponseMessage.CreateFrom(ModelState));
            }

            if (PrimaryKeyGetter.Get(b).All(id => id != null))
            {
                var a = await ContentGetter.GetAsync(contentType.Id, PrimaryKeyConverter.Convert(data.KeyValues, contentType.Id)).ConfigureAwait(false);

                foreach (var coreInterface in ContentTypeCoreInterfaceProvider.GetFor(contentType.Id))
                {
                    foreach (var propertyDefinition in coreInterface.PropertyDefinitions)
                    {
                        var display = propertyDefinition.Attributes.OfType <DisplayAttribute>().FirstOrDefault();

                        if (display != null && display.GetAutoGenerateField() == false)
                        {
                            continue;
                        }

                        propertyDefinition.Setter(a, propertyDefinition.Getter(b));
                    }
                }

                foreach (var propertyDefinition in PropertyDefinitionProvider.GetFor(contentType.Id))
                {
                    var display = propertyDefinition.Attributes.OfType <DisplayAttribute>().FirstOrDefault();

                    if (display != null && display.GetAutoGenerateField() == false)
                    {
                        continue;
                    }

                    propertyDefinition.Setter(a, propertyDefinition.Getter(b));
                }

                if (!TryValidateModel(b))
                {
                    throw new Exception("a was valid but b was not.");
                }

                await ContentUpdater.UpdateAsync(a).ConfigureAwait(false);

                return(new ContentResponseMessage(true, "Updated"));
            }
            else
            {
                await ContentCreator.CreateAsync(b).ConfigureAwait(false);

                return(new ContentResponseMessage(true, "Created"));
            }
        }