first working version of zugferd rest service
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using DigitalData.Modules.Interfaces;
|
||||
using DigitalData.Modules.Logging;
|
||||
|
||||
namespace ZUGFeRDRESTService.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ValidationController : ControllerBase
|
||||
{
|
||||
public static string RESPONSE_OK = "OK";
|
||||
public static string RESPONSE_ERROR = "ERROR";
|
||||
|
||||
private readonly ZUGFeRDInterface _zugferd;
|
||||
private readonly IDatabase _database;
|
||||
|
||||
private readonly DigitalData.Modules.Logging.LogConfig _logConfig;
|
||||
private readonly DigitalData.Modules.Logging.Logger _logger;
|
||||
|
||||
private readonly PropertyValues _props;
|
||||
private readonly Dictionary<String, XmlItemProperty> _propertyMap;
|
||||
|
||||
public class ValidationResponse
|
||||
{
|
||||
public string status;
|
||||
public string message;
|
||||
public List<string> errors;
|
||||
|
||||
public ValidationResponse()
|
||||
{
|
||||
status = RESPONSE_OK;
|
||||
message = String.Empty;
|
||||
errors = new List<string>();
|
||||
}
|
||||
}
|
||||
|
||||
public ValidationController(ILogging logging, IDatabase database)
|
||||
{
|
||||
_logConfig = logging.LogConfig;
|
||||
_logger = _logConfig.GetLogger();
|
||||
|
||||
_logger.Debug("Validation Controller initializing");
|
||||
|
||||
_database = database;
|
||||
var oGDPictureKey = database.GetGDPictureKey();
|
||||
var oPropertyMap = database.GetPropertyMap();
|
||||
|
||||
_propertyMap = oPropertyMap;
|
||||
|
||||
_zugferd = new ZUGFeRDInterface(_logConfig, oGDPictureKey);
|
||||
_props = new PropertyValues(_logConfig);
|
||||
|
||||
_logger.Debug("Validation Controller initialized!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// POST: api/Validation
|
||||
/// </summary>
|
||||
/// <param name="files">This parameter's name needs to correspond to the html form's file-input name</param>
|
||||
[HttpPost]
|
||||
public async Task<ValidationResponse> Post(IFormFile file, string user_id)
|
||||
{
|
||||
string oFilePath;
|
||||
string oFileName;
|
||||
|
||||
oFilePath = Path.Combine(Path.GetTempPath(), file.FileName);
|
||||
oFileName = file.FileName;
|
||||
|
||||
using FileStream oStream = new FileStream(oFilePath, FileMode.Create);
|
||||
await file.CopyToAsync(oStream);
|
||||
|
||||
CrossIndustryDocumentType oDocument;
|
||||
|
||||
try
|
||||
{
|
||||
oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oFilePath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex);
|
||||
return new ValidationResponse()
|
||||
{
|
||||
status = RESPONSE_ERROR,
|
||||
message = "The uploaded file is not a valid ZUGFeRD Invoice!"
|
||||
};
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
PropertyValues.CheckPropertyValuesResult oResult = _props.CheckPropertyValues(oDocument, _propertyMap, "MESSAGEID");
|
||||
|
||||
if (oResult.MissingProperties.Count > 0)
|
||||
{
|
||||
return new ValidationResponse()
|
||||
{
|
||||
status = RESPONSE_ERROR,
|
||||
message = "The uploaded file is a valid ZUGFeRD Invoice but missing some properties!",
|
||||
errors = oResult.MissingProperties
|
||||
};
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex);
|
||||
return new ValidationResponse()
|
||||
{
|
||||
status = RESPONSE_ERROR,
|
||||
message = "The uploaded file is not a valid ZUGFeRD Invoice!"
|
||||
};
|
||||
}
|
||||
|
||||
return new ValidationResponse()
|
||||
{
|
||||
status = RESPONSE_OK,
|
||||
message = "The uploaded files is a valid ZUGFeRD Invoice!"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace ZUGFeRDRESTService.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ZugferdValidationController : ControllerBase
|
||||
{
|
||||
public static string RESPONSE_OK = "OK";
|
||||
public static string RESPONSE_ERROR = "ERROR";
|
||||
|
||||
private readonly IZugferdValidationDataService _dataService;
|
||||
|
||||
public class ZugferdValidationResponse
|
||||
{
|
||||
public string status;
|
||||
public string message;
|
||||
public List<string> errors;
|
||||
|
||||
|
||||
|
||||
public ZugferdValidationResponse()
|
||||
{
|
||||
status = RESPONSE_OK;
|
||||
message = String.Empty;
|
||||
errors = new List<string>();
|
||||
}
|
||||
}
|
||||
|
||||
public ZugferdValidationController(IZugferdValidationDataService dataService)
|
||||
{
|
||||
_dataService = dataService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// POST: api/ZugferdValidation
|
||||
/// </summary>
|
||||
/// <param name="files">This parameter's name needs to correspond to the html form's file-input name</param>
|
||||
[HttpPost]
|
||||
public async Task<ZugferdValidationResponse> Post(List<IFormFile> files)
|
||||
{
|
||||
var oFilePaths = new List<String>();
|
||||
var oFileNames = new List<String>();
|
||||
|
||||
if (files.Count == 0) {
|
||||
return new ZugferdValidationResponse()
|
||||
{
|
||||
status = RESPONSE_ERROR,
|
||||
message = "No File received!"
|
||||
};
|
||||
}
|
||||
|
||||
foreach (var formFile in files)
|
||||
{
|
||||
var oFilePath = Path.GetTempFileName();
|
||||
oFilePaths.Add(oFilePath);
|
||||
oFileNames.Add(formFile.FileName);
|
||||
|
||||
using (var oStream = new FileStream(oFilePath, FileMode.Create))
|
||||
{
|
||||
await formFile.CopyToAsync(oStream);
|
||||
}
|
||||
}
|
||||
|
||||
var oResponse = new ZugferdValidationResponse
|
||||
{
|
||||
message = "You uploaded the following file: " + oFileNames.First()
|
||||
};
|
||||
|
||||
return oResponse;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user