public object RemoveContent([FromBody] RemoveContentInput removeContentInput)
        {
            if (!ModelState.IsValid)
            {
                return(new
                {
                    success = false,
                    errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => new { description = e.ErrorMessage }),
                });
            }

            var contentType = ContentTypeProvider.Get(removeContentInput.ContentTypeId);
            var content     = ContainerSpecificContentGetter.Get <IContent>(removeContentInput.Id, null, contentType.Container);

            if (content.ContentTypeId != contentType.Id)
            {
                throw new Exception($"Content is not of type {contentType.Id}");
            }

            ContainerSpecificContentDeleter.Delete(content.Id, contentType.Container);

            return(new
            {
                success = true,
            });
        }
Beispiel #2
0
        public async Task <IEnumerable <IContent> > GetAncestorsAsync(IContent content)
        {
            var result = new List <IContent>();

            var position    = content;
            var contentType = ContentTypeProvider.Get(content.ContentTypeId);

            while (true)
            {
                var document = (await DocumentFinder.Find(contentType.Container).WhereEquals <IContent, string>(x => x.Id, ((IHierarchical)position).ParentId).WhereExists <IHierarchical, string>(x => x.ParentId).GetResultAsync().ConfigureAwait(false)).FirstOrDefault();

                if (document == null)
                {
                    break;
                }

                contentType = ContentTypeProvider.Get((string)document.GlobalFacet.Interfaces["IContent"].Properties["ContentTypeId"]);
                position    = ContentDeserializer.Deserialize(document, contentType, null);

                result.Add(position);

                if (((IHierarchical)position).ParentId == null)
                {
                    break;
                }
            }

            return(result);
        }
Beispiel #3
0
        public async Task <object> GetAsync(string contentTypeId, params object[] keyValues)
        {
            var contentType = ContentTypeProvider.Get(contentTypeId);
            var dbSet       = DbSetProvider.Get(contentType.Type);

            return(await dbSet.FindAsync(keyValues).ConfigureAwait(false));
        }
Beispiel #4
0
        public string SaveContent([FromBody] SaveContentRequestBody data)
        {
            var contentType = ContentTypeProvider.Get(data.ContentTypeId);

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

            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 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("Updated");
            }
            else
            {
                ContainerSpecificContentCreator.Create(b, contentType.Container);

                return("Saved");
            }
        }
Beispiel #5
0
        public IEnumerable <FormResponseItem> GetOptions(IEnumerable <string> types)
        {
            var result = new List <FormResponseItem>();

            foreach (var form in FormProvider.GetAll())
            {
                if (ContentTypeProvider.Get(form.Type) != null)
                {
                    continue;
                }

                if (!types.Contains(form.Id))
                {
                    continue;
                }

                result.Add(new FormResponseItem
                {
                    Type = form.Id,
                    Name = form.Type.GetCustomAttribute <DisplayAttribute>()?.Name ?? Humanizer.Humanize(form.Type.Name),
                });
            }

            return(result.OrderBy(f => f.Name).ToList().AsReadOnly());
        }
Beispiel #6
0
        public IContent GetSingleton(string id)
        {
            var contentType = ContentTypeProvider.Get(id);

            var singleton = SingletonProvider.Get(id);

            return(ContainerSpecificContentGetter.Get <IContent>(singleton.Id, null, contentType.Container));
        }
Beispiel #7
0
        public string GetUrl(string id, string contentTypeId)
        {
            var contentType = ContentTypeProvider.Get(contentTypeId);

            var content = ContainerSpecificContentGetter.Get <IContent>(id, null, contentType.Container);

            return(UrlProvider.Get(content));
        }
        public async Task Get(string[] contentTypeIds, string parent)
        {
            var contentTypes = contentTypeIds.Select(t => ContentTypeProvider.Get(t)).ToList().AsReadOnly();

            if (contentTypes.Count > 1)
            {
                throw new NotImplementedException("Multi type queries are not yet implemented");
            }

            var items = new List <object>();
            var itemChildrenCounts = new Dictionary <string, int>();

            var documentsQuery = ContentFinder.Find(contentTypes.Single().Type);

            if (parent != null)
            {
                documentsQuery.WhereParent(parent);
            }
            else
            {
                documentsQuery.WhereHasNoParent();
            }

            var documents = (await documentsQuery.GetResultAsync().ConfigureAwait(false)).ToList();

            foreach (var content in documents)
            {
                items.Add(content);

                if (content is IHierarchical)
                {
                    var id = PrimaryKeyGetter.Get(content);

                    itemChildrenCounts[string.Join(",", id)] = await ContentChildrenCounter.CountChildrenForAsync(id).ConfigureAwait(false);
                }
            }

            //var sortByPropertyName = typeof(INameable).IsAssignableFrom(contentType.Type) ? "Name" : "Id";
            //var sortByProperty = PropertyDefinitionProvider.GetFor(contentType.Id).FirstOrDefault(p => p.Name == sortByPropertyName);

            //if (sortByProperty != null)
            //{
            //    result = result.OrderBy(i => sortByProperty.Getter(i)).ToList();
            //}

            Response.ContentType = "application/json";

            await Response.WriteAsync(JsonConvert.SerializeObject(new ContentListResult {
                Items = items, ItemChildrenCounts = itemChildrenCounts
            }, new JsonSerializerSettings {
                Formatting = Formatting.Indented, ContractResolver = new CamelCasePropertyNamesContractResolver(), Converters = new List <JsonConverter> {
                    PolymorphicFormConverter
                }
            }));
        }
Beispiel #9
0
        public async Task DeleteAsync(string contentTypeId, string id)
        {
            var contentType = ContentTypeProvider.Get(contentTypeId);

            if (contentType == null)
            {
                throw new ArgumentException(nameof(contentTypeId));
            }

            await DocumentDeleter.DeleteAsync(contentType.Container, id);
        }
Beispiel #10
0
        public async Task DeleteAsync <T>(string id) where T : class, IContent
        {
            var contentType = ContentTypeProvider.Get(typeof(T));

            if (contentType == null)
            {
                throw new TypeNotRegisteredContentTypeException(typeof(T));
            }

            await DocumentDeleter.DeleteAsync(contentType.Container, id);
        }
        public async Task <Item> Get(string type, string value)
        {
            var contentType = ContentTypeProvider.Get(type);
            var content     = await ContainerSpecificContentGetter.GetAsync <IContent>(value, null, contentType.Container).ConfigureAwait(false);

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

            return(new Item((content as INameable)?.Name ?? content.Id, null, content.Id, (content as IImageable)?.Image));
        }
Beispiel #12
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"));
            }
        }
Beispiel #13
0
        public async Task <IEnumerable <Item> > GetAll(string type, ItemQuery query)
        {
            var result = new List <Item>();

            var contentType = ContentTypeProvider.Get(type);

            foreach (var content in await ContentFinder.Find(contentType.Type).GetResultAsync().ConfigureAwait(false))
            {
                result.Add(GetItem(content));
            }

            return(result.AsReadOnly());
        }
Beispiel #14
0
        public IEnumerable <T> GetChildren <T>(string id, string language) where T : class
        {
            var contentTypes = ContentTypeProvider.GetAll().Where(t => typeof(T).IsAssignableFrom(t.Type));

            var documents = DocumentFinder.Find(ContainerConstants.Content).WhereEquals <IHierarchical, string>(x => x.ParentId, id).WhereIn <IContent, string>(x => x.ContentTypeId, contentTypes.Select(t => t.Id)).GetResultAsync().Result.ToList();

            var result = new List <T>();

            foreach (var document in documents)
            {
                var contentTypeId = document.GlobalFacet.Interfaces["IContent"].Properties["ContentTypeId"] as string;

                result.Add((T)ContentDeserializer.Deserialize(document, ContentTypeProvider.Get(contentTypeId), language));
            }

            return(result.AsReadOnly());
        }
        public IEnumerable <ContentTypeGroupDescriptor> GetContentTypeGroupsFor(string contentTypeId)
        {
            var result = new List <ContentTypeGroupDescriptor>();

            var contentType = ContentTypeProvider.Get(contentTypeId);

            foreach (var contentTypeGroup in ContentTypeGroupProvider.GetAll())
            {
                if (contentTypeGroup.Type.IsAssignableFrom(contentType.Type))
                {
                    result.Add(contentTypeGroup);
                    break;
                }
            }

            return(result.AsReadOnly());
        }
        public async Task <IEnumerable <Item> > GetAll(string type)
        {
            var result = new List <Item>();

            var contentType = ContentTypeProvider.Get(type);

            foreach (var document in await DocumentFinder.Find(contentType.Container).GetResultAsync().ConfigureAwait(false))
            {
                if (document.GlobalFacet.Interfaces[nameof(IContent)].Properties[nameof(IContent.ContentTypeId)] as string != type)
                {
                    continue;
                }
                var content = ContentDeserializer.Deserialize(document, contentType, null);
                result.Add(new Item((content as INameable)?.Name ?? content.Id, null, content.Id, (content as IImageable)?.Image));
            }

            return(result.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 #18
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);
            }
        }
Beispiel #19
0
        public IEnumerable <object> GetContentList(string contentTypeId)
        {
            var contentType = ContentTypeProvider.Get(contentTypeId);

            var documents = DocumentFinder.Find(contentType.Container).WhereEquals <IContent, string>(x => x.ContentTypeId, contentType.Id).GetResultAsync().Result.ToList();

            var result = new List <object>();

            foreach (var document in documents)
            {
                result.Add(ContentDeserializer.Deserialize(document, contentType, DocumentLanguageConstants.Global));
            }

            var sortByPropertyName = typeof(INameable).IsAssignableFrom(contentType.Type) ? "Name" : "Id";
            var sortByProperty     = PropertyDefinitionProvider.GetFor(contentType.Id).FirstOrDefault(p => p.Name == sortByPropertyName);

            if (sortByProperty != null)
            {
                result = result.OrderBy(i => sortByProperty.Getter(i)).ToList();
            }

            return(result.AsReadOnly());
        }
Beispiel #20
0
        public void Initialize()
        {
            foreach (var singleton in SingletonProvider.GetAll())
            {
                var content     = ContentGetter.Get <IContent>(singleton.Id, null);
                var contentType = ContentTypeProvider.Get(singleton.ContentTypeId);

                if (content != null)
                {
                    if (content.ContentTypeId != contentType.Id)
                    {
                        throw new SingletonWithIdIsOfWrongType(singleton.Id, contentType, content.GetType(), content.ContentTypeId);
                    }

                    continue;
                }

                content = (IContent)Activator.CreateInstance(contentType.Type);

                content.Id = singleton.Id;

                ContentInserter.Insert(content);
            }
        }
Beispiel #21
0
        public async Task <IContent> GetAsync(string id, string contentTypeId)
        {
            var contentType = ContentTypeProvider.Get(contentTypeId);

            return(await ContainerSpecificContentGetter.GetAsync(id, null, contentType.Container));
        }
        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"));
            }
        }