clean up server
This commit is contained in:
parent
0b6eecc534
commit
bbfbe3a7b4
30
EnvelopeGenerator.Web/Controllers/BaseController.cs
Normal file
30
EnvelopeGenerator.Web/Controllers/BaseController.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using DigitalData.Modules.Logging;
|
||||||
|
using EnvelopeGenerator.Web.Services;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using static EnvelopeGenerator.Web.Constants;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Web.Controllers
|
||||||
|
{
|
||||||
|
public class BaseController : Controller
|
||||||
|
{
|
||||||
|
internal DatabaseService database;
|
||||||
|
internal LogConfig logConfig;
|
||||||
|
internal Logger logger;
|
||||||
|
|
||||||
|
public BaseController(DatabaseService database, LoggingService logging)
|
||||||
|
{
|
||||||
|
this.database = database;
|
||||||
|
this.logConfig = logging.LogConfig;
|
||||||
|
this.logger = logging.LogConfig.GetLoggerFor(GetType().Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal ObjectResult ErrorResponse(Exception e)
|
||||||
|
{
|
||||||
|
logger.Error(e);
|
||||||
|
return Problem(
|
||||||
|
statusCode: 500,
|
||||||
|
detail: e.Message,
|
||||||
|
type: ErrorType.ServerError.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,25 +1,16 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using DigitalData.Modules.Logging;
|
|
||||||
using EnvelopeGenerator.Common;
|
using EnvelopeGenerator.Common;
|
||||||
using EnvelopeGenerator.Web.Services;
|
using EnvelopeGenerator.Web.Services;
|
||||||
using static EnvelopeGenerator.Common.Constants;
|
|
||||||
using static EnvelopeGenerator.Web.Constants;
|
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Web.Controllers
|
namespace EnvelopeGenerator.Web.Controllers
|
||||||
{
|
{
|
||||||
public class DocumentController : Controller
|
public class DocumentController : BaseController
|
||||||
{
|
{
|
||||||
private readonly DatabaseService database;
|
private readonly EnvelopeService envelopeService;
|
||||||
private readonly LoggingService logging;
|
|
||||||
private readonly Logger logger;
|
|
||||||
private readonly ApiService api;
|
|
||||||
|
|
||||||
public DocumentController(DatabaseService database, LoggingService logging, ApiService api)
|
public DocumentController(DatabaseService database, LoggingService logging, EnvelopeService envelope) : base(database, logging)
|
||||||
{
|
{
|
||||||
this.database = database;
|
envelopeService = envelope;
|
||||||
this.logging = logging;
|
|
||||||
this.logger = logging.LogConfig.GetLoggerFor(GetType().Name);
|
|
||||||
this.api = api;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
@ -28,40 +19,25 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
logger.Info("Handling file download.");
|
logger.Info("DocumentController/Get");
|
||||||
|
|
||||||
// Validate Envelope Key
|
// Validate Envelope Key and load envelope
|
||||||
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
|
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
|
||||||
|
EnvelopeResponse response = envelopeService.LoadEnvelope(envelopeKey);
|
||||||
|
|
||||||
// Load document info
|
// Load document info
|
||||||
var Request = ControllerContext.HttpContext.Request;
|
var Request = ControllerContext.HttpContext.Request;
|
||||||
var document = api.GetDocument(Request, envelopeKey);
|
var document = envelopeService.GetDocument(Request, envelopeKey);
|
||||||
|
|
||||||
// Load the document from disk
|
// Load the document from disk
|
||||||
var bytes = await System.IO.File.ReadAllBytesAsync(document.Filepath);
|
var bytes = await envelopeService.GetDocumentContents(document);
|
||||||
logger.Info("Serving file, size: [{0}]", bytes.Length);
|
|
||||||
|
|
||||||
// Return the document as bytes
|
// Return the document as bytes
|
||||||
return File(bytes, "application/octet-stream");
|
return File(bytes, "application/octet-stream");
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
logger.Error(e);
|
|
||||||
return Problem(
|
|
||||||
statusCode: 500,
|
|
||||||
detail: e.Message,
|
|
||||||
type: ErrorType.ServerError.ToString());
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
// Better error handling & reporting
|
return ErrorResponse(e);
|
||||||
logger.Error(e);
|
|
||||||
return Problem(
|
|
||||||
statusCode: 500,
|
|
||||||
detail: e.Message,
|
|
||||||
type: ErrorType.ServerError.ToString());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,50 +47,27 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
logger.Info("Handling file update.");
|
logger.Info("DocumentController/Update");
|
||||||
|
|
||||||
// Validate Envelope Key
|
// Validate Envelope Key and load envelope
|
||||||
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
|
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
|
||||||
|
EnvelopeResponse response = envelopeService.LoadEnvelope(envelopeKey);
|
||||||
|
|
||||||
// Load Document info
|
// Load Document info
|
||||||
var Request = ControllerContext.HttpContext.Request;
|
var Request = ControllerContext.HttpContext.Request;
|
||||||
var document = api.GetDocument(Request, envelopeKey);
|
var document = envelopeService.GetDocument(Request, envelopeKey);
|
||||||
|
|
||||||
// Try to update the document with new data
|
// Update the document with new data
|
||||||
if (!await api.UpdateDocument(Request.Body, document.Filepath))
|
await envelopeService.UpdateDocument(Request.Body, document.Filepath);
|
||||||
{
|
|
||||||
throw new IOException("Document could not be saved to disk!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add history entry
|
// Add history entry
|
||||||
database.InsertHistoryEntry(new EnvelopeHistoryEntry()
|
envelopeService.InsertHistoryEntrySigned(response);
|
||||||
{
|
|
||||||
ActionDescription = "Dokument wurde signiert",
|
|
||||||
ActionDate = DateTime.Now,
|
|
||||||
ActionType = EnvelopeHistoryActionType.Signed,
|
|
||||||
EnvelopeId = r.Envelope.Id,
|
|
||||||
UserReference = r.Receiver.Email
|
|
||||||
});
|
|
||||||
|
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
logger.Error(e);
|
|
||||||
return Problem(
|
|
||||||
statusCode: 500,
|
|
||||||
detail: e.Message,
|
|
||||||
type: ErrorType.ServerError.ToString());
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
// Better error handling & reporting
|
return ErrorResponse(e);
|
||||||
logger.Error(e);
|
|
||||||
return Problem(
|
|
||||||
statusCode: 500,
|
|
||||||
detail: e.Message,
|
|
||||||
type: ErrorType.ServerError.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,24 +2,17 @@
|
|||||||
using EnvelopeGenerator.Common;
|
using EnvelopeGenerator.Common;
|
||||||
using EnvelopeGenerator.Web.Services;
|
using EnvelopeGenerator.Web.Services;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using NLog;
|
|
||||||
using static EnvelopeGenerator.Web.Constants;
|
using static EnvelopeGenerator.Web.Constants;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Web.Controllers
|
namespace EnvelopeGenerator.Web.Controllers
|
||||||
{
|
{
|
||||||
public class EnvelopeController : Controller
|
public class EnvelopeController : BaseController
|
||||||
{
|
{
|
||||||
private readonly DatabaseService database;
|
private readonly EnvelopeService envelopeService;
|
||||||
private readonly LoggingService logging;
|
|
||||||
private readonly Logger logger;
|
|
||||||
private readonly ApiService api;
|
|
||||||
|
|
||||||
public EnvelopeController(DatabaseService database, LoggingService logging, ApiService api)
|
public EnvelopeController(DatabaseService database, LoggingService logging, EnvelopeService envelope) : base(database, logging)
|
||||||
{
|
{
|
||||||
this.database = database;
|
envelopeService = envelope;
|
||||||
this.logging = logging;
|
|
||||||
this.logger = logging.LogConfig.GetLoggerFor(GetType().Name);
|
|
||||||
this.api = api;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
@ -28,23 +21,17 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
logger.Info("Handling envelope loading.");
|
logger.Info("EnvelopeController/Get");
|
||||||
|
|
||||||
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
|
// Validate Envelope Key and load envelope
|
||||||
|
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
|
||||||
// Return the envelope and additional data as json
|
EnvelopeResponse response = envelopeService.LoadEnvelope(envelopeKey);
|
||||||
return Json(r);
|
|
||||||
|
|
||||||
|
return Json(response);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
// Better error handling & reporting
|
return ErrorResponse(e);
|
||||||
logger.Error(e);
|
|
||||||
return Problem(
|
|
||||||
statusCode: 500,
|
|
||||||
detail: e.Message,
|
|
||||||
type: ErrorType.ServerError.ToString());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,24 +41,21 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
logger.Info("Handling envelope saving.");
|
logger.Info("EnvelopeController/Update");
|
||||||
|
|
||||||
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
|
// Validate Envelope Key and load envelope
|
||||||
|
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
|
||||||
|
EnvelopeResponse response = envelopeService.LoadEnvelope(envelopeKey);
|
||||||
|
|
||||||
var Request = ControllerContext.HttpContext.Request;
|
var Request = ControllerContext.HttpContext.Request;
|
||||||
var document = api.GetDocument(Request, envelopeKey);
|
var document = envelopeService.GetDocument(Request, envelopeKey);
|
||||||
|
|
||||||
string? annotationData = await api.EnsureValidAnnotationData(Request);
|
string annotationData = await envelopeService.EnsureValidAnnotationData(Request);
|
||||||
|
|
||||||
if (annotationData == null)
|
envelopeService.InsertDocumentStatus(new DocumentStatus()
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException("AnnotationData");
|
EnvelopeId = response.Envelope.Id,
|
||||||
}
|
ReceiverId = response.Receiver.Id,
|
||||||
|
|
||||||
database.InsertDocumentStatus(new DocumentStatus()
|
|
||||||
{
|
|
||||||
EnvelopeId = r.Envelope.Id,
|
|
||||||
ReceiverId = r.Receiver.Id,
|
|
||||||
Value = annotationData,
|
Value = annotationData,
|
||||||
Status = Common.Constants.DocumentStatus.Signed
|
Status = Common.Constants.DocumentStatus.Signed
|
||||||
});
|
});
|
||||||
@ -80,12 +64,7 @@ namespace EnvelopeGenerator.Web.Controllers
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
// Better error handling & reporting
|
return ErrorResponse(e);
|
||||||
logger.Error(e);
|
|
||||||
return Problem(
|
|
||||||
statusCode: 500,
|
|
||||||
detail: e.Message,
|
|
||||||
type: ErrorType.ServerError.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,7 +41,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Data\" />
|
|
||||||
<Folder Include="wwwroot\lib\NewFolder\" />
|
<Folder Include="wwwroot\lib\NewFolder\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
@page "/"
|
@page "/"
|
||||||
@using EnvelopeGenerator.Common;
|
@using EnvelopeGenerator.Common;
|
||||||
@using EnvelopeGenerator.Web.Services;
|
@using EnvelopeGenerator.Web.Services;
|
||||||
@inject DatabaseService Database;
|
@inject EnvelopeService Envelope;
|
||||||
|
|
||||||
<PageTitle>Index</PageTitle>
|
<PageTitle>Index</PageTitle>
|
||||||
|
|
||||||
@ -32,7 +32,7 @@
|
|||||||
protected override void OnInitialized()
|
protected override void OnInitialized()
|
||||||
{
|
{
|
||||||
// Test
|
// Test
|
||||||
envelopes = Database.LoadEnvelopes(receiverId);
|
envelopes = Envelope.LoadEnvelopes(receiverId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -6,11 +6,13 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||
builder.Services.AddRazorPages();
|
builder.Services.AddRazorPages();
|
||||||
builder.Services.AddServerSideBlazor();
|
builder.Services.AddServerSideBlazor();
|
||||||
|
|
||||||
// Add custom services
|
// Add base services
|
||||||
builder.Services.AddSingleton<LoggingService>();
|
builder.Services.AddSingleton<LoggingService>();
|
||||||
builder.Services.AddSingleton<ApiService>();
|
|
||||||
builder.Services.AddTransient<DatabaseService>();
|
builder.Services.AddTransient<DatabaseService>();
|
||||||
|
|
||||||
|
// Add higher order services
|
||||||
|
builder.Services.AddSingleton<EnvelopeService>();
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
builder.Services.AddLocalization();
|
builder.Services.AddLocalization();
|
||||||
|
|||||||
@ -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.Database;
|
||||||
using DigitalData.Modules.Logging;
|
|
||||||
using EnvelopeGenerator.Common;
|
using EnvelopeGenerator.Common;
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Web.Services
|
namespace EnvelopeGenerator.Web.Services
|
||||||
{
|
{
|
||||||
public class DatabaseService
|
public class DatabaseService: BaseService
|
||||||
{
|
{
|
||||||
public MSSQLServer MSSQL { get; set; }
|
public MSSQLServer MSSQL { get; set; }
|
||||||
|
|
||||||
private EnvelopeModel envelopeModel;
|
public class ModelContainer
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
_logConfig = Logging.LogConfig;
|
public EnvelopeModel envelopeModel;
|
||||||
_logger = Logging.LogConfig.GetLogger();
|
public DocumentModel documentModel;
|
||||||
|
public ReceiverModel receiverModel;
|
||||||
|
public ElementModel elementModel;
|
||||||
|
public HistoryModel historyModel;
|
||||||
|
public DocumentStatusModel documentStatusModel;
|
||||||
|
|
||||||
_logger.Debug("Establishing MSSQL Database connection..");
|
public ModelContainer(State state)
|
||||||
MSSQL = new MSSQLServer(_logConfig, Config["Config:ConnectionString"]);
|
{
|
||||||
|
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)
|
if (MSSQL.DBInitialized == true)
|
||||||
{
|
{
|
||||||
_logger.Debug("MSSQL Connection: [{0}]", MSSQL.CurrentConnectionString);
|
logger.Debug("MSSQL Connection: [{0}]", MSSQL.CurrentConnectionString);
|
||||||
|
|
||||||
var state = GetState();
|
var state = GetState();
|
||||||
InitializeModels(state);
|
Models = new(state);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.Warn("Connection could not be established!");
|
logger.Error("Connection could not be established!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public State GetState()
|
private State GetState()
|
||||||
{
|
{
|
||||||
return new State
|
return new State
|
||||||
{
|
{
|
||||||
Database = MSSQL,
|
Database = MSSQL,
|
||||||
LogConfig = _logConfig,
|
LogConfig = logConfig,
|
||||||
UserId = 2 // TODO
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user