A view component

This commit is contained in:
2016-11-09 14:03:57 +01:00
parent bd39e900ef
commit 207b0e8664
11 changed files with 258 additions and 190 deletions

View File

@ -1,19 +1,66 @@
using System;
using System.IO;
using System.Security.Claims;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
using System.Threading.Tasks;
using System.Web.Routing;
using Microsoft.AspNet.Mvc.ViewComponents;
using Microsoft.AspNet.Razor;
namespace Yavsc.ApiControllers
{
using Models;
[Route("api/pdfestimate"), Authorize]
public class PdfEstimateController : Controller
{
[HttpGet("{id}", Name = "Get")]
ApplicationDbContext dbContext;
DefaultViewComponentHelper helper;
IViewComponentDescriptorCollectionProvider provider;
IViewComponentInvokerFactory factory;
RazorEngineHost host;
RazorTemplateEngine engine;
IViewComponentSelector selector;
public PdfEstimateController(
IViewComponentDescriptorCollectionProvider provider,
IViewComponentSelector selector,
IViewComponentInvokerFactory factory,
ApplicationDbContext context)
{
this.selector = selector;
this.provider = provider;
this.factory = factory;
helper = new DefaultViewComponentHelper(provider, selector, factory);
dbContext = context;
var language = new CSharpRazorCodeLanguage();
host = new RazorEngineHost(language)
{
DefaultBaseClass = "RazorPage",
DefaultClassName = "Estimate",
DefaultNamespace = "Yavsc",
};
// Everyone needs the System namespace, right?
host.NamespaceImports.Add("System");
engine = new RazorTemplateEngine(host);
/*
GeneratorResults razorResult =
engine.GenerateCode(
) */
}
[HttpGet("get/{id}", Name = "Get"), Authorize]
public IActionResult Get(long id)
{
var filename = $"estimate-{id}.pdf";
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
@ -23,15 +70,43 @@ namespace Yavsc.ApiControllers
// the browser to try to show the file inline
Inline = false,
};
FileInfo fi = new FileInfo(Path.Combine(Startup.UserBillsDirName,filename));
FileStreamResult result = null;
var s = fi.OpenRead();
result = File(s,"application/x-pdf",filename);
return result;
FileInfo fi = new FileInfo(Path.Combine(Startup.UserBillsDirName, filename));
if (!fi.Exists) return Ok(new { Error = "Not generated" });
return File(fi.OpenRead(), "application/x-pdf", filename); ;
}
[HttpGet("GetComponents", Name = "GetComponents")]
public IActionResult GetComponents()
{
return Ok(provider);
}
[HttpGet("estimate-{id}.tex", Name = "GetTex"), Authorize]
public IActionResult GetTex(long id)
{
Response.ContentType = "text/x-tex";
return ViewComponent("Estimate",new object[] { id, false });
}
[HttpGet("gen/{id}")]
public async Task<IActionResult> GeneratePdf(long id)
{
/*
using (TextWriter wr = new StringWriter()) {
ViewComponentContext ctx = new ViewComponentContext(
selector.SelectComponent("Estimate"), new object[]{id},
new ViewContext(),
wr
);
}
*/
return ViewComponent("Estimate",new object[] { id, true } );
}
}
}