88 lines
2.1 KiB
C#
88 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using EgtBEAMWALL.DataLayer.DatabaseModels;
|
|
|
|
namespace EgtBEAMWALL.DataLayer.Controllers
|
|
{
|
|
public class DbController : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private AdminContext adbCtx;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Fields
|
|
|
|
/// <summary>
|
|
/// Singleton gestione
|
|
/// </summary>
|
|
public static DbController man = new DbController();
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public DbController()
|
|
{
|
|
// Initialize database context for ADMIN
|
|
adbCtx = new AdminContext(DbConfig.ADMIN_CONNECTION_STRING);
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
public bool checkCreateUser(string username, string pwd)
|
|
{
|
|
bool answ = false;
|
|
// ricerca utente...
|
|
var foundUser = adbCtx
|
|
.UserList
|
|
.Where(x => x.User == username)
|
|
.ToList();
|
|
if (foundUser != null && foundUser.Count > 0)
|
|
{
|
|
answ = true;
|
|
}
|
|
if (!answ)
|
|
{
|
|
// creo utente
|
|
string sqlCommand = $"ALTER USER '{username}'@'localhost' IDENTIFIED BY '{pwd}'; FLUSH PRIVILEGES;";
|
|
adbCtx.Database.ExecuteSqlCommand(sqlCommand);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database context
|
|
adbCtx.Dispose();
|
|
}
|
|
|
|
public bool ResetDb()
|
|
{
|
|
bool answ = false;
|
|
|
|
try
|
|
{
|
|
adbCtx
|
|
.Database
|
|
.SqlQuery<int>("CALL stp_ResetDb()");
|
|
answ = true;
|
|
|
|
adbCtx.SaveChanges();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Console.WriteLine($"EXCEPTION on ResetDb: {exc}");
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |