134 lines
4.8 KiB
C#
134 lines
4.8 KiB
C#
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
|
|
|
|
/// <summary>
|
|
/// Recupera l'elenco Company (ALL)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<CompanyModel> CompanyAll()
|
|
{
|
|
// init dati necessari
|
|
List<CompanyModel> dbResult = new List<CompanyModel>();
|
|
// 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;
|
|
}
|
|
|
|
/// <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> GetUserRoleClamByUserId()
|
|
{
|
|
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).ToList();
|
|
UserRoleClaimDTO userDTO = new UserRoleClaimDTO()
|
|
{
|
|
userName = item.UserName,
|
|
rolesName = userRoles
|
|
};
|
|
dbResult.Add(userDTO);
|
|
}
|
|
}
|
|
return dbResult;
|
|
}
|
|
|
|
#endregion user methods
|
|
}
|
|
} |