770420ee37
- fig gestione dati <-> Mongo - fix grafico
155 lines
5.3 KiB
C#
155 lines
5.3 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using NLog.Fluent;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using MongoDB.Driver;
|
|
using MongoDB.Bson;
|
|
using MP.Data.MgModels;
|
|
using System.IO;
|
|
using MP.Data.Conf;
|
|
using Newtonsoft.Json;
|
|
using static MP.Data.MgModels.RecipeModel;
|
|
|
|
namespace MP.Data.Controllers
|
|
{
|
|
public class MpMongoController : IDisposable
|
|
{
|
|
|
|
public MpMongoController(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
string mongoConf = _configuration.GetConnectionString("MongoConnect");
|
|
client = new MongoClient(mongoConf);
|
|
mongoDb = client.GetDatabase("MAPO");
|
|
Log.Info("Avviata classe MpMongoController");
|
|
}
|
|
/// <summary>
|
|
/// Database corrente MongoDB
|
|
/// </summary>
|
|
private IMongoDatabase mongoDb;
|
|
|
|
public void Dispose()
|
|
{
|
|
client = null;
|
|
_configuration = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ricerca ricetta su MongoDB dato PODL
|
|
/// </summary>
|
|
/// <param name="idxPODL"></param>
|
|
/// <returns></returns>
|
|
public async Task<RecipeModel> RecipeGetByPODL(int idxPODL)
|
|
{
|
|
await Task.Delay(1);
|
|
RecipeModel answ = new RecipeModel();
|
|
try
|
|
{
|
|
// definisco filtro
|
|
var filtBuilder = Builders<RecipeModel>.Filter;
|
|
var filter = filtBuilder.Eq<int>("IdxPODL", idxPODL);
|
|
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>
|
|
/// <param name="currRecord"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> RecipeSetByPODL(RecipeModel currRecord)
|
|
{
|
|
await Task.Delay(1);
|
|
bool answ = false;
|
|
try
|
|
{
|
|
// definisco filtro
|
|
var filtBuilder = Builders<RecipeModel>.Filter;
|
|
var filter = filtBuilder.Eq<int>("IdxPODL", currRecord.IdxPODL);
|
|
var collectionData = mongoDb.GetCollection<RecipeModel>("RecipeArchive");
|
|
// elimino old
|
|
collectionData.DeleteMany(filter);
|
|
// aggiungo
|
|
collectionData.InsertOne(currRecord);
|
|
answ = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Eccezione in RecipeSetByPODL{Environment.NewLine}{exc}");
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Init ricetta dato PODL + conf
|
|
/// </summary>
|
|
/// <param name=""></param>
|
|
/// <returns></returns>
|
|
public RecipeModel InitRecipe(int idxPODL, string confPath)
|
|
{
|
|
RecipeModel answ = new RecipeModel();
|
|
// per prima cosa leggo file di conf x inizializzare ricetta...
|
|
string fullPath = RecipePath(confPath);
|
|
bool fileOk = File.Exists(fullPath);
|
|
if (fileOk)
|
|
{
|
|
string rawData = File.ReadAllText(fullPath);
|
|
var currRecipe = JsonConvert.DeserializeObject<RecipeConfig>(rawData);
|
|
// copio la mia ricetta come conf...
|
|
answ = new RecipeModel(idxPODL, currRecipe);
|
|
|
|
//answ = new RecipeModel()
|
|
//{
|
|
// IdxPODL = idxPODL,
|
|
// TemplateFile = currRecipe.TemplateFile,
|
|
// HeadConf = currRecipe.HeadConf,
|
|
// RowsConf = currRecipe.RowsConf,
|
|
// //HeadVal = currRecipe.HeadConf.ListKeys,
|
|
// HeadVal = RecipeModel.ConvertToValues(currRecipe.HeadConf.ListKeys),
|
|
// // aggiungo 1 riga...
|
|
// RowsVal = new Dictionary<string, Dictionary<string, string>>()
|
|
// {
|
|
// { "1", currRecipe.RowsConf.ListKeys}
|
|
// }
|
|
|
|
// //IdxPODL = idxPODL,
|
|
// //TemplateFile = currRecipe.TemplateFile,
|
|
// //HeadConf = currRecipe.HeadConf,
|
|
// //RowsConf = currRecipe.RowsConf,
|
|
// ////HeadVal = currRecipe.HeadConf.ListKeys,
|
|
// //HeadVal = RecipeModel.ConvertToObj(currRecipe.HeadConf.ListKeys),
|
|
// //// aggiungo 1 riga...
|
|
// //RowsVal = new Dictionary<string, Dictionary<string, Element>>()
|
|
// //{
|
|
// // //{ "1", rigaBase }
|
|
// // { "1", RecipeModel.ConvertToObj(currRecipe.RowsConf.ListKeys)}
|
|
// //}
|
|
//};
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
private MongoClient client = new MongoClient("mongodb://localhost:27017");
|
|
|
|
private static IConfiguration _configuration;
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
public static string RecipePath(string ruleName)
|
|
{
|
|
return string.Format($"Recipe/{ruleName}");
|
|
}
|
|
|
|
}
|
|
}
|