235 lines
9.1 KiB
C#
235 lines
9.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SQLite;
|
|
using System.IO;
|
|
using System.Linq;
|
|
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 SQLiteDataReader conseguirProductosDAO()
|
|
{
|
|
SQLiteDataReader reader = null;
|
|
var db_conexion = new Conexion();
|
|
try
|
|
{
|
|
using (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, idcategoria) "+
|
|
"VALUES (@nombre, @descripcion, @precio, @stock, @img, @idcategoria)";
|
|
|
|
if (!producto.img.Equals(""))
|
|
{
|
|
string rutaDestino = guardarImg(producto.img);
|
|
producto.img = rutaDestino;
|
|
}
|
|
|
|
|
|
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("@stock", producto.stock);
|
|
command.Parameters.AddWithValue("@img", producto.img);
|
|
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 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;
|
|
}
|
|
}
|
|
|
|
private static string guardarImg(String rutaImagen)
|
|
{
|
|
//consigue la carpeta del proyectro
|
|
string carpetaImagenes = Path.Combine(Aplication.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 Login",
|
|
"DROP TABLE IF EXISTS Empleado",
|
|
"DROP TABLE IF EXISTS Producto",
|
|
"DROP TABLE IF EXISTS Categoria",
|
|
|
|
"CREATE TABLE Empleado (idempleado INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT NOT NULL, "+
|
|
"apellido TEXT NOT NULL, calle TEXT NOT NULL, fecha_contratacion 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, activo INTEGER NOT NULL)",
|
|
|
|
"CREATE TABLE Login (idlogin INTEGER PRIMARY KEY AUTOINCREMENT, tipo TEXT NOT NULL, usuario TEXT NOT NULL, "+
|
|
"contra TEXT NOT NULL, fk_idempleado INTEGER NOT NULL, FOREIGN KEY (fk_idempleado) REFERENCES Empleado (idempleado))",
|
|
|
|
"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, stock INTEGER NOT NULL, img TEXT NOT NULL, " +
|
|
"fk_idcategoria INTEGER NOT NULL, FOREIGN KEY (fk_idcategoria) REFERENCES Categoria (idcategoria))",
|
|
|
|
"INSERT INTO Empleado ( nombre, apellido, calle, fecha_contratacion, colonia, cp, no_ext,no_int, referencia, fecha_nac, telefono, activo) VALUES('jefe', '', '', '', '', '', '', '', '', '', '', 0);",
|
|
"INSERT INTO Login ( tipo, usuario, contra, fk_idempleado) VALUES('admin', 'admin', '12345', 1);"
|
|
};
|
|
|
|
foreach (string consulta in consultas)
|
|
{
|
|
using (var comando = new SQLiteCommand(consulta, conexion))
|
|
{
|
|
comando.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
} |