111 lines
2.6 KiB
C#
111 lines
2.6 KiB
C#
using System;
|
|
using System.Data;
|
|
using DALL;
|
|
using MLL;
|
|
|
|
namespace BLL
|
|
{
|
|
public class BLLBancos
|
|
{
|
|
private DALBancos dal;
|
|
|
|
public BLLBancos(string conexao)
|
|
{
|
|
dal = new DALBancos(conexao);
|
|
}
|
|
|
|
#region INSERT
|
|
public bool Inserir(ModeloBancos banco)
|
|
{
|
|
string erro = Validar(banco);
|
|
|
|
if (!string.IsNullOrEmpty(erro))
|
|
throw new Exception(erro);
|
|
|
|
return dal.Inserir(banco.NUMERO, banco.NOME);
|
|
}
|
|
#endregion
|
|
|
|
#region UPDATE
|
|
public bool Alterar(ModeloBancos banco)
|
|
{
|
|
if (banco.ID_BANCOS <= 0)
|
|
throw new Exception("Banco inválido.");
|
|
|
|
string erro = Validar(banco);
|
|
|
|
if (!string.IsNullOrEmpty(erro))
|
|
throw new Exception(erro);
|
|
|
|
return dal.Alterar(banco.ID_BANCOS, banco.NUMERO, banco.NOME);
|
|
}
|
|
#endregion
|
|
|
|
#region DELETE
|
|
public bool Excluir(int id)
|
|
{
|
|
if (id <= 0)
|
|
throw new Exception("ID inválido.");
|
|
|
|
return dal.Excluir(id);
|
|
}
|
|
#endregion
|
|
|
|
#region SELECT
|
|
public DataTable LocalizarTodos()
|
|
{
|
|
return dal.LocalizarTodos();
|
|
}
|
|
|
|
public ModeloBancos Carregar(int id)
|
|
{
|
|
if (id <= 0)
|
|
return null;
|
|
|
|
return dal.CarregarModeloBanco(id);
|
|
}
|
|
|
|
public string ObterNomePorNumero(string numero)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(numero))
|
|
return null;
|
|
|
|
return dal.ObterNomePorNumero(numero.Trim());
|
|
}
|
|
#endregion
|
|
|
|
#region VALIDAÇÃO
|
|
private string Validar(ModeloBancos banco)
|
|
{
|
|
if (banco == null)
|
|
return "Objeto banco inválido.";
|
|
|
|
if (string.IsNullOrWhiteSpace(banco.NUMERO))
|
|
return "Número do banco é obrigatório.";
|
|
|
|
if (!SomenteNumeros(banco.NUMERO))
|
|
return "Número do banco deve conter apenas números.";
|
|
|
|
if (string.IsNullOrWhiteSpace(banco.NOME))
|
|
return "Nome do banco é obrigatório.";
|
|
|
|
if (banco.NOME.Length < 3)
|
|
return "Nome do banco muito curto.";
|
|
|
|
return null;
|
|
}
|
|
#endregion
|
|
|
|
#region AUX
|
|
private bool SomenteNumeros(string texto)
|
|
{
|
|
foreach (char c in texto)
|
|
{
|
|
if (!char.IsDigit(c))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
#endregion
|
|
}
|
|
} |