ln.templates/ln.templates.service/HttpTemplateEndpoints.cs

101 lines
4.1 KiB
C#

using System;
using System.IO;
using Jint;
using Jint.Native;
using Jint.Native.Json;
using Jint.Native.Object;
using Jint.Runtime;
using ln.http;
using ln.json;
using ln.json.mapping;
namespace ln.templates.service
{
public class HttpTemplateEndpoints : HttpEndpointController
{
private TemplateService _templateService;
public HttpTemplateEndpoints(TemplateService templateService)
{
_templateService = templateService;
}
[Map(HttpMethod.GET, "/templates$")]
public HttpResponse ListTemplates()
{
JSONArray templates = JSONMapper.DefaultMapper.ToJson(_templateService.ListTemplates()) as JSONArray;
return HttpResponse.OK().Content(templates);
}
[Map(HttpMethod.GET, "/templates/(?<templatePath>.*\\.html)$")]
public HttpResponse GetTemplate(string templatePath)
{
if (_templateService.GetTemplateSource(templatePath, out Stream sourceStream))
return HttpResponse.OK().Content(sourceStream);
return HttpResponse.NotFound();
}
[Map(HttpMethod.POST, "/templates/(?<templatePath>.*\\.html)$")]
public HttpResponse PostTemplate(
string templatePath,
[HttpArgumentSource(HttpArgumentSource.HEADER,ArgumentName = "content-type")]string contentType,
[HttpArgumentSource(HttpArgumentSource.CONTENT)]Stream sourceStream)
{
if (contentType.Equals("text/html"))
{
if (_templateService.StoreTemplate(templatePath, sourceStream))
return HttpResponse.NoContent();
} else if (contentType.Equals("application/json"))
{
if (_templateService.StoreTemplateMetadata(templatePath, sourceStream))
return HttpResponse.NoContent();
}
return HttpResponse.BadRequest();
}
[Map(HttpMethod.POST, "/render/(?<templatePath>.*)$")]
[Map(HttpMethod.GET, "/render/(?<templatePath>.*)$")]
public HttpResponse RenderTemplate(
[HttpArgumentSource(HttpArgumentSource.PARAMETER)]string templatePath,
[HttpArgumentSource(HttpArgumentSource.CONTENT)]string postContent = "{}"
)
{
ObjectInstance jsonObject = new JsonParser(new Engine()).Parse(postContent) as ObjectInstance;
var templateStream = _templateService.RenderTemplate(templatePath, jsonObject);
if (templateStream)
return HttpResponse.OK().ContentType("text/html").Content(templateStream.Value);
if (templateStream.Is(out FileNotFoundException e))
return HttpResponse.NotFound().Content(e.FileName);
if (templateStream.Is(out JavaScriptException jse))
return HttpResponse.BadRequest()
.Content(String.Format("JavaScript exception: {0}", jse.Error.ToString()));
return HttpResponse.InternalServerError();
}
[Map(HttpMethod.POST, "/pdf/(?<templatePath>.*)$")]
[Map(HttpMethod.GET, "/pdf/(?<templatePath>.*)$")]
public HttpResponse RenderTemplatePDF(
[HttpArgumentSource(HttpArgumentSource.PARAMETER)]string templatePath,
[HttpArgumentSource(HttpArgumentSource.CONTENT)]string postContent = "{}"
)
{
ObjectInstance jsonObject = new JsonParser(new Engine()).Parse(postContent) as ObjectInstance;
var pdfStream = _templateService.RenderPDF(templatePath, jsonObject);
if (pdfStream)
return HttpResponse.OK().ContentType("application/pdf").Content(pdfStream.Value);
if (pdfStream.Is(out FileNotFoundException e))
return HttpResponse.NotFound().Content(e.FileName);
if (pdfStream.Is(out JavaScriptException jse))
return HttpResponse.BadRequest()
.Content(String.Format("JavaScript exception: {0}", jse.Error.ToString()));
return HttpResponse.InternalServerError();
}
}
}