Files
webdoorcreator/WebDoorCreator.Data/Controllers/WebDoorCreatorController.cs
T
2023-04-28 10:59:58 +02:00

1075 lines
38 KiB
C#

using Microsoft.AspNetCore.DataProtection;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Configuration;
using NLog;
using System.Data;
using WebDoorCreator.Data.DbModels;
using WebDoorCreator.Data.DTO;
namespace WebDoorCreator.Data.Controllers
{
public class WebDoorCreatorController : IDisposable
{
#region Public Constructors
public WebDoorCreatorController(IConfiguration configuration)
{
_configuration = configuration;
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Adding a new company
/// </summary>
/// <param name="addEditRec">Record to edit or add</param>
/// <returns></returns>
public async Task<bool> CompanyAddMod(CompanyModel addEditRec)
{
bool fatto = false;
//List<ItemModel> dbResult = new List<ItemModel>();
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;
}
/// <summary>
/// Company list (All)
/// </summary>
/// <returns></returns>
public List<CompanyModel> CompanyGetByKey(int id)
{
List<CompanyModel> dbResult = new List<CompanyModel>();
// cerco su DB recuperando set di dati....
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
if (id == 0)
{
// extracting entire set
dbResult = localDbCtx
.DbSetCompany
.OrderBy(x => x.CompanyName)
.ToList();
}
else
{
var risultato = localDbCtx
.DbSetCompany
.Where(x => x.CompanyId == id)
.OrderBy(x => x.CompanyName)
.ToList();
if (risultato != null)
{
dbResult = risultato;
}
}
}
catch (Exception exc)
{
Log.Error($"Error in CompanyAll:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Adding a new Compo
/// </summary>
/// <param name="addRec">Record to add</param>
/// <returns></returns>
public async Task<bool> CompoAdd(HardwareModel addRec)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
localDbCtx
.DbSetHardware
.Add(addRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante CompoAdd: {Environment.NewLine}{exc}");
}
}
return fatto;
}
public void Dispose()
{
// Clear database context
//Log.Info("Dispose di GWMSController");
}
/// <summary>
/// Remove door
/// </summary>
/// <param name="DoorId">ID da eliminare</param>
/// <returns></returns>
public async Task<bool> DoorDelete(int DoorId)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
var currRec = localDbCtx
.DbSetDoor
.Where(x => x.DoorId == DoorId)
.FirstOrDefault();
// procedo solo se c'è
if (currRec != null)
{
var newDbRec = localDbCtx
.DbSetDoor
.Remove(currRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorDelete: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Adding a new door
/// </summary>
/// <param name="newRec">Record to edit or add</param>
/// <returns></returns>
public async Task<int> DoorInsert(DoorModel newRec)
{
int newId = 0;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
var currRec = localDbCtx
.DbSetDoor
.Where(x => x.DoorId == newRec.DoorId)
.FirstOrDefault();
// procedo solo se non c'è già
if (currRec == null)
{
var newDbRec = localDbCtx
.DbSetDoor
.Add(newRec);
await localDbCtx.SaveChangesAsync();
newId = newRec.DoorId;
}
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorInsert: {Environment.NewLine}{exc}");
}
}
return newId;
}
/// <summary>
/// Adding or removing a single door
/// </summary>
/// <param name="doorId">Record id to edit or add</param>
/// <param name="isAdd">States if it has to be added or removing a door</param>
/// <returns></returns>
public async Task<bool> DoorModQty(int doorId, bool isAdd)
{
/* crea nuovo metodo per modifica singola quantità porta: DOORMODQTY */
bool fatto = false;
//List<ItemModel> dbResult = new List<ItemModel>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
var currRec = localDbCtx
.DbSetDoor
.Where(x => x.DoorId == doorId)
.FirstOrDefault();
if (currRec != null) //if is not null edit the record found
{
if (isAdd)
{
currRec.Quantity = currRec.Quantity + 1;
}
else
{
currRec.Quantity = currRec.Quantity - 1;
}
localDbCtx.Entry(currRec).State = EntityState.Modified;
}
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorModQty: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Retrieving current data from door
/// </summary>
/// <returns></returns>
public List<DoorOpModel> DoorOpGetByDoorId(int doorId)
{
List<DoorOpModel> dbResult = new List<DoorOpModel>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// extracting entire set
dbResult = localDbCtx
.DbSetDoorOp
.Where(x => x.DoorId == doorId)
.ToList();
}
catch (Exception exc)
{
Log.Error($"Error in DoorOpGetByDoorId:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Adding door's OP
/// </summary>
/// <param name="newOpRec">Records to add</param>
/// <returns></returns>
public async Task<bool> DoorOpInsert(List<DoorOpModel> newOpRec)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
localDbCtx
.DbSetDoorOp
.AddRange(newOpRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorInsert: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Update door's OP
/// </summary>
/// <param name="modOpRec">Records to add</param>
/// <returns></returns>
public async Task<bool> DoorOpUpdate(DoorOpModel modOpRec)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
var currRec = localDbCtx
.DbSetDoorOp
.Where(x => x.DoorId == modOpRec.DoorId && x.DoorOpId == modOpRec.DoorOpId)
.FirstOrDefault();
if (currRec != null) //if is not null edit the record found
{
currRec.JsoncActVal = modOpRec.JsoncActVal;
currRec.JsoncConfigVal = modOpRec.JsoncConfigVal;
currRec.userConfirm = modOpRec.userConfirm;
currRec.DtConfirm = modOpRec.DtConfirm;
localDbCtx.Entry(currRec).State = EntityState.Modified;
}
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorOpUpdate: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Delete doorOp instance
/// </summary>
/// <param name="modOpRec">Record to delete</param>
/// <returns></returns>
public async Task<bool> DoorOpDelete(DoorOpModel modOpRec)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
var currRec = localDbCtx
.DbSetDoorOp
.Where(x => x.DoorId == modOpRec.DoorId && x.DoorOpId == modOpRec.DoorOpId)
.FirstOrDefault();
//if is not null edit the record found
if (currRec != null)
{
var recToRem = localDbCtx
.DbSetDoorOp
.Remove(currRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorOpDelete: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Delete doorOp list
/// </summary>
/// <param name="modOpRec">Record to delete</param>
/// <returns></returns>
public async Task<bool> DoorOpDeleteRange(List<DoorOpModel> listOpRec)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
localDbCtx
.DbSetDoorOp
.RemoveRange(listOpRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorOpDeleteRange: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Adding a new DoorOpType
/// </summary>
/// <param name="addRec">Record to add</param>
/// <returns></returns>
public async Task<bool> DoorOpTypeAdd(DoorOpTypeModel addRec)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
localDbCtx
.DbSetDoorOpType
.Add(addRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorOpTypeAdd:{Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Adding a new DoorOpType
/// </summary>
/// <param name="listRec">Record to add</param>
/// <returns></returns>
public async Task<bool> DoorOpTypeAddRange(List<DoorOpTypeModel> listRec)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
localDbCtx
.DbSetDoorOpType
.AddRange(listRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorOpTypeAddRange:{Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Retrieving data from door operation type table
/// </summary>
/// <returns></returns>
public List<DoorOpTypeModel> DoorOpTypeGetAll()
{
List<DoorOpTypeModel> dbResult = new List<DoorOpTypeModel>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// extracting entire set
dbResult = localDbCtx
.DbSetDoorOpType
.OrderBy(x => x.DoorOpTypId)
.ToList();
}
catch (Exception exc)
{
Log.Error($"Error in DoorOpTypeGetAll:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
public async Task<List<DoorOpTypeModel>> DoorOpTypeGetDefault()
{
List<DoorOpTypeModel> dbResult = new List<DoorOpTypeModel>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
dbResult = localDbCtx
.DbSetDoorOpType
.Where(x => x.IsDefault)
.ToList();
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorOpTypeGetDefault: {Environment.NewLine}{exc}");
}
}
await Task.Delay(1);
return dbResult;
}
/// <summary>
/// Getting door data by orderId
/// </summary>
/// <returns></returns>
public List<DoorModel> DoorsGetByOrderId(int orderId)
{
List<DoorModel> dbResult = new List<DoorModel>();
// retrieving data from db
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// extracting entire set
dbResult = localDbCtx
.DbSetDoor
.Where(x => x.OrderId == orderId)
.Include(o => o.OrderNav)
.Include(t => t.TypeNav)
.OrderBy(x => x.DoorId)
.AsNoTracking()
.ToList();
}
catch (Exception exc)
{
Log.Error($"Error in DoorsGetByOrderId:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Modifying or adding a new door
/// </summary>
/// <param name="addEditRec">Record to edit or add</param>
/// <returns></returns>
public async Task<bool> DoorUpsert(DoorModel addEditRec)
{
bool fatto = false;
//List<ItemModel> dbResult = new List<ItemModel>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
var currRec = localDbCtx
.DbSetDoor
.Where(x => x.DoorId == addEditRec.DoorId)
.FirstOrDefault();
if (currRec != null) //if is not null edit the record found
{
currRec.Quantity = addEditRec.Quantity;
localDbCtx.Entry(currRec).State = EntityState.Modified;
}
else //if is null add the record as new in the table
{
localDbCtx
.DbSetDoor
.Add(addEditRec);
}
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante DoorUpsert: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Estraggo tutte le lingue disponibili per questa applicazione
/// </summary>
/// <returns></returns>
public List<LanguageModel> LanguageGetAll()
{
List<LanguageModel> dbResult = new List<LanguageModel>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// extracting entire set
dbResult = localDbCtx
.DbSetLanguages
.ToList();
}
catch (Exception exc)
{
Log.Error($"Error in LanguageGetAll:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// ListValues list (All)
/// </summary>
/// <returns></returns>
public List<ListValuesModel> ListValuesGetAll(string tableName, string fieldName)
{
List<ListValuesModel> dbResult = new List<ListValuesModel>();
// cerco su DB recuperando set di dati....
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
if (tableName != "*" && fieldName != "*")
{
// extracting entire set
dbResult = localDbCtx
.DbSetValues
.Where(x => (x.TableName == tableName) && (x.FieldName == fieldName))
.OrderBy(x => x.Ordinal)
.ToList();
}
else if (tableName == "*" && fieldName != "*")
{
dbResult = localDbCtx
.DbSetValues
.Where(x => (x.FieldName == fieldName))
.OrderBy(x => x.Ordinal)
.ToList();
}
else if (tableName != "*" && fieldName == "*")
{
dbResult = localDbCtx
.DbSetValues
.Where(x => (x.TableName == tableName))
.OrderBy(x => x.Ordinal)
.ToList();
}
}
catch (Exception exc)
{
Log.Error($"Error in ListValuesGetAll:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Adding a new list value
/// </summary>
/// <param name="addRec">Record to add</param>
/// <returns></returns>
public async Task<bool> ListValuesAdd(List<ListValuesTempModel> addList)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// stored di reset ListValues
var storedRes = localDbCtx
.Database
.ExecuteSqlRaw("exec dbo.stp_ListVal_Prepare");
await localDbCtx.SaveChangesAsync();
// import massivo dati in tab temp
localDbCtx
.DbSetValuesTemp
.AddRange(addList);
await localDbCtx.SaveChangesAsync();
// stored di merge dati in vocabolario
storedRes = localDbCtx
.Database
.ExecuteSqlRaw("exec dbo.stp_ListVal_Import");
fatto = true;
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante ListValuesAdd: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Adding a new order
/// </summary>
/// <param name="addRec">Record to add</param>
/// <returns></returns>
public async Task<bool> OrderAdd(OrderModel addRec)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
localDbCtx
.DbSetOrders
.Add(addRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante OrderAd: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Remove order
/// </summary>
/// <param name="OrdId">Record to add</param>
/// <returns></returns>
public async Task<bool> OrderRem(int OrdId)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
var selRec = localDbCtx
.DbSetOrders
.Where(x => x.OrderId == OrdId)
.FirstOrDefault();
if (selRec != null)
{
localDbCtx.DbSetOrders.Remove(selRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
}
catch (Exception exc)
{
Log.Error($"Eccezione durante OrderRem: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Order status
/// </summary>
/// <param name="companyId"></param>
/// <param name="orderStatus"></param>
/// <param name="dataFrom"></param>
/// <param name="dataTo"></param>
/// <returns></returns>
public List<OrderStatusViewModel> OrderStatusGetAll(int companyId, int orderStatus, DateTime dataFrom, DateTime dataTo)
{
List<OrderStatusViewModel> dbResult = new List<OrderStatusViewModel>();
using (var dbCtx = new WDCDataContext(_configuration))
{
try
{
var CompanyId = new SqlParameter("@CompanyId", companyId);
var OrderStatus = new SqlParameter("@OrderStatus", orderStatus);
var DataFrom = new SqlParameter("@DataFrom", dataFrom);
var DataTo = new SqlParameter("@DataTo", dataTo);
dbResult = dbCtx
.DbSetOrderStatus
.FromSqlRaw("exec dbo.stp_OrderList_getFilt @CompanyId, @OrderStatus, @DataFrom, @DataTo", CompanyId, OrderStatus, DataFrom, DataTo)
.AsNoTracking()
.ToList();
}
catch (Exception exc)
{
Log.Error($"Error in GetOrderStatus:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Adding a new graphic param
/// </summary>
/// <param name="addRec">Record to add</param>
/// <returns></returns>
public async Task<bool> ParamAdd(GraphicParamsModel addRec)
{
bool fatto = false;
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
localDbCtx
.DbSetGraphicParams
.Add(addRec);
await localDbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante ParamAdd: {Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Getting all the graphic parameters by hw id
/// </summary>
/// <param name="hwId">Hardware's id to search for</param>
/// <returns></returns>
public List<GraphicParamsModel> ParamGetByHwId(int hwId)
{
List<GraphicParamsModel> dbResult = new List<GraphicParamsModel>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
dbResult = localDbCtx
.DbSetGraphicParams
.Where(x => x.compoId == hwId)
.ToList();
}
catch (Exception exc)
{
Log.Error($"Eccezione durante ParamGetByHwId: {Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Retrieving data from roles table
/// </summary>
/// <returns></returns>
public List<AspNetRoles> RolesGetAll()
{
List<AspNetRoles> dbResult = new List<AspNetRoles>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// extracting entire set
dbResult = localDbCtx
.DbSetRoles
.OrderBy(x => x.Name)
.ToList();
}
catch (Exception exc)
{
Log.Error($"Error in GetRolesAll:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// truncate tables for testing purposes
/// </summary>
/// <returns></returns>
public bool TestTablesTruncate()
{
bool dbResult = false;
using (var dbCtx = new WDCDataContext(_configuration))
{
try
{
dbCtx
.Database
.ExecuteSqlRaw("EXEC man.[stp_TruncTables]");
dbResult = true;
}
catch (Exception exc)
{
Log.Error($"Error in TablesTruncate:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Populating DTO to handle users
/// </summary>
/// <param name="userId">User id to search for</param>
/// <returns></returns>
public List<UserRoleClaimDTO> UserRoleClaimGetByUserId()
{
List<UserRoleClaimDTO> dbResult = new List<UserRoleClaimDTO>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
var users = localDbCtx.DbSetUsers.ToList();
foreach (var item in users)
{
var userRoles = localDbCtx.DbSetUserRoles
.Where(x => x.UserId == item.Id)
.Include(u => u.UsersNav)
.Include(u => u.RolesNav)
.ToList();
}
}
return dbResult;
}
/// <summary>
/// Retrieving data from users table
/// </summary>
/// <returns></returns>
public List<AspNetUsers> UsersGetAll()
{
List<AspNetUsers> dbResult = new List<AspNetUsers>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// extracting entire set
dbResult = localDbCtx
.DbSetUsers
.OrderBy(x => x.Id)
.ToList();
}
catch (Exception exc)
{
Log.Error($"Error in GetUserRole:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Retrieving data from users table filtered by id
/// </summary>
/// <returns></returns>
public AspNetUsers UsersGetById(string userId)
{
AspNetUsers dbResult = new AspNetUsers();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// extracting entire set
var risultato = localDbCtx
.DbSetUsers
.Where(x => x.Id == userId)
.AsNoTracking()
.OrderBy(x => x.Id)
.FirstOrDefault();
if (risultato != null)
{
dbResult = risultato;
}
else
{
dbResult = new AspNetUsers();
}
}
catch (Exception exc)
{
Log.Error($"Error in GetUsersById:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
/// <summary>
/// Retrieving data from user/roles relation
/// </summary>
/// <returns></returns>
public List<UsersViewModel> UserViewGetAll()
{
List<UsersViewModel> dbResult = new List<UsersViewModel>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// extracting entire set
dbResult = localDbCtx
.DbSetUsersView
.OrderBy(x => x.UserId)
.ToList();
}
catch (Exception exc)
{
Log.Error($"Error in GetUserRole:{Environment.NewLine}{exc}");
}
}
return dbResult;
}
public Dictionary<string, Dictionary<string, string>> VocLemmaGetAll()
{
Dictionary<string, Dictionary<string, string>> DTOResult = new Dictionary<string, Dictionary<string, string>>();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
List<string> lingue = new List<string>()
{
"IT",
"EN"
};
// extracting entire set
var allVoc = localDbCtx
.DbSetVocabulary
.AsNoTracking()
.ToList();
foreach (var lingua in lingue)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (var lemma in allVoc.Where(x => x.Lingua == lingua))
{
dict.Add(lemma.Lemma, lemma.Traduzione);
}
DTOResult.Add(lingua, dict);
}
}
catch (Exception exc)
{
Log.Error($"Error in VocLemmaGetAll:{Environment.NewLine}{exc}");
}
}
return DTOResult;
}
/// <summary>
/// Aggiunta di un nuovo set di lemmi
/// </summary>
/// <param name="listNewTerms"></param>
/// <returns></returns>
public async Task<bool> VocLemmaInsert(List<VocabularyTempModel> listNewTerms)
{
bool fatto = false;
//var o = listNewTerms.Where(x => x.Traduzione.Length > 200).ToList();
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
{
try
{
// stored di reset vocabolario
var storedRes = localDbCtx
.Database
.ExecuteSqlRaw("exec dbo.stp_Voc_Prepare");
await localDbCtx.SaveChangesAsync();
// import massivo dati in tab temp
localDbCtx
.DbSetVocabularyTemp
.AddRange(listNewTerms);
await localDbCtx.SaveChangesAsync();
// stored di merge dati in vocabolario
storedRes = localDbCtx
.Database
.ExecuteSqlRaw("exec dbo.stp_Voc_Import");
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante VocLemmaInsert: {Environment.NewLine}{exc}");
}
}
return fatto;
}
#endregion Public Methods
#region Private Fields
private static IConfiguration _configuration;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
}
}