using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebDoorCreator.Data.DbModels;
namespace WebDoorCreator.Data.Controllers
{
public class WebDoorCreatorController : IDisposable
{
private static IConfiguration _configuration;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
public WebDoorCreatorController(IConfiguration configuration)
{
_configuration = configuration;
}
public void Dispose()
{
// Clear database context
//Log.Info("Dispose di GWMSController");
}
#region Company methods
///
/// Recupera l'elenco Company (ALL)
///
///
public List CompanyAll()
{
// init dati necessari
List dbResult = new List();
// cerco su DB recuperando set di dati....
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// recupero intero set...
dbResult = localDbCtx
.DbSetCompany
.OrderBy(x => x.CompanyName)
.ToList();
}
catch (Exception exc)
{
Log.Error($"Error in CompanyAll:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
///
/// Adding a new company
///
/// Record to edit or add
///
public async Task CompanyAddMod(CompanyModel addEditRec)
{
bool fatto = false;
//List dbResult = new List();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
var currRec = localDbCtx
.DbSetCompany
.Where(x => x.CompanyId == addEditRec.CompanyId)
.FirstOrDefault();
if (currRec != null) //if is not null edit the record found
{
currRec.CompanyId = addEditRec.CompanyId;
currRec.CompanyName = addEditRec.CompanyName;
currRec.CompanyExtCode = addEditRec.CompanyExtCode;
currRec.Address = addEditRec.Address;
currRec.City = addEditRec.City;
currRec.State = addEditRec.State;
currRec.ZipCode = addEditRec.ZipCode;
currRec.VAT = addEditRec.VAT;
currRec.PrivateNote = addEditRec.PrivateNote;
currRec.CompanyToken = addEditRec.CompanyToken;
localDbCtx.Entry(currRec).State = EntityState.Modified;
}
else //if is null add the record as new in the table
{
localDbCtx
.DbSetCompany
.Add(addEditRec);
}
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante CompanyAddMod;{Environment.NewLine}{exc}");
}
}
return fatto;
}
#endregion company methods
}
}