- completata serializzazione ricetta in modo parametrico/composto
This commit is contained in:
Samuele Locatelli
2023-02-13 11:55:50 +01:00
parent 14d4534e5c
commit 0b40b0cd22
15 changed files with 340 additions and 26 deletions
+81 -2
View File
@@ -60,6 +60,29 @@ namespace MP.Data.Controllers
{ }
return answ;
}
/// <summary>
/// Ricerca ricetta su MongoDB dato ID MongoDB
/// </summary>
/// <param name="mID"></param>
/// <returns></returns>
public async Task<RecipeModel> RecipeGetByID(string mID)
{
await Task.Delay(1);
RecipeModel answ = new RecipeModel();
try
{
// definisco filtro
var filtBuilder = Builders<RecipeModel>.Filter;
var filter = filtBuilder.Eq<string>("_id", mID);
var collectionData = mongoDb.GetCollection<RecipeModel>("RecipeArchive");
// recupero
answ = collectionData.Find(filter).Project<RecipeModel>("{_id: 0}").FirstOrDefault<RecipeModel>();
}
catch
{ }
return answ;
}
/// <summary>
/// Salva ricetta su MongoDB
/// </summary>
@@ -99,8 +122,6 @@ namespace MP.Data.Controllers
public RecipeModel InitRecipe(string confPath, int idxPODL, Dictionary<string, string> CalcArgs)
{
RecipeModel answ = new RecipeModel();
// recupero dati da PODL...
// per prima cosa leggo file di conf x inizializzare ricetta...
string fullPath = RecipePath(confPath);
bool fileOk = File.Exists(fullPath);
@@ -115,6 +136,64 @@ namespace MP.Data.Controllers
return answ;
}
/// <summary>
/// Ricetta in formato calcolato (string)
/// </summary>
public string CalcRecipe(RecipeModel currRecipe)
{
string answ = "";
// per prima cosa leggo file di conf x inizializzare ricetta...
string fullPath = RecipePath(currRecipe.TemplateFile);
bool fileOk = File.Exists(fullPath);
if (fileOk)
{
// preparo dati header
var headStrings = currRecipe.HeadVal.Select(x => $"\"{x.Key}\": \"{x.Value}\"").ToList();
string headVals = string.Join(",", headStrings);
// leggo template
string rawData = File.ReadAllText(fullPath);
// recupero configurazione x inizio/fine righe
string[] righe = rawData.Split(Environment.NewLine);
List<string> righeRed = new List<string>();
string sRow = "";
string eRow = "";
foreach (var riga in righe)
{
if (riga.Trim().StartsWith("||SROW:"))
{
sRow = riga.Trim().Replace("||SROW:", "").Replace("||", "");
}
else if (riga.Trim().StartsWith("||EROW:"))
{
eRow = riga.Trim().Replace("||EROW:", "").Replace("||", "");
}
else
{
righeRed.Add(riga);
}
}
// rigenero righe senza le parti da ripetere...
rawData = string.Join(Environment.NewLine, righeRed);
// effettuo sostituzione...
answ = rawData.Replace("||PlaceholderHeader||", headVals);
// per le righe prima calcolo blocchi ripetuti
List<string> rowComp = new List<string>();
foreach (var item in currRecipe.RowsVal)
{
var rowsStrings = item.Value.Select(x => $"\"{x.Key}\": \"{x.Value}\"").ToList();
rowComp.Add($"{Environment.NewLine}{sRow}{string.Join(",", rowsStrings)}{eRow}");
}
string rowsVals = string.Join(",", rowComp);
//...poi sostituisco
answ = answ.Replace("||PlaceholderRows||", rowsVals);
}
return answ;
}
private MongoClient client = new MongoClient("mongodb://localhost:27017");
private static IConfiguration _configuration;