using Microsoft.AspNetCore.Mvc; using EnvelopeGenerator.Common; using EnvelopeGenerator.Web.Services; namespace EnvelopeGenerator.Web.Controllers { public class DocumentController : BaseController { private readonly EnvelopeService envelopeService; public DocumentController(DatabaseService database, LoggingService logging, EnvelopeService envelope) : base(database, logging) { envelopeService = envelope; } [HttpGet] [Route("api/document/{envelopeKey}")] public async Task Get(string envelopeKey) { try { logger.Info("DocumentController/Get"); // Validate Envelope Key and load envelope envelopeService.EnsureValidEnvelopeKey(envelopeKey); EnvelopeResponse response = envelopeService.LoadEnvelope(envelopeKey); // Load document info var Request = ControllerContext.HttpContext.Request; var document = envelopeService.GetDocument(Request, envelopeKey); // Load the document from disk var bytes = await envelopeService.GetDocumentContents(document); // Return the document as bytes return File(bytes, "application/octet-stream"); } catch (Exception e) { return ErrorResponse(e); } } [HttpPost] [Route("api/document/{envelopeKey}")] public async Task Update(string envelopeKey) { try { logger.Info("DocumentController/Update"); // Validate Envelope Key and load envelope envelopeService.EnsureValidEnvelopeKey(envelopeKey); EnvelopeResponse response = envelopeService.LoadEnvelope(envelopeKey); // Load Document info var Request = ControllerContext.HttpContext.Request; var document = envelopeService.GetDocument(Request, envelopeKey); // Update the document with new data await envelopeService.UpdateDocument(Request.Body, document.Filepath); // Add history entry envelopeService.InsertHistoryEntrySigned(response); return Ok(); } catch (Exception e) { return ErrorResponse(e); } } } }