Example #1
0
        public static async Task <ContentOperationContext> CreateAsync(
            IContentRepository contentRepository,
            ContentDomainObject content,
            ContentCommand command,
            IAppProvider appProvider,
            IAssetRepository assetRepository,
            IScriptEngine scriptEngine,
            Func <string> message)
        {
            var a = content.Snapshot.AppId;
            var s = content.Snapshot.SchemaId;

            if (command is CreateContent createContent)
            {
                a = a ?? createContent.AppId;
                s = s ?? createContent.SchemaId;
            }

            var(appEntity, schemaEntity) = await appProvider.GetAppWithSchemaAsync(a.Id, s.Id);

            var context = new ContentOperationContext
            {
                appEntity         = appEntity,
                assetRepository   = assetRepository,
                contentRepository = contentRepository,
                content           = content,
                command           = command,
                message           = message,
                schemaEntity      = schemaEntity,
                scriptEngine      = scriptEngine
            };

            return(context);
        }
        public static async Task <ContentOperationContext> CreateAsync(
            Guid appId,
            Guid schemaId,
            ContentCommand command,
            IAppProvider appProvider,
            IAssetRepository assetRepository,
            IContentRepository contentRepository,
            IScriptEngine scriptEngine,
            Func <string> message)
        {
            var(appEntity, schemaEntity) = await appProvider.GetAppWithSchemaAsync(appId, schemaId);

            var context = new ContentOperationContext
            {
                appEntity         = appEntity,
                assetRepository   = assetRepository,
                command           = command,
                contentRepository = contentRepository,
                message           = message,
                schemaId          = schemaId,
                schemaEntity      = schemaEntity,
                scriptEngine      = scriptEngine
            };

            return(context);
        }
Example #3
0
        public static async Task <ContentOperationContext> CreateAsync(
            IContentRepository contentRepository,
            ContentDomainObject content,
            ContentCommand command,
            IAppProvider appProvider,
            ISchemaProvider schemas,
            IScriptEngine scriptEngine,
            IAssetRepository assetRepository,
            Func <string> message)
        {
            var taskForApp    = appProvider.FindAppByIdAsync(command.AppId.Id);
            var taskForSchema = schemas.FindSchemaByIdAsync(command.SchemaId.Id);

            await Task.WhenAll(taskForApp, taskForSchema);

            var context = new ContentOperationContext();

            context.appEntity         = taskForApp.Result;
            context.assetRepository   = assetRepository;
            context.contentRepository = contentRepository;
            context.content           = content;
            context.command           = command;
            context.message           = message;
            context.schemaEntity      = taskForSchema.Result;
            context.scriptEngine      = scriptEngine;

            return(context);
        }
        private ContentCommand RegisterWebContent(string webAppRootDir, IFileCache fileCache, IWebServerConfiguration config)
        {
            string contentRootDirectory = Path.GetFullPath(Path.Combine(webAppRootDir, "Content"));

            ContentCommand contentCommand = new ContentCommand(contentRootDirectory, FileSystem, fileCache);

            contentCommand.CacheByMaxAge(@"cached-by-max-age.txt", TimeSpan.FromDays(30));
            contentCommand.CacheByETag(@"cached-by-etag.txt", TimeSpan.FromDays(10), () => GenerateContentETag(config));
            return(contentCommand);
        }
        private async Task <CommandContext> HandleAsync(ContentCommand command)
        {
            command.AppId    = appId;
            command.SchemaId = schemaId;

            var commandContext = new CommandContext(command, A.Fake <ICommandBus>());

            await sut.HandleAsync(commandContext);

            return(commandContext);
        }
Example #6
0
        public static void CheckPermission(IContentEntity content, ContentCommand command, params string[] permissions)
        {
            if (Equals(content.CreatedBy, command.Actor) || command.User == null)
            {
                return;
            }

            if (permissions.All(x => !command.User.Allows(x, content.AppId.Name, content.SchemaId.Name)))
            {
                throw new DomainForbiddenException(T.Get("common.errorNoPermission"));
            }
        }
Example #7
0
        public static void CheckPermission(IContentEntity content, ContentCommand command, string permission)
        {
            if (content.CreatedBy?.Equals(command.Actor) == true)
            {
                return;
            }

            if (!command.User.Allows(permission, content.AppId.Name, content.SchemaId.Name))
            {
                throw new DomainForbiddenException(T.Get("common.errorNoPermission"));
            }
        }
Example #8
0
        private async Task <ContentOperationContext> CreateContext(ContentCommand command, Func <string> message)
        {
            var operationContext =
                await ContentOperationContext.CreateAsync(command, Snapshot,
                                                          contentRepository,
                                                          appProvider,
                                                          assetRepository,
                                                          scriptEngine,
                                                          message);

            return(operationContext);
        }
Example #9
0
        public async Task <ContentDto> CreateContentAsync(ContentCommand contentCommand)
        {
            var contentToCreate = _mapper.Map <Content>(contentCommand);

            var createResult = await _contentRepository.CreateContentAsync(contentToCreate);

            if (!createResult)
            {
                _logger.LogError($"Content {JsonConvert.SerializeObject(contentToCreate)} failed to create.");
                return(null);
            }

            return(_mapper.Map <ContentDto>(contentToCreate));
        }
Example #10
0
        private async Task <ContentOperationContext> CreateContext(ContentCommand command, ContentDomainObject content, Func <string> message)
        {
            var operationContext =
                await ContentOperationContext.CreateAsync(
                    contentRepository,
                    content,
                    command,
                    appProvider,
                    schemas,
                    scriptEngine,
                    assetRepository,
                    message);

            return(operationContext);
        }
Example #11
0
        public void Setup()
        {
            fileSystem = MockRepository.GenerateStub <IFileSystem>();
            fileCache  = null;
            cmd        = new ContentCommand(ContentRootDirectory, fileSystem, fileCache);

            IWebServerConfiguration webServerConfiguration = MockRepository.GenerateStub <IWebServerConfiguration>();

            now         = new DateTime(2014, 06, 18, 7, 11, 0);
            timeService = MockRepository.GenerateStub <ITimeService>();

            context = new FakeWebContext(null, null, fileSystem, timeService, webServerConfiguration);

            routeMatch = new WebRequestRouteMatch();
        }
Example #12
0
        public async Task <IActionResult> CreateContent([FromBody] ContentCommand command)
        {
            var existingContent = await _contentService.ContentExistAsync(command.ContentName);

            if (existingContent)
            {
                return(BadRequest("There is already a content with a same key"));
            }

            var createdContent = await _contentService.CreateContentAsync(command);

            if (createdContent)
            {
                return(BadRequest("Resource failed to create."));
            }

            var resourceUri = _uriService.GetContentUri(createdContent.ContentName);

            return(Created(resourceUri, createdContent));
        }
        public void Add_Content_Return_Same_Content()
        {
            //Arrange
            var createCommand = new ContentCommand
            {
                ContentName   = "Foo:Posts",
                ContentFields = new Dictionary <string, string>
                {
                    { "Article", "Hello World" },
                    { "Body", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor" }
                }
            };
            var readQuery = new ContentQuery
            {
                ContentName = "Foo:Posts"
            };

            var expectedContent = new ContentDto
            {
                ContentName   = "Foo:Posts",
                ContentFields = new Dictionary <string, string>
                {
                    { "Article", "Hello World" },
                    { "Body", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor" }
                }
            };

            var contentService = GetContentService();
            var compareLogic   = new CompareLogic();

            //Act
            contentService.CreateContentAsync(createCommand).Wait();

            var retrievedContent = contentService.GetContentAsync(readQuery).Result;

            //Assert
            var compareResult = compareLogic.Compare(expectedContent, retrievedContent);

            Assert.IsTrue(compareResult.AreEqual,
                          $"Added and retrieved roles aren't identical differences are {string.Join(",", compareResult.Differences)}");
        }
Example #14
0
        public async Task LoadAsync(NamedId <Guid> appId, NamedId <Guid> schemaId, ContentCommand command, Func <string> message, bool optimized)
        {
            var(app, schema) = await appProvider.GetAppWithSchemaAsync(appId.Id, schemaId.Id);

            if (app == null)
            {
                throw new InvalidOperationException("Cannot resolve app.");
            }

            if (schema == null)
            {
                throw new InvalidOperationException("Cannot resolve schema.");
            }

            this.app     = app;
            this.schema  = schema;
            this.command = command;
            this.message = message;

            validationContext = new ValidationContext(appId, schemaId, schema.SchemaDef, command.ContentId).Optimized(optimized);
        }
        public SyborgTestHttpModuleAppHost()
        {
            IFileSystem               fileSystem          = new WindowsFileSystem();
            IApplicationInfo          applicationInfo     = new ApplicationInfo();
            ITimeService              timeService         = new RealTimeService();
            IRazorCompiler            razorCompiler       = new InMemoryRazorCompiler();
            IRazorViewRenderingEngine viewRenderingEngine = new RazorViewRenderingEngine(fileSystem, razorCompiler);

            IWebServerConfiguration config = new WebServerConfiguration();

            FileMimeTypesMap fileMimeTypesMap = new FileMimeTypesMap().RegisterStandardMimeTypes();

            Initialize(config, fileSystem, applicationInfo, timeService, fileMimeTypesMap, viewRenderingEngine);

            IFileCache fileCache = new FileCache();

            string webAppRootDir;

            if (!WebServerConfiguration.WebServerDevelopmentMode)
            {
#if NCRUNCH
                webAppRootDir = @"D:\hg\ScalableMaps\WebApp\ScalableMaps\ScalableMaps.Web2";
#else
                webAppRootDir = ApplicationInfo.GetAppDirectoryPath("..");
#endif
            }
            else
            {
                webAppRootDir = ApplicationInfo.GetAppDirectoryPath("..");
            }

            ContentCommand    contentCommand = RegisterWebContent(webAppRootDir, fileCache, config);
            TestStreamCommand streamCommand  = new TestStreamCommand();

            AddRoute(new RegexWebRequestRoute("^Content/(?<path>.+)$", HttpMethod.GET, contentCommand));
            AddRoute(new RegexWebRequestRoute("^stream/(?<path>.+)$", HttpMethod.GET, streamCommand));

            AddPolicies(new IWebPolicy[] { new SecureResponseHeadersPolicy() });
        }
Example #16
0
        public void Setup()
        {
            IFileSystem      fileSystem      = new WindowsFileSystem();
            IApplicationInfo applicationInfo = new ApplicationInfo();
            ITimeService     timeService     = new RealTimeService();

            ISignal serverStopSignal = new ManualResetSignal(false);

            IWebServerConfiguration configuration = new WebServerConfiguration();

            IRazorCompiler            razorCompiler       = new InMemoryRazorCompiler();
            IRazorViewRenderingEngine viewRenderingEngine = new RazorViewRenderingEngine(fileSystem, razorCompiler);

            IWebServerController webServerController = new WebServerController(serverStopSignal);
            IFileMimeTypesMap    fileMimeTypesMap    = new FileMimeTypesMap();
            IFileCache           fileCache           = new FileCache();

            List <IWebRequestRoute> routes      = new List <IWebRequestRoute>();
            string         contentRootDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "sample-content");
            ContentCommand contentCommand       = new ContentCommand(contentRootDirectory, fileSystem, fileCache);

            routes.Add(new RegexWebRequestRoute("^content/(?<path>.+)$", HttpMethod.GET, contentCommand));

            // ReSharper disable once CollectionNeverUpdated.Local
            List <IWebPolicy> policies = new List <IWebPolicy>();

            const string ExternalUrl = "http://localhost";
            const int    Port        = 12345;

            testServiceUrl = "{0}:{1}/".Fmt(ExternalUrl, Port);

            host = new TestHost(
                configuration, ExternalUrl, Port, null, fileSystem, applicationInfo, timeService, viewRenderingEngine, fileMimeTypesMap, webServerController, routes, policies);
            host.Start();

            IWebConfiguration webConfiguration = new WebConfiguration("Syborg.Tests");

            restClientFactory = new RestClientFactory(webConfiguration);
        }
Example #17
0
        public async Task LoadAsync(NamedId <DomainId> appId, NamedId <DomainId> schemaId, ContentCommand command, bool optimized)
        {
            this.command = command;

            var(app, schema) = await appProvider.GetAppWithSchemaAsync(appId.Id, schemaId.Id);

            if (app == null)
            {
                throw new DomainObjectNotFoundException(appId.ToString());
            }

            this.app = app;

            if (schema == null)
            {
                throw new DomainObjectNotFoundException(schemaId.ToString());
            }

            this.schema = schema;

            validationContext = new ValidationContext(appId, schemaId, schema.SchemaDef, command.ContentId).Optimized(optimized);
        }
Example #18
0
        public static async Task <ContentOperationContext> CreateAsync(
            Guid appId,
            Guid schemaId,
            ContentCommand command,
            IAppProvider appProvider,
            IAssetRepository assetRepository,
            IContentRepository contentRepository,
            IScriptEngine scriptEngine,
            Func <string> message)
        {
            var(appEntity, schemaEntity) = await appProvider.GetAppWithSchemaAsync(appId, schemaId);

            if (appEntity == null)
            {
                throw new InvalidOperationException("Cannot resolve app.");
            }

            if (schemaEntity == null)
            {
                throw new InvalidOperationException("Cannot resolve schema.");
            }

            var context = new ContentOperationContext
            {
                appEntity         = appEntity,
                assetRepository   = assetRepository,
                command           = command,
                contentRepository = contentRepository,
                message           = message,
                schemaId          = schemaId,
                schemaEntity      = schemaEntity,
                scriptEngine      = scriptEngine
            };

            return(context);
        }
Example #19
0
        public static async Task <ContentOperationContext> CreateAsync(
            IContentRepository contentRepository,
            ContentDomainObject content,
            ContentCommand command,
            IAppProvider appProvider,
            IAssetRepository assetRepository,
            IScriptEngine scriptEngine,
            Func <string> message)
        {
            var(appEntity, schemaEntity) = await appProvider.GetAppWithSchemaAsync(command.AppId.Name, command.SchemaId.Id);

            var context = new ContentOperationContext();

            context.appEntity         = appEntity;
            context.assetRepository   = assetRepository;
            context.contentRepository = contentRepository;
            context.content           = content;
            context.command           = command;
            context.message           = message;
            context.schemaEntity      = schemaEntity;
            context.scriptEngine      = scriptEngine;

            return(context);
        }
Example #20
0
        private void Parse()
        {
            this.commandNameEndIndex = this.GetCommandNameEndIndex();

            this.Name = this.ParseName();
            this.Parameters = this.ParseParameters();
            this.TrimParams();

            this.Type = this.ParseCommandType(this.Name);
        }
Example #21
0
        private async Task <object?> PublishAsync(ContentCommand command)
        {
            var result = await sut.ExecuteAsync(CreateContentCommand(command));

            return(result);
        }
 private static ScriptContext CreateScriptContext(object operation, ContentCommand command, NamedContentData data, NamedContentData oldData)
 {
     return(new ScriptContext {
         ContentId = command.ContentId, OldData = oldData, Data = data, User = command.User, Operation = operation.ToString()
     });
 }
Example #23
0
 private Task LoadContext(ContentCommand command, bool optimize)
 {
     return(context.LoadAsync(command.AppId, command.SchemaId, command, optimize));
 }
Example #24
0
 public void ProposeUpdate(ContentCommand command, NamedContentData data)
 {
     RaiseEvent(SimpleMapper.Map(command, new ContentUpdateProposed {
         Data = data
     }));
 }
Example #25
0
 private void CancelChangeStatus(ContentCommand command)
 {
     Raise(command, new ContentSchedulingCancelled());
 }
Example #26
0
 private void Update(ContentCommand command, ContentData data)
 {
     Raise(command, new ContentUpdated {
         Data = data
     });
 }
        public Task ExecuteScriptAsync(Func <ISchemaEntity, string> script, object operation, ContentCommand command, NamedContentData data, NamedContentData oldData = null)
        {
            var ctx = CreateScriptContext(operation, command, data, oldData);

            scriptEngine.Execute(ctx, script(schemaEntity));

            return(TaskHelper.Done);
        }
        public Task <NamedContentData> ExecuteScriptAndTransformAsync(Func <ISchemaEntity, string> script, object operation, ContentCommand command, NamedContentData data, NamedContentData oldData = null)
        {
            var ctx = CreateScriptContext(operation, command, data, oldData);

            var result = scriptEngine.ExecuteAndTransform(ctx, script(schemaEntity));

            return(Task.FromResult(result));
        }