Files
lux/Lux.Data/Controllers/LuxController.cs
T

112 lines
3.8 KiB
C#

using Lux.Data.DbModel;
using Microsoft.Extensions.Configuration;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Lux.Data.Controllers
{
public class LuxController
{
// manca costruttore parametrico contoller...
#region Public Methods
public List<ArticoloModel> ArticoliGetAll()
{
List<ArticoloModel> dbResult = new List<ArticoloModel>();
//using (DataLayerContext dbCtx = new DataLayerContext(_configuration))
using (DataLayerContext dbCtx = new DataLayerContext())
{
try
{
dbResult = dbCtx
.DbSetArticolo
.ToList();
}
catch (Exception exc)
{
Log.Error($"Eccezione durante ArticoliGetAll{Environment.NewLine}{exc}");
}
}
return dbResult;
}
public List<ArticoloModel> ArticoliGetSearch(string term)
{
List<ArticoloModel> dbResult = new List<ArticoloModel>();
//using (DataLayerContext dbCtx = new DataLayerContext(_configuration))
using (DataLayerContext dbCtx = new DataLayerContext())
{
try
{
dbResult = dbCtx
.DbSetArticolo
.Where(x => x.Description.Contains(term))
.ToList();
}
catch (Exception exc)
{
Log.Error($"Eccezione durante ArticoliGetSearch{Environment.NewLine}{exc}");
}
}
return dbResult;
}
public bool ArticoliUpsert(ArticoloModel newRec)
{
bool answ = false;
//using (DataLayerContext dbCtx = new DataLayerContext(_configuration))
using (DataLayerContext dbCtx = new DataLayerContext())
{
try
{
var currRec = dbCtx
.DbSetArticolo
.Where(x => x.ArtID == newRec.ArtID)
.FirstOrDefault();
// se trovato --> aggiorno
if (currRec != null)
{
currRec.Description = newRec.Description;
currRec.CodFornitore = newRec.CodFornitore;
currRec.CodParl = newRec.CodParl;
currRec.CodArt = newRec.CodArt;
currRec.Cost = newRec.Cost;
currRec.Description = newRec.Description;
currRec.IsMake = newRec.IsMake;
currRec.IsService = newRec.IsService;
currRec.Margin = newRec.Margin;
dbCtx.Entry(currRec).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
}
// se mancasse --> aggiungo
else
{
dbCtx.DbSetArticolo.Add(newRec);
}
// salvo...
dbCtx.SaveChanges();
}
catch (Exception exc)
{
Log.Error($"Eccezione durante ArticoliGetSearch{Environment.NewLine}{exc}");
}
}
return answ; ;
}
#endregion Public Methods
#region Private Fields
private static IConfiguration _configuration;
private static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
}
}