using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using NLog; using WebDoorCreator.Data.DbModels; using WebDoorCreator.Data.DTO; 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 /// /// Company list (All) /// /// public List CompanyGetByKey(int id) { List dbResult = new List(); // 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; } /// /// 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 #region User methods /// /// Populating DTO to handle users /// /// User id to search for /// public List GetUserRoleClaimByUserId() { List dbResult = new List(); 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(); //UserRoleClaimDTO userDTO = new UserRoleClaimDTO() //{ // userName = item.UserName, // rolesName = userRoles //}; //dbResult.Add(userDTO); } } return dbResult; } /// /// Retrieving data from user/roles relation /// /// public List GetUserView() { List dbResult = new List(); 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; } /// /// Retrieving data from users table /// /// public List GetUsersAll() { List dbResult = new List(); 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; } /// /// Retrieving data from users table filtered by id /// /// public AspNetUsers GetUsersById(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; } #endregion User methods #region Roles methods /// /// Retrieving data from roles table /// /// public List GetRolesAll() { List dbResult = new List(); 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; } #endregion Roles methods #region Order status methods /// /// Order status /// /// /// /// /// /// public List GetOrderStatus(int companyId, int orderStatus, DateTime dataFrom, DateTime dataTo) { List dbResult = new List(); 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.Date); var DataTo = new SqlParameter("@DataTo", dataTo.Date); var k = $"exec dbo.stp_OrderList_getFilt {companyId}, {orderStatus}, '{dataFrom.Date.ToString("yyyy/MM/dd")}', '{dataTo.Date.ToString("yyyy/MM/dd")}'"; 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; } #endregion Order status methods #region Order methods /// /// Adding a new order /// /// Record to add /// public async Task 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; } #endregion Order methods #region Door methods /// /// Getting door data by orderId /// /// public List DoorsGetByOrderId(int orderId) { List dbResult = new List(); // retrieving data from db using (WDCDataContext localDbCtx = new WDCDataContext(_configuration)) { try { // extracting entire set dbResult = localDbCtx .DbSetDoor .Where(x => x.OrderId == orderId) .OrderBy(x => x.DoorId) .ToList(); } catch (Exception exc) { Log.Error($"Error in DoorsGetByOrderId:{Environment.NewLine}{exc}"); } } return dbResult; } /// /// Modifying or adding a new door /// /// Record to edit or add /// public async Task DoorUpsert(DoorModel addEditRec) { bool fatto = false; //List dbResult = new List(); 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 DoorsAddMod: {Environment.NewLine}{exc}"); } } return fatto; } /// /// Adding or removing a single door /// /// Record id to edit or add /// States if it has to be added or removing a door /// public async Task DoorModQty(int doorId, bool isAdd) { /* crea nuovo metodo per modifica singola quantità porta: DOORMODQTY */ bool fatto = false; //List dbResult = new List(); 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; } #endregion Door methods #region ListValues Methods /// /// ListValues list (All) /// /// public List ListValuesGetAll(string tableName, string fieldName) { List dbResult = new List(); // cerco su DB recuperando set di dati.... using (WDCDataContext localDbCtx = new WDCDataContext(_configuration)) { try { // extracting entire set dbResult = localDbCtx .DbSetValues .Where(x => (x.TableName == tableName) && (x.FieldName == fieldName)) .OrderBy(x => x.ordinal) .ToList(); } catch (Exception exc) { Log.Error($"Error in ListValuesGetAll:{Environment.NewLine}{exc}"); } } return dbResult; } #endregion ListValues Methods #region Components Methods /// /// Adding a new Compo /// /// Record to add /// public async Task 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; } /// /// Adding a new graphic param /// /// Record to add /// public async Task 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; } #endregion Components Methods #region Door Operationm Methods /// /// Adding a new DoorOpType /// /// Record to add /// public async Task 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; } #endregion Door Operationm Methods } }