clean up server
This commit is contained in:
@@ -1,131 +0,0 @@
|
||||
using DigitalData.Modules.Database;
|
||||
using DigitalData.Modules.Logging;
|
||||
using EnvelopeGenerator.Common;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Text;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Services
|
||||
{
|
||||
public class ApiService
|
||||
{
|
||||
private readonly DatabaseService database;
|
||||
private readonly LogConfig logConfig;
|
||||
private readonly Logger logger;
|
||||
|
||||
public ApiService(LoggingService Logging, DatabaseService database, IConfiguration Config)
|
||||
{
|
||||
this.database = database;
|
||||
this.logConfig = Logging.LogConfig;
|
||||
this.logger = Logging.LogConfig.GetLogger();
|
||||
|
||||
logger.Debug("Initializing ApiService");
|
||||
}
|
||||
|
||||
public EnvelopeResponse 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");
|
||||
|
||||
EnvelopeResponse response = database.LoadEnvelope(envelopeKey);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
18
EnvelopeGenerator.Web/Services/BaseService.cs
Normal file
18
EnvelopeGenerator.Web/Services/BaseService.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using DigitalData.Modules.Logging;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Services
|
||||
{
|
||||
public class BaseService
|
||||
{
|
||||
internal readonly LogConfig logConfig;
|
||||
internal readonly IConfiguration config;
|
||||
internal Logger logger;
|
||||
|
||||
public BaseService(IConfiguration Config, LoggingService Logging)
|
||||
{
|
||||
logConfig = Logging.LogConfig;
|
||||
logger = Logging.LogConfig.GetLogger();
|
||||
config = Config;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,116 +1,61 @@
|
||||
using DigitalData.Modules.Database;
|
||||
using DigitalData.Modules.Logging;
|
||||
using EnvelopeGenerator.Common;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Services
|
||||
{
|
||||
public class DatabaseService
|
||||
public class DatabaseService: BaseService
|
||||
{
|
||||
public MSSQLServer MSSQL { get; set; }
|
||||
|
||||
private EnvelopeModel envelopeModel;
|
||||
private DocumentModel documentModel;
|
||||
private ReceiverModel receiverModel;
|
||||
private ElementModel elementModel;
|
||||
private HistoryModel historyModel;
|
||||
private DocumentStatusModel documentStatusModel;
|
||||
private readonly LogConfig _logConfig;
|
||||
private readonly Logger _logger;
|
||||
|
||||
|
||||
public DatabaseService(LoggingService Logging, IConfiguration Config)
|
||||
public class ModelContainer
|
||||
{
|
||||
_logConfig = Logging.LogConfig;
|
||||
_logger = Logging.LogConfig.GetLogger();
|
||||
public EnvelopeModel envelopeModel;
|
||||
public DocumentModel documentModel;
|
||||
public ReceiverModel receiverModel;
|
||||
public ElementModel elementModel;
|
||||
public HistoryModel historyModel;
|
||||
public DocumentStatusModel documentStatusModel;
|
||||
|
||||
_logger.Debug("Establishing MSSQL Database connection..");
|
||||
MSSQL = new MSSQLServer(_logConfig, Config["Config:ConnectionString"]);
|
||||
public ModelContainer(State state)
|
||||
{
|
||||
envelopeModel = new(state);
|
||||
documentModel = new(state);
|
||||
receiverModel = new(state);
|
||||
elementModel = new(state);
|
||||
historyModel = new(state);
|
||||
documentStatusModel = new(state);
|
||||
}
|
||||
}
|
||||
public readonly ModelContainer? Models;
|
||||
|
||||
public DatabaseService(IConfiguration Config, LoggingService Logging, DatabaseService database) : base(Config, Logging)
|
||||
{
|
||||
logger = Logging.LogConfig.GetLogger();
|
||||
|
||||
logger.Debug("Establishing MSSQL Database connection..");
|
||||
MSSQL = new MSSQLServer(logConfig, Config["Config:ConnectionString"]);
|
||||
|
||||
if (MSSQL.DBInitialized == true)
|
||||
{
|
||||
_logger.Debug("MSSQL Connection: [{0}]", MSSQL.CurrentConnectionString);
|
||||
logger.Debug("MSSQL Connection: [{0}]", MSSQL.CurrentConnectionString);
|
||||
|
||||
var state = GetState();
|
||||
InitializeModels(state);
|
||||
Models = new(state);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Warn("Connection could not be established!");
|
||||
logger.Error("Connection could not be established!");
|
||||
}
|
||||
}
|
||||
|
||||
public State GetState()
|
||||
private State GetState()
|
||||
{
|
||||
return new State
|
||||
{
|
||||
Database = MSSQL,
|
||||
LogConfig = _logConfig,
|
||||
LogConfig = logConfig,
|
||||
UserId = 2 // TODO
|
||||
};
|
||||
}
|
||||
|
||||
public void InitializeModels(State state)
|
||||
{
|
||||
envelopeModel = new(state);
|
||||
documentModel = new(state);
|
||||
receiverModel = new(state);
|
||||
elementModel = new(state);
|
||||
historyModel = new(state);
|
||||
documentStatusModel = new(state);
|
||||
}
|
||||
|
||||
public EnvelopeResponse LoadEnvelope(string 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);
|
||||
|
||||
if (envelope == null)
|
||||
{
|
||||
throw new NullReferenceException("Envelope not found");
|
||||
}
|
||||
|
||||
if (envelope.Receivers == null)
|
||||
{
|
||||
throw new NullReferenceException("Receivers for envelope not loaded");
|
||||
}
|
||||
|
||||
EnvelopeReceiver? receiver = envelope.Receivers.Where(r => r.Id == receiverId).SingleOrDefault();
|
||||
|
||||
if (receiver == null)
|
||||
{
|
||||
throw new NullReferenceException("Receiver not found");
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Receiver = receiver,
|
||||
Envelope = envelope
|
||||
};
|
||||
}
|
||||
|
||||
public List<Envelope> LoadEnvelopes(int pReceiverId)
|
||||
{
|
||||
return (List<Envelope>)envelopeModel.List(pReceiverId);
|
||||
}
|
||||
|
||||
public EnvelopeDocument LoadDocument(int pDocumentId)
|
||||
{
|
||||
return documentModel.GetById(pDocumentId);
|
||||
}
|
||||
|
||||
public bool InsertHistoryEntry(EnvelopeHistoryEntry historyEntry)
|
||||
{
|
||||
return historyModel.Insert(historyEntry);
|
||||
}
|
||||
|
||||
public bool InsertDocumentStatus(DocumentStatus documentStatus)
|
||||
{
|
||||
return documentStatusModel.InsertOrUpdate(documentStatus);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
192
EnvelopeGenerator.Web/Services/EnvelopeService.cs
Normal file
192
EnvelopeGenerator.Web/Services/EnvelopeService.cs
Normal file
@@ -0,0 +1,192 @@
|
||||
using EnvelopeGenerator.Common;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Text;
|
||||
using static EnvelopeGenerator.Common.Constants;
|
||||
|
||||
namespace EnvelopeGenerator.Web.Services
|
||||
{
|
||||
public class EnvelopeService : BaseService
|
||||
{
|
||||
private ReceiverModel receiverModel;
|
||||
private EnvelopeModel envelopeModel;
|
||||
private HistoryModel historyModel;
|
||||
private DocumentModel documentModel;
|
||||
private DocumentStatusModel documentStatusModel;
|
||||
|
||||
public EnvelopeService(IConfiguration Config, LoggingService Logging, DatabaseService database) : base(Config, Logging)
|
||||
{
|
||||
logger = Logging.LogConfig.GetLogger();
|
||||
|
||||
if (database.Models == null)
|
||||
{
|
||||
throw new ArgumentNullException("Models not loaded.");
|
||||
}
|
||||
|
||||
receiverModel = database.Models.receiverModel;
|
||||
envelopeModel = database.Models.envelopeModel;
|
||||
historyModel = database.Models.historyModel;
|
||||
documentModel = database.Models.documentModel;
|
||||
documentStatusModel = database.Models.documentStatusModel;
|
||||
}
|
||||
|
||||
public void 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");
|
||||
}
|
||||
|
||||
|
||||
public EnvelopeResponse LoadEnvelope(string 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);
|
||||
|
||||
if (envelope == null)
|
||||
{
|
||||
throw new NullReferenceException("Envelope not found");
|
||||
}
|
||||
|
||||
if (envelope.Receivers == null)
|
||||
{
|
||||
throw new NullReferenceException("Receivers for envelope not loaded");
|
||||
}
|
||||
|
||||
EnvelopeReceiver? receiver = envelope.Receivers.Where(r => r.Id == receiverId).SingleOrDefault();
|
||||
|
||||
if (receiver == null)
|
||||
{
|
||||
throw new NullReferenceException("Receiver not found");
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Receiver = receiver,
|
||||
Envelope = envelope
|
||||
};
|
||||
}
|
||||
|
||||
public List<Envelope> LoadEnvelopes(int pReceiverId)
|
||||
{
|
||||
return (List<Envelope>)envelopeModel.List(pReceiverId);
|
||||
}
|
||||
|
||||
public bool InsertHistoryEntry(EnvelopeHistoryEntry historyEntry)
|
||||
{
|
||||
return historyModel.Insert(historyEntry);
|
||||
}
|
||||
|
||||
public bool InsertHistoryEntrySigned(EnvelopeResponse response)
|
||||
{
|
||||
return historyModel.Insert(new EnvelopeHistoryEntry()
|
||||
{
|
||||
ActionDescription = "Dokument wurde signiert",
|
||||
ActionDate = DateTime.Now,
|
||||
ActionType = EnvelopeHistoryActionType.Signed,
|
||||
EnvelopeId = response.Envelope.Id,
|
||||
UserReference = response.Receiver.Email
|
||||
});
|
||||
}
|
||||
|
||||
public bool InsertDocumentStatus(Common.DocumentStatus documentStatus)
|
||||
{
|
||||
return documentStatusModel.InsertOrUpdate(documentStatus);
|
||||
}
|
||||
|
||||
public async Task<string?> EnsureValidAnnotationData(HttpRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.Debug("Parsing annotation data from request..");
|
||||
|
||||
using MemoryStream ms = new();
|
||||
await request.BodyReader.CopyToAsync(ms);
|
||||
var bytes = ms.ToArray();
|
||||
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.Error(e);
|
||||
throw new ArgumentNullException("AnnotationData");
|
||||
}
|
||||
}
|
||||
|
||||
public int EnsureValidDocumentIndex(HttpRequest request)
|
||||
{
|
||||
if (!request.Query.TryGetValue("index", out StringValues documentIndexStringList))
|
||||
{
|
||||
logger.Warn("There is no query parameter called index");
|
||||
throw new ArgumentNullException("DocumentIndex");
|
||||
}
|
||||
|
||||
if (documentIndexStringList.FirstOrDefault() == null)
|
||||
{
|
||||
logger.Warn("There is no query parameter called index");
|
||||
throw new ArgumentNullException("DocumentIndex");
|
||||
}
|
||||
|
||||
if (!int.TryParse(documentIndexStringList.First(), out int documentIndex))
|
||||
{
|
||||
logger.Warn("Invalid document index [{0}]", documentIndexStringList.First());
|
||||
throw new ArgumentNullException("DocumentIndex");
|
||||
}
|
||||
|
||||
return documentIndex;
|
||||
}
|
||||
|
||||
public EnvelopeDocument GetDocument(HttpRequest request, string envelopeKey)
|
||||
{
|
||||
EnvelopeResponse response = LoadEnvelope(envelopeKey);
|
||||
int documentId = EnsureValidDocumentIndex(request);
|
||||
|
||||
logger.Debug("Loading document for Id [{0}]", documentId);
|
||||
|
||||
var document = response.Envelope.Documents.
|
||||
Where(d => d.Id == documentId).
|
||||
FirstOrDefault();
|
||||
|
||||
if (document == null)
|
||||
throw new ArgumentException("DocumentId");
|
||||
|
||||
logger.Debug("Document [{0}] loaded!", documentId);
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
public async Task UpdateDocument(Stream fileStream, string filePath)
|
||||
{
|
||||
logger.Debug("Writing document to path [{0}]..", filePath);
|
||||
|
||||
using FileStream fs = new(filePath, FileMode.Open);
|
||||
await fileStream.CopyToAsync(fs);
|
||||
|
||||
logger.Debug("Document written!");
|
||||
}
|
||||
|
||||
public async Task<byte[]> GetDocumentContents(EnvelopeDocument document)
|
||||
{
|
||||
logger.Debug("Loading file [{0}]", document.Filepath);
|
||||
var bytes = await File.ReadAllBytesAsync(document.Filepath);
|
||||
logger.Info("File loaded, size: [{0}]", bytes.Length);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user