Add history inserts, add controllers
This commit is contained in:
90
EnvelopeGenerator.Web/Controllers/DocumentController.cs
Normal file
90
EnvelopeGenerator.Web/Controllers/DocumentController.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using DigitalData.Modules.Logging;
|
||||
using EnvelopeGenerator.Common;
|
||||
using EnvelopeGenerator.Web.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Reflection.Metadata;
|
||||
using static EnvelopeGenerator.Web.Handler.FileHandler;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Controllers
|
||||
{
|
||||
public class DocumentController : Controller
|
||||
{
|
||||
private readonly DatabaseService database;
|
||||
private readonly LoggingService logging;
|
||||
private readonly Logger logger;
|
||||
private readonly ApiService api;
|
||||
|
||||
public DocumentController(DatabaseService database, LoggingService logging, ApiService api)
|
||||
{
|
||||
this.database = database;
|
||||
this.logging = logging;
|
||||
this.logger = logging.LogConfig.GetLoggerFor(GetType().Name);
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("api/document/{envelopeKey}")]
|
||||
[IgnoreAntiforgeryToken]
|
||||
public async Task<IActionResult> Get(string envelopeKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.Info("Handling file download.");
|
||||
|
||||
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
|
||||
|
||||
var Request = ControllerContext.HttpContext.Request;
|
||||
var document = api.GetDocument(Request, envelopeKey);
|
||||
|
||||
// Load the document from disk
|
||||
var bytes = await System.IO.File.ReadAllBytesAsync(document.Filepath);
|
||||
logger.Info("Serving file, size: [{0}]", bytes.Length);
|
||||
|
||||
// Return the document as bytes
|
||||
return File(bytes, "application/octet-stream");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Better error handling & reporting
|
||||
logger.Error(e);
|
||||
return Problem(
|
||||
statusCode: 500,
|
||||
detail: e.Message,
|
||||
type: ErrorType.ServerError.ToString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/document/{envelopeKey}")]
|
||||
[IgnoreAntiforgeryToken]
|
||||
public async Task<IActionResult> Update(string envelopeKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.Info("Handling file update.");
|
||||
|
||||
api.EnsureValidEnvelopeKey(envelopeKey);
|
||||
|
||||
var Request = ControllerContext.HttpContext.Request;
|
||||
var document = api.GetDocument(Request, envelopeKey);
|
||||
|
||||
if (!await api.UpdateDocument(Request.Body, document.Filepath))
|
||||
{
|
||||
throw new IOException("Document could not be saved to disk!");
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Better error handling & reporting
|
||||
logger.Error(e);
|
||||
return Problem(
|
||||
statusCode: 500,
|
||||
detail: e.Message,
|
||||
type: ErrorType.ServerError.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user