yavsc/Yavsc/ApiControllers/PdfEstimateController.cs

119 lines
4.3 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-12-05 05:57:07 +01:00
using System;
2016-12-07 00:09:12 +01:00
using System.Security.Claims;
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);
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
return new ChallengeResult();
}
2016-12-05 05:57:07 +01:00
2016-11-07 19:34:56 +01:00
var filename = $"estimate-{id}.pdf";
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]
2016-12-05 05:57:07 +01:00
public async Task<IActionResult> GetTex(long id)
2016-11-09 14:03:57 +01:00
{
2016-12-05 05:57:07 +01:00
var estimate = dbContext.Estimates.Include(
e=>e.Query
).FirstOrDefault(e=>e.Id == id);
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
return new ChallengeResult();
}
2016-11-09 14:03:57 +01:00
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-12-05 05:57:07 +01:00
public async Task<IActionResult> GeneratePdf(long id)
2016-11-09 14:03:57 +01:00
{
2016-12-05 05:57:07 +01:00
var estimate = dbContext.Estimates.Include(
e=>e.Query
).FirstOrDefault(e=>e.Id == id);
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
return new ChallengeResult();
}
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}")]
2016-12-05 05:57:07 +01:00
public async Task<IActionResult> ProSign(long id)
2016-12-01 17:42:34 +01:00
{
2016-12-07 00:09:12 +01:00
var uid = User.GetUserId();
2016-12-05 05:57:07 +01:00
var estimate = dbContext.Estimates.Include(
e=>e.Query
2016-12-07 00:09:12 +01:00
).FirstOrDefault(e=>e.Id == id && e.OwnerId == uid );
2016-12-05 05:57:07 +01:00
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
return new ChallengeResult();
}
2016-12-01 17:42:34 +01:00
if (Request.Form.Files.Count!=1)
return new BadRequestResult();
2016-12-07 00:09:12 +01:00
User.ReceiveSignature(id,Request.Form.Files[0],"pro");
2016-12-05 05:57:07 +01:00
estimate.ProviderValidationDate = DateTime.Now;
dbContext.SaveChanges();
return Ok (new { ProviderValidationDate = estimate.ProviderValidationDate });
2016-12-01 17:42:34 +01:00
}
2016-12-07 00:09:12 +01:00
[HttpPost("clisign/{id}")]
public async Task<IActionResult> CliSign(long id)
{
var uid = User.GetUserId();
var estimate = dbContext.Estimates.Include( e=>e.Query
).FirstOrDefault( e=> e.Id == id && e.Query.ClientId == uid );
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
return new ChallengeResult();
}
if (Request.Form.Files.Count!=1)
return new BadRequestResult();
User.ReceiveSignature(id,Request.Form.Files[0],"cli");
estimate.ClientValidationDate = DateTime.Now;
dbContext.SaveChanges();
return Ok (new { ClientValidationDate = estimate.ClientValidationDate });
}
2016-11-07 19:34:56 +01:00
}
}