Files
mapo-core/MP.IOC/Controllers/RecipeArchiveController.cs
T
Samuele Locatelli ef2fd78269 Fix log x IOC
2023-04-05 09:32:09 +02:00

132 lines
4.4 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MP.IOC.Data;
using NLog;
using NLog.Fluent;
namespace MP.IOC.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RecipeArchiveController : ControllerBase
{
#region Public Constructors
public RecipeArchiveController(IConfiguration configuration, MpDataService DataService)
{
Log.Info("Starting RecipeArchiveController");
_configuration = configuration;
DService = DataService;
Log.Info("Avviata classe RecipeArchiveController");
}
#endregion Public Constructors
#region Public Methods
[HttpGet("GetByPODL")]
public async Task<string> GetByPODL(string idxMacc, int idxPODL)
{
string answ = "NO DATA";
string archPath = await DService.MacchineRecipeArchive(idxMacc);
var podlData = await DService.POdlGetByKey(idxPODL);
if (podlData != null)
{
string fileName = podlData?.Recipe ?? "";
string fullPath = Path.Combine(archPath, fileName);
if (!string.IsNullOrEmpty(fullPath))
{
try
{
if (System.IO.File.Exists(fullPath))
{
answ = System.IO.File.ReadAllText(fullPath);
if (string.IsNullOrEmpty(answ))
{
Log.Error($"Errore in GetByPODL: contenuto file vuoto | idxMacc: {idxMacc} | idxPODL: {idxPODL}");
}
}
else
{
Log.Error($"Errore in GetByPODL: il file non esiste | fullPath {fullPath}");
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in GetByPODL:{Environment.NewLine}{exc}");
}
}
else
{
Log.Error($"Errore in GetByPODL: fullPath null ");
}
}
else
{
Log.Error($"Errore in GetByPODL: podlData null | idxPODL: {idxPODL}");
}
return answ;
}
[HttpGet("GetFile")]
public async Task<string> GetFile(string idxMacc, string fileName)
{
string answ = "NO DATA";
string archPath = await DService.MacchineRecipeArchive(idxMacc);
if (!string.IsNullOrEmpty(archPath))
{
string fullPath = Path.Combine(archPath, fileName);
if (!string.IsNullOrEmpty(fullPath))
{
try
{
if (System.IO.File.Exists(fullPath))
{
answ = System.IO.File.ReadAllText(fullPath);
if (string.IsNullOrEmpty(answ))
{
Log.Error($"Errore in GetFile: contenuto file vuoto | idxMacc: {idxMacc} | fileName: {fileName}");
}
}
else
{
Log.Error($"Errore in GetFile: il file non esiste | fullPath {fullPath}");
}
}
catch (Exception exc)
{
Log.Error($"Eccezione in GetFile:{Environment.NewLine}{exc}");
}
}
else
{
Log.Error($"Errore in GetFile: fullPath null ");
}
}
else
{
Log.Error($"Errore in GetFile: archPath null ");
}
return answ;
}
#endregion Public Methods
#region Protected Properties
/// <summary>
/// Dataservice x accesso DB
/// </summary>
protected MpDataService DService { get; set; }
#endregion Protected Properties
#region Private Fields
private static IConfiguration _configuration = null!;
private static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
}
}