ventana personal completamente terminada

This commit is contained in:
carlos
2024-01-16 22:06:23 -06:00
parent 28a2ec0842
commit 038bb92384
27 changed files with 1044 additions and 347 deletions
+155 -14
View File
@@ -76,7 +76,37 @@ namespace compabetov2.database
}
}
public static bool crearLoginDAO(LoginModel login)
public static bool verificarExistenciaLoginDAO(LoginModel loginModel)
{
SQLiteDataReader reader = null;
var db_conexion = new Conexion();
try
{
SQLiteConnection conexion = db_conexion.get_connection();
conexion.Open();
string sql = "SELECT * FROM Login WHERE usuario = @usuario";
SQLiteCommand command = new SQLiteCommand(sql, conexion);
command.Parameters.AddWithValue("@usuario", loginModel.usuario);
reader = command.ExecuteReader();
if(reader.Read())
{
return false;
}
return true;
}
catch (SQLiteException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public static bool crearLoginDAO(LoginModel login, int idempleado)
{
var db_conexion = new Conexion();
try
@@ -91,18 +121,30 @@ namespace compabetov2.database
{
string sqlLogin = "INSERT INTO Login (tipo, usuario, contra) VALUES(@tipo, @usuario, @contra);";
string sqlEmpleado = "UPDATE Empleado SET fk_idLogin = @fklogin WHERE idempleado = @idempleado";
string sqlConsulta = "SELECT idlogin FROM Login ORDER BY idlogin DESC LIMIT 1";
SQLiteCommand commandLogin = new SQLiteCommand(sqlLogin, conexion);
commandLogin.Parameters.AddWithValue("@tipo", login.tipo);
commandLogin.Parameters.AddWithValue("@usuario", login.usuario);
commandLogin.Parameters.AddWithValue("@contra", login.contra);
commandLogin.ExecuteNonQuery();
SQLiteCommand commandConsulta = new SQLiteCommand(sqlConsulta, conexion);
SQLiteDataReader reader = commandConsulta.ExecuteReader();
long fkLogin = -1;
while(reader.Read())
{
fkLogin = reader.GetInt64(0);
}
SQLiteCommand commandEmpleado = new SQLiteCommand(sqlEmpleado, conexion);
commandEmpleado.Parameters.AddWithValue("@fklogin", login.fkEmpleado);
commandEmpleado.Parameters.AddWithValue("@idempleado", login.fkEmpleado);
commandEmpleado.Parameters.AddWithValue("@fklogin", fkLogin);
commandEmpleado.Parameters.AddWithValue("@idempleado", idempleado);
commandLogin.ExecuteNonQuery();
commandEmpleado.ExecuteNonQuery();
transaccion.Commit();
@@ -124,6 +166,83 @@ namespace compabetov2.database
}
}
public static bool eliminarLoginDAO(Empleado empleado)
{
var db_conexion = new Conexion();
try
{
using (SQLiteConnection conexion = db_conexion.get_connection())
{
conexion.Open();
using (var transaccion = conexion.BeginTransaction())
{
try
{
string sql = "UPDATE Empleado SET fk_idLogin = -1 WHERE idempleado = @idempleado";
string sqlLogin = "DELETE FROM Login WHERE idlogin = @idlogin";
SQLiteCommand command = new SQLiteCommand(sql, conexion);
command.Parameters.AddWithValue("@idempleado", empleado.idempleado);
SQLiteCommand commandLogin = new SQLiteCommand(sqlLogin, conexion);
commandLogin.Parameters.AddWithValue("@idlogin", empleado.fkIdLogin);
command.ExecuteNonQuery();
commandLogin.ExecuteNonQuery();
transaccion.Commit();
return true;
}
catch (SQLiteException ex)
{
transaccion.Rollback();
MessageBox.Show(ex.Message);
return false;
}
}
}
}
catch (SQLiteException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public static bool modificarContraDAO(Empleado empleado, string contra)
{
var db_conexion = new Conexion();
try
{
using (SQLiteConnection conexion = db_conexion.get_connection())
{
conexion.Open();
string sql = "UPDATE Login SET contra = @contra WHERE idlogin = @idlogin";
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
{
command.Parameters.AddWithValue("@contra", contra);
command.Parameters.AddWithValue("@idlogin", empleado.fkIdLogin);
command.ExecuteNonQuery();
return true;
}
}
}
catch (SQLiteException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public static bool insertarEmpleadoDAO(Empleado empleado)
{
var db_conexion = new Conexion();
@@ -133,8 +252,8 @@ namespace compabetov2.database
{
conexion.Open();
string sql = "INSERT INTO Empleado (nombre, apellidos, calle, fecha_contratacion, estado, colonia, cp, no_ext, no_int, referencia, fecha_nac, telefono) " +
"VALUES (@nombre, @apellidos, @calle, @fecha_contratacion, @estado, @colonia, @cp, @no_ext, @no_int, @referencia, @fecha_nac, @telefono)";
string sql = "INSERT INTO Empleado (nombre, apellidos, calle, fecha_contratacion, estado, colonia, cp, no_ext, no_int, referencia, fecha_nac, telefono,img,fk_idLogin) " +
"VALUES (@nombre, @apellidos, @calle, @fecha_contratacion, @estado, @colonia, @cp, @no_ext, @no_int, @referencia, @fecha_nac, @telefono, @img, @fk_idLogin)";
@@ -152,6 +271,8 @@ namespace compabetov2.database
command.Parameters.AddWithValue("@referencia", empleado.referencia);
command.Parameters.AddWithValue("@fecha_nac", empleado.fecha_nac);
command.Parameters.AddWithValue("@telefono", empleado.telefono);
command.Parameters.AddWithValue("@img", empleado.img);
command.Parameters.AddWithValue("@fk_idLogin", empleado.fkIdLogin);
int filasAfectadas = command.ExecuteNonQuery();
@@ -234,23 +355,43 @@ namespace compabetov2.database
}
}
public static bool eliminarEmpleadoDAO(int idEmpleado) {
public static bool eliminarEmpleadoDAO(Empleado empleado) {
var db_conexion = new Conexion();
try
{
using (SQLiteConnection conexion = db_conexion.get_connection())
{
conexion.Open();
string sql = "DELETE FROM Empleado WHERE idempleado = @idempleado";
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
using (var transaccion = conexion.BeginTransaction())
{
command.Parameters.AddWithValue("@idempleado", idEmpleado);
int filasAfectadas = command.ExecuteNonQuery();
try
{
string sql = "DELETE FROM Empleado WHERE idempleado = @idempleado";
string sqlLogin = "DELETE FROM Login WHERE idlogin = @idlogin";
SQLiteCommand command = new SQLiteCommand(sql, conexion);
command.Parameters.AddWithValue("@idempleado", empleado.idempleado);
SQLiteCommand commandLogin = new SQLiteCommand(sqlLogin, conexion);
commandLogin.Parameters.AddWithValue("@idlogin", empleado.fkIdLogin);
command.ExecuteNonQuery();
commandLogin.ExecuteNonQuery();
transaccion.Commit();
return true;
}
catch (SQLiteException ex)
{
transaccion.Rollback();
MessageBox.Show(ex.Message);
return false;
}
if (filasAfectadas > 0) { return true; }
else { return false; }
}
}
}
+4 -2
View File
@@ -21,9 +21,10 @@ namespace compabetov2.database.modelos
public string referencia;
public string fecha_nac;
public string telefono;
public int? fkIdLogin;
public string img;
public int fkIdLogin;
public Empleado(int idempeado = -1, string nombre ="", string apellidos = "", string calle = "", string fecha_contratacion = "", string estado = "", string colonia = "", string cp = "", string no_ext = "", string no_int = "", string referencia = "", string fecha_nac = "", string telefono ="",int? fkIdLogin = null)
public Empleado(int idempeado = -1, string nombre ="", string apellidos = "", string calle = "", string fecha_contratacion = "", string estado = "", string colonia = "", string cp = "", string no_ext = "", string no_int = "", string referencia = "", string fecha_nac = "", string telefono ="",string img = "",int fkIdLogin = -1)
{
this.idempleado = idempeado;
this.nombre = nombre;
@@ -38,6 +39,7 @@ namespace compabetov2.database.modelos
this.referencia = referencia;
this.fecha_nac=fecha_nac;
this.telefono = telefono;
this.img = img;
this.fkIdLogin = fkIdLogin;
}
+24 -9
View File
@@ -17,9 +17,9 @@ namespace compabetov2.database
return DAO.iniciarSesionDAO(usuario.nombreUsuario, usuario.contra);
}
public static bool crearLogin(LoginModel loginModel)
public static bool crearLogin(LoginModel loginModel, int idEmpleado)
{
return DAO.crearLoginDAO(loginModel);
return DAO.crearLoginDAO(loginModel, idEmpleado);
}
public static List<Producto> conseguirProductos()
@@ -110,12 +110,11 @@ namespace compabetov2.database
while (reader.Read())
{
int? fkIdLogin = null;
try
{
int fkIdLogin = -1;
try {
long fkIdLoginTemp = reader.GetInt64(13);
fkIdLogin = Convert.ToInt32(fkIdLoginTemp); ;
}catch (Exception ex){}
fkIdLogin = Convert.ToInt32(fkIdLoginTemp);
}catch (Exception ex) { }
long id = reader.GetInt64(0);
Empleado empleado = new Empleado(
Convert.ToInt32(id),
@@ -131,6 +130,7 @@ namespace compabetov2.database
reader["referencia"].ToString(),
reader["fecha_nac"].ToString(),
reader["telefono"].ToString(),
reader["img"].ToString(),
fkIdLogin
) ;
empleados.Add(empleado);
@@ -145,14 +145,29 @@ namespace compabetov2.database
return DAO.aumentarStockDAO(stockNuevo);
}
public static bool eliminarEmpleado(int idEmpleado)
public static bool eliminarEmpleado(Empleado empleado)
{
return DAO.eliminarEmpleadoDAO(idEmpleado);
return DAO.eliminarEmpleadoDAO(empleado);
}
public static bool actualizarEmpleado(Empleado empleado)
{
return DAO.actualizarEmpleadoDAO(empleado);
}
public static bool verificarExistenciaLogin(LoginModel loginModel)
{
return DAO.verificarExistenciaLoginDAO(loginModel);
}
public static bool eliminarLogin(Empleado empleado)
{
return DAO.eliminarLoginDAO(empleado);
}
public static bool modificarContra(Empleado empleado, string contra)
{
return DAO.modificarContraDAO(empleado, contra);
}
}
}