Exemplo n.º 1
0
        private async Task PerformSave(CargoEngine cargoEngine, IDictionary <string, object> environment, CancellationToken cancellationToken)
        {
            int itemsWritten = 0;

            var request = ReadJsonFromRequest(environment) as JObject;

            if (request != null)
            {
                var items = request.Properties()
                            .Select(x => new { id = x.Name, val = ((x.Value as JObject)?.Property("content")?.Value as JValue)?.Value as string })
                            .Where(x => x.id != null && x.val != null)
                            .ToDictionary(x => x.id, x => x.val);

                if (items.Count > 0)
                {
                    using (var ds = cargoEngine.CreateDataSource())
                    {
                        ds.SetById(items);
                        itemsWritten = items.Count;
                    }
                }
            }

            await WriteObject(environment, new { message = $"saved {itemsWritten} items" }, cancellationToken);
        }
Exemplo n.º 2
0
        private async Task PerformImport(CargoEngine cargoEngine, IDictionary <string, object> environment, CancellationToken cancellationToken)
        {
            var request = ReadObjectFromRequest <List <ContentItem> >(environment);

            using (var ds = cargoEngine.CreateDataSource())
            {
                ds.Set(request);
                await WriteObject(environment, new { message = "ok" }, cancellationToken);
            }
        }
Exemplo n.º 3
0
 private async Task HandleGetAsync(CargoEngine cargoEngine, IDictionary <string, object> environment, bool onlyHead, string strippedPath, CancellationToken cancellationToken)
 {
     if (strippedPath == "/js")
     {
         await WriteFromResource(environment, "cargo.js", "application/json", cancellationToken, TimeSpan.FromDays(10));
     }
     else if (strippedPath == "/css")
     {
         await WriteFromResource(environment, "cargo.css", "text/css", cancellationToken, TimeSpan.FromDays(10));
     }
     else if (strippedPath == "/export")
     {
         using (var ds = cargoEngine.CreateDataSource())
         {
             await WriteObject(environment, ds.GetAllContent(), cancellationToken);
         }
     }
     else
     {
         await Return404Async(environment);
     }
 }