Beispiel #1
0
        public async Task <ItemResponse> Get(string type, string value)
        {
            var content = await ContentGetter.GetAsync(type, value).ConfigureAwait(false);

            if (content == null)
            {
                return(null);
            }

            var ancestors = await AncestorProvider.GetAncestorsAsync(content).ConfigureAwait(false);

            return(new ItemResponse(GetItem(content), ancestors.Select(a =>
            {
                var id = "{" + string.Join(",", PrimaryKeyGetter.Get(a)) + "}";
                return new ItemParent((a as INameable)?.Name ?? id, id);
            }).ToList().AsReadOnly()));
        }
        public async Task <IEnumerable <string> > GetUrl(string[] id, string contentTypeId)
        {
            var contentType = ContentTypeProvider.Get(contentTypeId);

            var content = await ContentGetter.GetAsync(contentTypeId, PrimaryKeyConverter.Convert(id, contentTypeId)).ConfigureAwait(false);

            var contentRouteSegment = await UrlProvider.GetAsync(content).ConfigureAwait(false);

            var contentRoutes = ContentRouteMatcher.GetFor(contentType);

            var result = new List <string>();

            foreach (var contentRoute in contentRoutes)
            {
                result.Add(contentRoute.Apply(contentRouteSegment));
            }

            return(result.AsReadOnly());
        }
Beispiel #3
0
        public async Task InitializeAsync()
        {
            foreach (var singleton in SingletonProvider.GetAll())
            {
                var content = await ContentGetter.GetAsync(singleton.ContentTypeId, singleton.KeyValues);

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

                if (content != null)
                {
                    continue;
                }

                content = ContentInstanceCreator.Create(contentType);

                PrimaryKeySetter.Set(singleton.KeyValues, content);

                await ContentInserter.InsertAsync(content).ConfigureAwait(false);
            }
        }
        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"));
            }
        }