yavsc/Yavsc/ApiControllers/PdfEstimateController.cs

84 lines
2.7 KiB
C#
Raw Normal View History

2016-11-07 19:34:56 +01:00
using System.IO;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
2016-11-09 14:03:57 +01:00
using System.Web.Routing;
2016-11-07 19:34:56 +01:00
namespace Yavsc.ApiControllers
{
2016-11-09 14:03:57 +01:00
using Models;
2016-12-01 17:42:34 +01:00
using Helpers;
2016-12-02 13:22:39 +01:00
using System.Linq;
using Microsoft.Data.Entity;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
2016-11-09 14:03:57 +01:00
2016-11-07 19:34:56 +01:00
[Route("api/pdfestimate"), Authorize]
public class PdfEstimateController : Controller
{
2016-11-09 14:03:57 +01:00
ApplicationDbContext dbContext;
2016-12-02 13:22:39 +01:00
private IAuthorizationService authorizationService;
private ILogger logger;
2016-11-09 14:03:57 +01:00
public PdfEstimateController(
2016-12-02 13:22:39 +01:00
IAuthorizationService authorizationService,
ILoggerFactory loggerFactory,
2016-11-09 14:03:57 +01:00
ApplicationDbContext context)
{
2016-12-02 13:22:39 +01:00
this.authorizationService = authorizationService;
2016-11-09 14:03:57 +01:00
dbContext = context;
2016-12-02 13:22:39 +01:00
logger = loggerFactory.CreateLogger<PdfEstimateController>();
2016-11-09 14:03:57 +01:00
}
[HttpGet("get/{id}", Name = "Get"), Authorize]
2016-12-02 13:22:39 +01:00
public async Task<IActionResult> Get(long id)
2016-11-07 19:34:56 +01:00
{
2016-12-02 13:22:39 +01:00
var estimate = dbContext.Estimates.Include(
e=>e.Query
).FirstOrDefault(e=>e.Id == id);
logger.LogWarning($"#######ESTIMATE OWNER ID {estimate.OwnerId} ########");
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
return new ChallengeResult();
}
2016-11-07 19:34:56 +01:00
var filename = $"estimate-{id}.pdf";
2016-11-09 14:03:57 +01:00
2016-11-07 19:34:56 +01:00
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = filename,
// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
2016-11-09 14:03:57 +01:00
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("estimate-{id}.tex", Name = "GetTex"), Authorize]
public IActionResult GetTex(long id)
{
Response.ContentType = "text/x-tex";
2016-11-30 11:01:22 +01:00
return ViewComponent("Estimate",new object[] { id, "LaTeX" });
2016-11-09 14:03:57 +01:00
}
[HttpPost("gen/{id}")]
2016-11-24 01:01:41 +01:00
public IActionResult GeneratePdf(long id)
2016-11-09 14:03:57 +01:00
{
2016-11-30 11:01:22 +01:00
return ViewComponent("Estimate",new object[] { id, "Pdf" } );
2016-11-07 19:34:56 +01:00
}
2016-12-01 17:42:34 +01:00
[HttpPost("prosign/{id}")]
public IActionResult ProSign(long id)
{
if (Request.Form.Files.Count!=1)
return new BadRequestResult();
return Ok (User.ReceiveProSignature(id,Request.Form.Files[0]));
}
2016-11-07 19:34:56 +01:00
}
}