LevelOS-Core/DAL/DALLConfig.cs

252 lines
5.8 KiB
C#

using Microsoft.Data.SqlClient;
using MLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace DALL
{
public class DALLConfig
{
private readonly string connectionString;
public DALLConfig(string conexao)
{
connectionString = conexao;
}
#region CRUD
public bool Inserir(ModeloConfig modelo)
{
try
{
using SqlConnection conn = new(connectionString);
PropertyInfo[] props =
typeof(ModeloConfig)
.GetProperties()
.Where(p => p.Name != "ID_CONFIG")
.ToArray();
string colunas =
string.Join(",",
props.Select(p => p.Name));
string parametros =
string.Join(",",
props.Select(p => "@" + p.Name));
string sql = $@"
INSERT INTO Config
(
{colunas}
)
VALUES
(
{parametros}
)";
using SqlCommand cmd =
new(sql, conn);
PreencherParametros(cmd, modelo);
conn.Open();
return cmd.ExecuteNonQuery() > 0;
}
catch
{
return false;
}
}
public bool Alterar(ModeloConfig modelo)
{
try
{
using SqlConnection conn = new(connectionString);
PropertyInfo[] props =
typeof(ModeloConfig)
.GetProperties()
.Where(p => p.Name != "ID_CONFIG")
.ToArray();
string sets =
string.Join(",",
props.Select(p =>
$"{p.Name}=@{p.Name}"));
string sql = $@"
UPDATE Config
SET
{sets}
WHERE ID_CONFIG=@ID_CONFIG";
using SqlCommand cmd =
new(sql, conn);
PreencherParametros(cmd, modelo);
cmd.Parameters.AddWithValue(
"@ID_CONFIG",
modelo.ID_CONFIG);
conn.Open();
return cmd.ExecuteNonQuery() > 0;
}
catch
{
return false;
}
}
public bool Excluir(int id)
{
try
{
using SqlConnection conn =
new(connectionString);
string sql =
"DELETE FROM Config WHERE ID_CONFIG=@ID_CONFIG";
using SqlCommand cmd =
new(sql, conn);
cmd.Parameters.AddWithValue(
"@ID_CONFIG",
id);
conn.Open();
return cmd.ExecuteNonQuery() > 0;
}
catch
{
return false;
}
}
public ModeloConfig? Carregar(int id)
{
using SqlConnection conn =
new(connectionString);
string sql = @"
SELECT *
FROM Config
WHERE ID_CONFIG=@ID_CONFIG";
using SqlCommand cmd =
new(sql, conn);
cmd.Parameters.AddWithValue(
"@ID_CONFIG",
id);
conn.Open();
using SqlDataReader dr =
cmd.ExecuteReader();
if (dr.Read())
{
return PreencherModelo(dr);
}
return null;
}
public List<ModeloConfig> Listar()
{
List<ModeloConfig> lista = [];
using SqlConnection conn =
new(connectionString);
string sql =
"SELECT * FROM Config ORDER BY ID_CONFIG";
using SqlCommand cmd =
new(sql, conn);
conn.Open();
using SqlDataReader dr =
cmd.ExecuteReader();
while (dr.Read())
{
lista.Add(
PreencherModelo(dr));
}
return lista;
}
#endregion
#region HELPERS
private static ModeloConfig PreencherModelo(
SqlDataReader dr)
{
ModeloConfig modelo = new();
PropertyInfo[] props =
typeof(ModeloConfig)
.GetProperties();
foreach (PropertyInfo prop in props)
{
try
{
if (dr[prop.Name] != DBNull.Value)
{
prop.SetValue(
modelo,
Convert.ToString(
dr[prop.Name]));
}
}
catch
{
}
}
modelo.ID_CONFIG =
Convert.ToInt32(
dr["ID_CONFIG"]);
return modelo;
}
private static void PreencherParametros(
SqlCommand cmd,
ModeloConfig modelo)
{
PropertyInfo[] props =
typeof(ModeloConfig)
.GetProperties()
.Where(p => p.Name != "ID_CONFIG")
.ToArray();
foreach (PropertyInfo prop in props)
{
object? valor =
prop.GetValue(modelo);
cmd.Parameters.AddWithValue(
"@" + prop.Name,
valor ?? DBNull.Value);
}
}
#endregion
}
}