clean up server

This commit is contained in:
Jonathan Jenne
2023-11-06 10:46:02 +01:00
parent 0b6eecc534
commit bbfbe3a7b4
10 changed files with 317 additions and 330 deletions

View File

@@ -1,25 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using DigitalData.Modules.Logging;
using EnvelopeGenerator.Common;
using EnvelopeGenerator.Web.Services;
using static EnvelopeGenerator.Common.Constants;
using static EnvelopeGenerator.Web.Constants;
namespace EnvelopeGenerator.Web.Controllers
{
public class DocumentController : Controller
public class DocumentController : BaseController
{
private readonly DatabaseService database;
private readonly LoggingService logging;
private readonly Logger logger;
private readonly ApiService api;
private readonly EnvelopeService envelopeService;
public DocumentController(DatabaseService database, LoggingService logging, ApiService api)
public DocumentController(DatabaseService database, LoggingService logging, EnvelopeService envelope) : base(database, logging)
{
this.database = database;
this.logging = logging;
this.logger = logging.LogConfig.GetLoggerFor(GetType().Name);
this.api = api;
envelopeService = envelope;
}
[HttpGet]
@@ -28,40 +19,25 @@ namespace EnvelopeGenerator.Web.Controllers
{
try
{
logger.Info("Handling file download.");
logger.Info("DocumentController/Get");
// Validate Envelope Key
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
// Validate Envelope Key and load envelope
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
EnvelopeResponse response = envelopeService.LoadEnvelope(envelopeKey);
// Load document info
var Request = ControllerContext.HttpContext.Request;
var document = api.GetDocument(Request, envelopeKey);
var document = envelopeService.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);
var bytes = await envelopeService.GetDocumentContents(document);
// Return the document as bytes
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)
{
// Better error handling & reporting
logger.Error(e);
return Problem(
statusCode: 500,
detail: e.Message,
type: ErrorType.ServerError.ToString());
return ErrorResponse(e);
}
}
@@ -71,50 +47,27 @@ namespace EnvelopeGenerator.Web.Controllers
{
try
{
logger.Info("Handling file update.");
logger.Info("DocumentController/Update");
// Validate Envelope Key
EnvelopeResponse r = api.EnsureValidEnvelopeKey(envelopeKey);
// Validate Envelope Key and load envelope
envelopeService.EnsureValidEnvelopeKey(envelopeKey);
EnvelopeResponse response = envelopeService.LoadEnvelope(envelopeKey);
// Load Document info
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
if (!await api.UpdateDocument(Request.Body, document.Filepath))
{
throw new IOException("Document could not be saved to disk!");
}
// Update the document with new data
await envelopeService.UpdateDocument(Request.Body, document.Filepath);
// Add history entry
database.InsertHistoryEntry(new EnvelopeHistoryEntry()
{
ActionDescription = "Dokument wurde signiert",
ActionDate = DateTime.Now,
ActionType = EnvelopeHistoryActionType.Signed,
EnvelopeId = r.Envelope.Id,
UserReference = r.Receiver.Email
});
envelopeService.InsertHistoryEntrySigned(response);
return Ok();
}
catch (IOException e)
{
logger.Error(e);
return Problem(
statusCode: 500,
detail: e.Message,
type: ErrorType.ServerError.ToString());
}
catch (Exception e)
{
// Better error handling & reporting
logger.Error(e);
return Problem(
statusCode: 500,
detail: e.Message,
type: ErrorType.ServerError.ToString());
return ErrorResponse(e);
}
}
}