585 lines
24 KiB
C#
585 lines
24 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SQLite;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Policy;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using compabetov2.database;
|
|
using compabetov2.database.modelos;
|
|
|
|
namespace compabetov2.database
|
|
{
|
|
internal class DAO
|
|
{
|
|
public static bool iniciarSesionDAO(String usuario, String contra)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
|
|
using (SQLiteConnection conexion = db_conexion.get_connection()) {
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT usuario, contra FROM Login WHERE usuario = @param0";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue($"@param{0}", usuario);
|
|
SQLiteDataReader reader = command.ExecuteReader();
|
|
|
|
if (reader.HasRows)
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
if (reader["contra"].Equals(contra))
|
|
{
|
|
conexion.Close();
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Usuario o contraseña incorrectos");
|
|
conexion.Close();
|
|
return false;
|
|
}
|
|
}
|
|
MessageBox.Show("Usuario o contraseña incorrectos");
|
|
conexion.Close();
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Usuario o contraseña incorrectos");
|
|
conexion.Close();
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
catch (SQLiteException ex)
|
|
{
|
|
if (ex.Message.Contains("no such table"))
|
|
{
|
|
crearDB();
|
|
MessageBox.Show("La base de datos fue creada vuelva a intentar");
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public static bool crearLoginDAO(LoginModel login)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using (var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
string sqlLogin = "INSERT INTO Login (tipo, usuario, contra) VALUES(@tipo, @usuario, @contra);";
|
|
string sqlEmpleado = "UPDATE Empleado SET fk_idLogin = @fklogin WHERE idempleado = @idempleado";
|
|
|
|
|
|
SQLiteCommand commandLogin = new SQLiteCommand(sqlLogin, conexion);
|
|
commandLogin.Parameters.AddWithValue("@tipo", login.tipo);
|
|
commandLogin.Parameters.AddWithValue("@usuario", login.usuario);
|
|
commandLogin.Parameters.AddWithValue("@contra", login.contra);
|
|
|
|
SQLiteCommand commandEmpleado = new SQLiteCommand(sqlEmpleado, conexion);
|
|
commandEmpleado.Parameters.AddWithValue("@fklogin", login.fkEmpleado);
|
|
commandEmpleado.Parameters.AddWithValue("@idempleado", login.fkEmpleado);
|
|
|
|
commandLogin.ExecuteNonQuery();
|
|
commandEmpleado.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 insertarEmpleadoDAO(Empleado empleado)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
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)";
|
|
|
|
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", empleado.nombre);
|
|
command.Parameters.AddWithValue("@apellidos", empleado.apellidos);
|
|
command.Parameters.AddWithValue("@calle", empleado.calle);
|
|
command.Parameters.AddWithValue("@fecha_contratacion", empleado.fecha_contratacion);
|
|
command.Parameters.AddWithValue("@estado", empleado.estado);
|
|
command.Parameters.AddWithValue("@colonia", empleado.colonia);
|
|
command.Parameters.AddWithValue("@cp", empleado.cp);
|
|
command.Parameters.AddWithValue("@no_ext", empleado.no_ext);
|
|
command.Parameters.AddWithValue("@no_int", empleado.no_int);
|
|
command.Parameters.AddWithValue("@referencia", empleado.referencia);
|
|
command.Parameters.AddWithValue("@fecha_nac", empleado.fecha_nac);
|
|
command.Parameters.AddWithValue("@telefono", empleado.telefono);
|
|
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirEmpleadosDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM Empleado";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool actualizarEmpleadoDAO(Empleado empleado)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "UPDATE Empleado SET nombre = @nombre, apellidos = @apellidos, calle = @calle, fecha_contratacion = @fecha_contratacion, " +
|
|
"estado = @estado, colonia = @colonia, cp = @cp, no_ext = @no_ext, no_int = @no_int, referencia = @referencia, " +
|
|
"fecha_nac = @fecha_nac, telefono = @telefono WHERE idempleado = @idempleado;";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", empleado.nombre);
|
|
command.Parameters.AddWithValue("@apellidos", empleado.apellidos);
|
|
command.Parameters.AddWithValue("@calle", empleado.calle);
|
|
command.Parameters.AddWithValue("@fecha_contratacion", empleado.fecha_contratacion);
|
|
command.Parameters.AddWithValue("@estado", empleado.estado);
|
|
command.Parameters.AddWithValue("@colonia", empleado.colonia);
|
|
command.Parameters.AddWithValue("@cp", empleado.cp);
|
|
command.Parameters.AddWithValue("@no_ext", empleado.no_ext);
|
|
command.Parameters.AddWithValue("@no_int", empleado.no_int);
|
|
command.Parameters.AddWithValue("@referencia", empleado.referencia);
|
|
command.Parameters.AddWithValue("@fecha_nac", empleado.fecha_nac);
|
|
command.Parameters.AddWithValue("@telefono", empleado.telefono);
|
|
command.Parameters.AddWithValue("@idempleado", empleado.idempleado);
|
|
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool eliminarEmpleadoDAO(int idEmpleado) {
|
|
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))
|
|
{
|
|
command.Parameters.AddWithValue("@idempleado", idEmpleado);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirProductosDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM Producto INNER JOIN Categoria ON Producto.fk_idcategoria = Categoria.idcategoria";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
}
|
|
catch (SQLiteException ex) {
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
public static bool insertarProductoDAO(Producto producto)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "INSERT INTO Producto (nombre, descripcion, precio,img, stock, fk_idcategoria) "+
|
|
"VALUES (@nombre, @descripcion, @precio,@img , @stock, @idcategoria)";
|
|
|
|
if (!producto.img.Equals(""))
|
|
{
|
|
producto.img = guardarImg(producto.img);
|
|
}
|
|
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", producto.nombre);
|
|
command.Parameters.AddWithValue("@descripcion", producto.descripcion);
|
|
command.Parameters.AddWithValue("@precio", producto.precion);
|
|
command.Parameters.AddWithValue("@img", producto.img);
|
|
command.Parameters.AddWithValue("@stock", producto.stock);
|
|
command.Parameters.AddWithValue("@idcategoria", producto.categoria.idCategoria);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if(filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool eliminarProductoDAO(Producto producto)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "DELETE FROM Producto WHERE idproducto = @idproducto";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@idproducto", producto.idProducto);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool aumentarStockDAO(List<Producto> stockNuevo)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
using(var transaccion = conexion.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
foreach (var item in stockNuevo)
|
|
{
|
|
string sql = "UPDATE Producto SET stock = @stock WHERE idproducto = @idproducto;";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
command.Parameters.AddWithValue("@idproducto", item.idProducto);
|
|
command.Parameters.AddWithValue("@stock", item.stock);
|
|
command.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 insertarCategoriaDAO(Categoria categoria)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "INSERT INTO Categoria (nombre, descripcion) " +
|
|
"VALUES (@nombre, @descripcion)";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", categoria.nombre);
|
|
command.Parameters.AddWithValue("@descripcion", categoria.descripcion);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool insertarClienteDAO(Cliente cliente)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "INSERT INTO Cliente (nombres, apellidos, calle, colonia, cp, no_ext, no_int, referencia, telefono, telefono_extra) " +
|
|
"VALUES (@nombres, @apellidos, @calle, @colonia, @cp, @no_ext, @no_int, @referencia, @telefono, @telefono_extra)";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@nombre", cliente.nombres);
|
|
command.Parameters.AddWithValue("@apellidios", cliente.apellidos);
|
|
command.Parameters.AddWithValue("@calle", cliente.calle);
|
|
command.Parameters.AddWithValue("@colonia", cliente.colonia);
|
|
command.Parameters.AddWithValue("@cp", cliente.cp);
|
|
command.Parameters.AddWithValue("@no_ext", cliente.no_ext);
|
|
command.Parameters.AddWithValue("@no_int", cliente.no_int);
|
|
command.Parameters.AddWithValue("@referencia", cliente.referencia);
|
|
command.Parameters.AddWithValue("@telefono", cliente.telefono);
|
|
command.Parameters.AddWithValue("@telefono_extra", cliente.telefono_extra);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static bool eliminarClienteDAO(Cliente cliente)
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "DELETE FROM Cliente WHERE idCliente = @idCliente";
|
|
|
|
using (SQLiteCommand command = new SQLiteCommand(sql, conexion))
|
|
{
|
|
command.Parameters.AddWithValue("@idCliente", cliente.idCliente);
|
|
int filasAfectadas = command.ExecuteNonQuery();
|
|
|
|
if (filasAfectadas > 0) { return true; }
|
|
else { return false; }
|
|
}
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static SQLiteDataReader conseguirClientesDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (SQLiteConnection conexion = db_conexion.get_connection())
|
|
{
|
|
conexion.Open();
|
|
|
|
string sql = "SELECT * FROM Cliente";
|
|
SQLiteCommand command = new SQLiteCommand(sql, conexion);
|
|
reader = command.ExecuteReader();
|
|
|
|
return reader;
|
|
}
|
|
}
|
|
catch (SQLiteException ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
private static string guardarImg(String rutaImagen)
|
|
{
|
|
//consigue la carpeta del proyectro
|
|
string carpetaImagenes = Path.Combine(Application.StartupPath, "produtos_img");
|
|
|
|
//crear el directorio sino existe
|
|
if (!Directory.Exists(carpetaImagenes))
|
|
{
|
|
Directory.CreateDirectory(carpetaImagenes);
|
|
}
|
|
|
|
string nombreArchivo = Path.GetFileName(rutaImagen);
|
|
string rutaDestino = Path.Combine(carpetaImagenes, nombreArchivo);
|
|
File.Copy(rutaImagen, rutaDestino, true);
|
|
|
|
return rutaDestino;
|
|
}
|
|
|
|
private static void crearDB()
|
|
{
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
SQLiteConnection conexion = db_conexion.get_connection();
|
|
conexion.Open();
|
|
string[] consultas = new string[]
|
|
{
|
|
"DROP TABLE IF EXISTS Empleado",
|
|
"DROP TABLE IF EXISTS Login",
|
|
"DROP TABLE IF EXISTS Producto",
|
|
"DROP TABLE IF EXISTS Categoria",
|
|
"DROP TABLE IF EXISTS Cliente",
|
|
|
|
"CREATE TABLE Login (idlogin INTEGER PRIMARY KEY AUTOINCREMENT, tipo TEXT NOT NULL, usuario TEXT NOT NULL, "+
|
|
"contra TEXT NOT NULL)",
|
|
|
|
"CREATE TABLE Empleado (idempleado INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, "+
|
|
"apellidos TEXT NOT NULL, calle TEXT NOT NULL, fecha_contratacion TEXT NOT NULL,estado TEXT NOT NULL, colonia TEXT NOT NULL, cp TEXT NOT NULL, no_ext TEXT NOT NULL," +
|
|
"no_int TEXT NOT NULL, referencia TEXT NOT NULL, fecha_nac TEXT NOT NULL, telefono TEXT NOT NULL, fk_idLogin INTEGER," +
|
|
"FOREIGN KEY (fk_idLogin) REFERENCES Login(idlogin))",
|
|
|
|
"CREATE TABLE Categoria (idcategoria INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, descripcion TEXT NOT NULL)",
|
|
|
|
"CREATE TABLE Producto (idproducto INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, descripcion TEXT NOT NULL, "+
|
|
"precio DECIMAL(10, 2) NOT NULL, img TEXT NOT NULL, stock INTEGER NOT NULL, " +
|
|
"fk_idcategoria INTEGER NOT NULL, FOREIGN KEY (fk_idcategoria) REFERENCES Categoria (idcategoria))",
|
|
|
|
"CREATE TABLE Cliente (idcliente INTEGER PRIMARY KEY AUTOINCREMENT, nombres TEXT NOT NULL, apellidos TEXT NOT NULL, " +
|
|
"calle TEXT NOT NULL, colonia TEXT NOT NULL, cp TEXT NOT NULL, no_ext TEXT NOT NULL, no_int TEXT NOT NULL, referencia TEXT NOT NULL, " +
|
|
"telefono TEXT NOT NULL, telefono_extra TEXT NOT NULL)",
|
|
|
|
"INSERT INTO Login ( tipo, usuario, contra) VALUES('admin', 'admin', '12345');",
|
|
"INSERT INTO Empleado ( nombre, apellidos, calle, fecha_contratacion,estado, colonia, cp, no_ext,no_int, referencia, fecha_nac, telefono,fk_idLogin) VALUES('jefe', '', '', '', '', '', '', '', '', '', '', '',1);",
|
|
|
|
};
|
|
|
|
foreach (string consulta in consultas)
|
|
{
|
|
using (var comando = new SQLiteCommand(consulta, conexion))
|
|
{
|
|
comando.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
} |