Add history controller
This commit is contained in:
parent
dc24ae3631
commit
c924a5ec70
85
EnvelopeGenerator.Web/Controllers/HistoryController.cs
Normal file
85
EnvelopeGenerator.Web/Controllers/HistoryController.cs
Normal file
@ -0,0 +1,85 @@
|
||||
using DigitalData.Modules.Logging;
|
||||
using EnvelopeGenerator.Common;
|
||||
using EnvelopeGenerator.Web.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using static EnvelopeGenerator.Common.Constants;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Controllers
|
||||
{
|
||||
public class HistoryController : Controller
|
||||
{
|
||||
private DatabaseService database;
|
||||
private LoggingService logging;
|
||||
private Logger logger;
|
||||
private ApiService api;
|
||||
|
||||
public HistoryController(DatabaseService database, LoggingService logging, ApiService api)
|
||||
{
|
||||
this.database = database;
|
||||
this.logging = logging;
|
||||
this.logger = logging.LogConfig.GetLoggerFor("HistoryController");
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/history/{envelopeKey}")]
|
||||
public IActionResult Create(HttpContext ctx, string envelopeKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
api.EnsureValidEnvelopeKey(envelopeKey);
|
||||
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
|
||||
var receiver = r.Envelope.Receivers.Where(receiver => receiver.Id == r.ReceiverId).SingleOrDefault();
|
||||
|
||||
if (receiver == null)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
string actionTypeString = EnsureValidQueryField(ctx.Request, "actionType");
|
||||
string actionDescription = EnsureValidQueryField(ctx.Request, "actionDescription");
|
||||
|
||||
if (!Enum.TryParse<EnvelopeHistoryActionType>(actionTypeString, out var actionType))
|
||||
{
|
||||
return BadRequest();
|
||||
};
|
||||
|
||||
database.InsertHistoryEntry(new EnvelopeHistoryEntry()
|
||||
{
|
||||
ActionDescription = actionDescription,
|
||||
ActionDate = DateTime.Now,
|
||||
ActionType = actionType,
|
||||
EnvelopeId = r.Envelope.Id,
|
||||
UserReference = receiver.Email
|
||||
});
|
||||
|
||||
return Json(new { });
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.Error(e);
|
||||
return Problem(statusCode: 500);
|
||||
}
|
||||
}
|
||||
|
||||
private string EnsureValidQueryField(HttpRequest request, string fieldName)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
68
EnvelopeGenerator.Web/Handler/HistoryHandler.cs
Normal file
68
EnvelopeGenerator.Web/Handler/HistoryHandler.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using EnvelopeGenerator.Common;
|
||||
using EnvelopeGenerator.Web.Services;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Handler
|
||||
{
|
||||
public class HistoryHandler
|
||||
{
|
||||
private DatabaseService database;
|
||||
private LoggingService logging;
|
||||
private ApiService api;
|
||||
|
||||
public HistoryHandler(DatabaseService database, LoggingService logging, ApiService api)
|
||||
{
|
||||
this.database = database;
|
||||
this.logging = logging;
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
public IResult HandlePostHistoryEntry(HttpContext ctx)
|
||||
{
|
||||
try
|
||||
{
|
||||
//
|
||||
|
||||
|
||||
// Load Envelope from EnvelopeKey
|
||||
string envelopeKey = api.EnsureValidEnvelopeKey(ctx.Request);
|
||||
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
|
||||
var receiver = r.Envelope.Receivers.Where(receiver => receiver.Id == r.ReceiverId).SingleOrDefault();
|
||||
|
||||
EnvelopeHistoryEntry historyEntry = new()
|
||||
{
|
||||
EnvelopeId = r.Envelope.Id,
|
||||
UserReference = receiver.Email
|
||||
};
|
||||
|
||||
database.InsertHistoryEntry(historyEntry);
|
||||
|
||||
return Results.Ok();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
return Results.Problem();
|
||||
}
|
||||
}
|
||||
|
||||
private int EnsureValidHistoryEntry(Logger logger, HttpRequest request)
|
||||
{
|
||||
if (request.Query.TryGetValue("index", out StringValues documentIndexString))
|
||||
{
|
||||
try
|
||||
{
|
||||
return int.Parse(documentIndexString.First());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new ArgumentNullException("DocumentIndex", e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentNullException("DocumentIndex");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -12,8 +12,11 @@ builder.Services.AddServerSideBlazor();
|
||||
|
||||
// Add custom services
|
||||
builder.Services.AddSingleton<LoggingService>();
|
||||
builder.Services.AddSingleton<ApiService>();
|
||||
builder.Services.AddTransient<DatabaseService>();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddLocalization();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
45
EnvelopeGenerator.Web/Services/ApiService.cs
Normal file
45
EnvelopeGenerator.Web/Services/ApiService.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using DigitalData.Modules.Logging;
|
||||
using EnvelopeGenerator.Common;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Services
|
||||
{
|
||||
public class ApiService
|
||||
{
|
||||
private LogConfig _logConfig;
|
||||
private Logger _logger;
|
||||
|
||||
public ApiService(LoggingService Logging, IConfiguration Config)
|
||||
{
|
||||
_logConfig = Logging.LogConfig;
|
||||
_logger = Logging.LogConfig.GetLogger();
|
||||
|
||||
_logger.Debug("Initializing ApiService");
|
||||
}
|
||||
|
||||
public string EnsureValidEnvelopeKey(string envelopeKey)
|
||||
{
|
||||
_logger.Debug("Parsing EnvelopeKey..");
|
||||
|
||||
if (string.IsNullOrEmpty(envelopeKey))
|
||||
throw new ArgumentNullException("EnvelopeKey");
|
||||
|
||||
Tuple<string, string> result = Helpers.DecodeEnvelopeReceiverId(envelopeKey);
|
||||
_logger.Debug("EnvelopeUUID: [{0}]", result.Item1);
|
||||
_logger.Debug("ReceiverSignature: [{0}]", result.Item2);
|
||||
|
||||
if (string.IsNullOrEmpty(result.Item1))
|
||||
throw new ArgumentNullException("EnvelopeUUID");
|
||||
|
||||
if (string.IsNullOrEmpty(result.Item2))
|
||||
throw new ArgumentNullException("ReceiverSignature");
|
||||
|
||||
return envelopeKey;
|
||||
}
|
||||
|
||||
public string EnsureValidEnvelopeKey(HttpRequest request)
|
||||
{
|
||||
var envelopeKey = request.RouteValues["envelopeKey"] as string;
|
||||
return EnsureValidEnvelopeKey(envelopeKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -12,6 +12,7 @@ namespace EnvelopeGenerator.Web.Services
|
||||
private DocumentModel documentModel;
|
||||
private ReceiverModel receiverModel;
|
||||
private ElementModel elementModel;
|
||||
private HistoryModel historyModel;
|
||||
|
||||
private readonly LogConfig _logConfig;
|
||||
private readonly Logger _logger;
|
||||
@ -54,6 +55,7 @@ namespace EnvelopeGenerator.Web.Services
|
||||
documentModel = new(state);
|
||||
receiverModel = new(state);
|
||||
elementModel = new(state);
|
||||
historyModel = new(state);
|
||||
}
|
||||
|
||||
public EnvelopeResponse LoadEnvelope(string pEnvelopeKey)
|
||||
@ -85,5 +87,10 @@ namespace EnvelopeGenerator.Web.Services
|
||||
return documentModel.GetById(pDocumentId);
|
||||
}
|
||||
|
||||
public bool InsertHistoryEntry(EnvelopeHistoryEntry historyEntry)
|
||||
{
|
||||
return historyModel.Insert(historyEntry);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user