398 lines
14 KiB
C#
398 lines
14 KiB
C#
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using NLog;
|
|
using System.Collections.Generic;
|
|
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
|
|
|
|
/// <summary>
|
|
/// Company list (All)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<CompanyModel> CompanyAll()
|
|
{
|
|
List<CompanyModel> dbResult = new List<CompanyModel>();
|
|
// cerco su DB recuperando set di dati....
|
|
using (WDCDataContext localDbCtx = new WDCDataContext(_configuration))
|
|
{
|
|
try
|
|
{
|
|
// extracting entire set
|
|
dbResult = localDbCtx
|
|
.DbSetCompany
|
|
.OrderBy(x => x.CompanyName)
|
|
.ToList();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Error in CompanyAll:{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
|
|
#endregion Company methods
|
|
|
|
#region User methods
|
|
|
|
/// <summary>
|
|
/// Populating DTO to handle users
|
|
/// </summary>
|
|
/// <param name="userId">User id to search for</param>
|
|
/// <returns></returns>
|
|
public List<UserRoleClaimDTO> GetUserRoleClaimByUserId()
|
|
{
|
|
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();
|
|
|
|
//UserRoleClaimDTO userDTO = new UserRoleClaimDTO()
|
|
//{
|
|
// userName = item.UserName,
|
|
// rolesName = userRoles
|
|
//};
|
|
//dbResult.Add(userDTO);
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieving data from user/roles relation
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<UsersViewModel> GetUserView()
|
|
{
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieving data from users table
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<AspNetUsers> GetUsersAll()
|
|
{
|
|
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 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
|
|
|
|
/// <summary>
|
|
/// Retrieving data from roles table
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<AspNetRoles> GetRolesAll()
|
|
{
|
|
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;
|
|
}
|
|
|
|
#endregion Roles methods
|
|
|
|
#region Order status methods
|
|
/// <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> GetOrderStatus(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.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
|
|
|
|
/// <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;
|
|
}
|
|
|
|
#endregion Order methods
|
|
|
|
#region Door methods
|
|
|
|
/// <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)
|
|
.OrderBy(x => x.DoorId)
|
|
.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)
|
|
{
|
|
/* 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 == 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;
|
|
}
|
|
|
|
#endregion Door methods
|
|
}
|
|
} |