83 lines
3.3 KiB
C#
83 lines
3.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using Jint;
|
|
using Jint.Native;
|
|
using Jint.Native.Json;
|
|
using Jint.Native.Object;
|
|
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;
|
|
if (_templateService.RenderTemplate(templatePath, jsonObject, out Stream templateStream))
|
|
return HttpResponse.OK().ContentType("text/html").Content(templateStream);
|
|
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;
|
|
if (_templateService.RenderPDF(templatePath, jsonObject, out Stream templateStream))
|
|
return HttpResponse.OK().ContentType("application/pdf").Content(templateStream);
|
|
return HttpResponse.InternalServerError();
|
|
}
|
|
|
|
}
|
|
} |