Add history inserts, add controllers

This commit is contained in:
Jonathan Jenne
2023-11-02 16:30:57 +01:00
parent 3f4f4681b5
commit 47d02aefee
14 changed files with 428 additions and 135 deletions

View File

@@ -1,31 +1,37 @@
using DigitalData.Modules.Logging;
using DigitalData.Modules.Database;
using DigitalData.Modules.Logging;
using EnvelopeGenerator.Common;
using Microsoft.Extensions.Primitives;
using System.Reflection.Metadata;
using System.Text;
namespace EnvelopeGenerator.Web.Services
{
public class ApiService
{
private LogConfig _logConfig;
private Logger _logger;
private readonly DatabaseService database;
private readonly LogConfig logConfig;
private readonly Logger logger;
public ApiService(LoggingService Logging, IConfiguration Config)
public ApiService(LoggingService Logging, DatabaseService database, IConfiguration Config)
{
_logConfig = Logging.LogConfig;
_logger = Logging.LogConfig.GetLogger();
this.database = database;
this.logConfig = Logging.LogConfig;
this.logger = Logging.LogConfig.GetLogger();
_logger.Debug("Initializing ApiService");
logger.Debug("Initializing ApiService");
}
public string EnsureValidEnvelopeKey(string envelopeKey)
public EnvelopeResponse EnsureValidEnvelopeKey(string envelopeKey)
{
_logger.Debug("Parsing 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);
logger.Debug("EnvelopeUUID: [{0}]", result.Item1);
logger.Debug("ReceiverSignature: [{0}]", result.Item2);
if (string.IsNullOrEmpty(result.Item1))
throw new ArgumentNullException("EnvelopeUUID");
@@ -33,13 +39,96 @@ namespace EnvelopeGenerator.Web.Services
if (string.IsNullOrEmpty(result.Item2))
throw new ArgumentNullException("ReceiverSignature");
return envelopeKey;
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
return r;
}
public string EnsureValidEnvelopeKey(HttpRequest request)
{
var envelopeKey = request.RouteValues["envelopeKey"] as string;
return EnsureValidEnvelopeKey(envelopeKey);
public async Task<string?> EnsureValidAnnotationData(HttpRequest request)
{
logger.Debug("Parsing AnnotationData..");
try
{
using MemoryStream ms = new();
await request.BodyReader.CopyToAsync(ms);
var bytes = ms.ToArray();
return Encoding.UTF8.GetString(bytes);
}
catch (Exception e)
{
logger.Error(e);
return null;
}
}
public int EnsureValidDocumentIndex(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");
}
}
public EnvelopeDocument GetDocument(HttpRequest request, string envelopeKey)
{
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
int documentId = EnsureValidDocumentIndex(request);
var document = r.Envelope.Documents.
Where(d => d.Id == documentId).
FirstOrDefault();
if (document == null)
throw new ArgumentException("DocumentId");
return document;
}
public async Task<bool> UpdateDocument(Stream fileStream, string filePath)
{
try
{
using FileStream fs = new(filePath, FileMode.Open);
await fileStream.CopyToAsync(fs);
fs.Flush();
return true;
}
catch (Exception ex)
{
logger.Error(ex);
return false;
}
}
public State GetState(LogConfig LogConfig, MSSQLServer Database)
{
return new State
{
LogConfig = LogConfig,
Database = Database,
};
}
}
}

View File

@@ -60,15 +60,12 @@ namespace EnvelopeGenerator.Web.Services
public EnvelopeResponse LoadEnvelope(string pEnvelopeKey)
{
Tuple<string, string> result = Helpers.DecodeEnvelopeReceiverId(pEnvelopeKey);
Tuple<string, string> result = Helpers.DecodeEnvelopeReceiverId(pEnvelopeKey);
var envelopeUuid = result.Item1;
var receiverSignature = result.Item2;
var receiverId = receiverModel.GetReceiverIdBySignature(receiverSignature);
Envelope envelope = envelopeModel.GetByUuid(envelopeUuid);
List<EnvelopeDocument> documents = (List<EnvelopeDocument>)documentModel.List(envelope.Id, receiverId);
envelope.Documents = documents;
return new()
{