Example #1
0
        public async Task InsertAsync(IContent content)
        {
            if (content.Id == null)
            {
                throw new InvalidOperationException($"This content has no Id. Inserting (sorry for the confusing terminology) is for forcing newly created content to have a set Id. Did you mean to use IContentCreator?");
            }

            var contentType = ContentTypeRepository.Get(content.GetType());

            if (contentType == null)
            {
                throw new InvalidOperationException($"This content has no content type (or rather its Type ({content.GetType()}) has no [ContentType] attribute)");
            }

            content.ContentTypeId = contentType.Id;

            foreach (var saveListener in SaveListenerProvider.GetFor(content))
            {
                saveListener.BeforeSave(content);
            }

            await DocumentCreator.Create(ContainerConstants.Content, ContentSerializer.Serialize(content, contentType));
        }
Example #2
0
        public async Task UpdateAsync(IContent content)
        {
            if (content.Id == null)
            {
                throw new InvalidOperationException($"This content cannot be updated as it doesn't seem to exist (Id is null). Did you mean to use IContentCreator?");
            }

            var contentType = ContentTypeRepository.Get(content.ContentTypeId);

            if (contentType == null)
            {
                throw new TypeNotRegisteredContentTypeException(content.GetType());
            }

            foreach (var saveListener in SaveListenerProvider.GetFor(content))
            {
                saveListener.BeforeSave(content);
            }

            var document = ContentSerializer.Serialize(content, contentType);

            await DocumentUpdater.UpdateAsync(contentType.Container, content.Id, document);
        }
Example #3
0
        public async Task CreateAsync(IContent content)
        {
            if (content.Id != null)
            {
                throw new InvalidOperationException($"This content seems to already exist as it has a non-null Id ({content.Id}). Did you mean to use IContentUpdater?");
            }

            var contentType = ContentTypeRepository.Get(content.GetType());

            if (contentType == null)
            {
                throw new TypeNotRegisteredContentTypeException(content.GetType());
            }

            content.Id            = IdGenerator.Generate();
            content.ContentTypeId = contentType.Id;

            foreach (var saveListener in SaveListenerProvider.GetFor(content))
            {
                saveListener.BeforeSave(content);
            }

            await DocumentCreator.Create(contentType.Container, ContentSerializer.Serialize(content, contentType));
        }