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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
98
EnvelopeGenerator.Web/Controllers/EnvelopeController.cs
Normal file
98
EnvelopeGenerator.Web/Controllers/EnvelopeController.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using EnvelopeGenerator.Common;
|
||||
using EnvelopeGenerator.Web.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NLog;
|
||||
using static EnvelopeGenerator.Web.Handler.FileHandler;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Controllers
|
||||
{
|
||||
public class EnvelopeController : Controller
|
||||
{
|
||||
private readonly DatabaseService database;
|
||||
private readonly LoggingService logging;
|
||||
private readonly Logger logger;
|
||||
private readonly ApiService api;
|
||||
|
||||
public EnvelopeController(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/envelope/{envelopeKey}")]
|
||||
[IgnoreAntiforgeryToken]
|
||||
public IActionResult Get(string envelopeKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.Info("Handling envelope loading.");
|
||||
|
||||
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
|
||||
|
||||
// Return the envelope and additional data as json
|
||||
return Json(r);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Better error handling & reporting
|
||||
logger.Error(e);
|
||||
return Problem(
|
||||
statusCode: 500,
|
||||
detail: e.Message,
|
||||
type: ErrorType.ServerError.ToString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/envelope/{envelopeKey}")]
|
||||
[IgnoreAntiforgeryToken]
|
||||
public async Task<IActionResult> Update(string envelopeKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.Info("Handling envelope saving.");
|
||||
|
||||
api.EnsureValidEnvelopeKey(envelopeKey);
|
||||
|
||||
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
|
||||
|
||||
var Request = ControllerContext.HttpContext.Request;
|
||||
var document = api.GetDocument(Request, envelopeKey);
|
||||
|
||||
string? annotationData = await api.EnsureValidAnnotationData(Request);
|
||||
|
||||
if (annotationData == null)
|
||||
{
|
||||
throw new ArgumentNullException("AnnotationData");
|
||||
}
|
||||
|
||||
State state = api.GetState(logging.LogConfig, database.MSSQL);
|
||||
DocumentStatusModel model = new(state);
|
||||
|
||||
model.InsertOrUpdate(new DocumentStatus()
|
||||
{
|
||||
EnvelopeId = r.Envelope.Id,
|
||||
ReceiverId = r.ReceiverId,
|
||||
Value = annotationData,
|
||||
Status = Common.Constants.DocumentStatus.Signed
|
||||
});
|
||||
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// Better error handling & reporting
|
||||
logger.Error(e);
|
||||
return Problem(
|
||||
statusCode: 500,
|
||||
detail: e.Message,
|
||||
type: ErrorType.ServerError.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,25 +9,28 @@ namespace EnvelopeGenerator.Web.Controllers
|
||||
{
|
||||
public class HistoryController : Controller
|
||||
{
|
||||
private DatabaseService database;
|
||||
private LoggingService logging;
|
||||
private Logger logger;
|
||||
private ApiService api;
|
||||
private readonly DatabaseService database;
|
||||
private readonly LoggingService logging;
|
||||
private readonly Logger logger;
|
||||
private readonly ApiService api;
|
||||
|
||||
public HistoryController(DatabaseService database, LoggingService logging, ApiService api)
|
||||
{
|
||||
this.database = database;
|
||||
this.logging = logging;
|
||||
this.logger = logging.LogConfig.GetLoggerFor("HistoryController");
|
||||
this.logger = logging.LogConfig.GetLoggerFor(GetType().Name);
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/history/{envelopeKey}")]
|
||||
public IActionResult Create(HttpContext ctx, string envelopeKey)
|
||||
[IgnoreAntiforgeryToken]
|
||||
public IActionResult Create(string envelopeKey, [FromBody] ActionObject action)
|
||||
{
|
||||
try
|
||||
{
|
||||
var Request = ControllerContext.HttpContext.Request;
|
||||
|
||||
api.EnsureValidEnvelopeKey(envelopeKey);
|
||||
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
|
||||
var receiver = r.Envelope.Receivers.Where(receiver => receiver.Id == r.ReceiverId).SingleOrDefault();
|
||||
@@ -37,8 +40,8 @@ namespace EnvelopeGenerator.Web.Controllers
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
string actionTypeString = EnsureValidQueryField(ctx.Request, "actionType");
|
||||
string actionDescription = EnsureValidQueryField(ctx.Request, "actionDescription");
|
||||
string actionTypeString = action.actionType;
|
||||
string actionDescription = action.actionDescription;
|
||||
|
||||
if (!Enum.TryParse<EnvelopeHistoryActionType>(actionTypeString, out var actionType))
|
||||
{
|
||||
@@ -54,7 +57,7 @@ namespace EnvelopeGenerator.Web.Controllers
|
||||
UserReference = receiver.Email
|
||||
});
|
||||
|
||||
return Json(new { });
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -63,23 +66,10 @@ namespace EnvelopeGenerator.Web.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
private string EnsureValidQueryField(HttpRequest request, string fieldName)
|
||||
public class ActionObject
|
||||
{
|
||||
if (request.Query.TryGetValue(fieldName, out StringValues documentIndexString))
|
||||
{
|
||||
try
|
||||
{
|
||||
return documentIndexString.First();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new ArgumentNullException(fieldName, e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentNullException(fieldName);
|
||||
}
|
||||
public string actionType { get; set; }
|
||||
public string actionDescription { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user