public static void Delete(int?id) { using (Entities_Bio db = new Entities_Bio()) { using (var transaction = db.Database.BeginTransaction()) { try { Usuario usuario = db.Usuarios.Find(id); var articulos = usuario.Articuloes.Where(t => t.idUsuario == id); Venta venta = db.Ventas.Find(usuario.idUsuario); var ventas = usuario.Ventas.Where(t => t.idUsuario == id); if (articulos != null) { db.Articuloes.RemoveRange(articulos); } if (venta != null) { db.Ventas.RemoveRange(ventas); } db.Entry(usuario).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
public static string Delete(SegurityViewModel modelo, int id) { if (!UsuarioBLL.Verify(modelo.token))//No autorizado { return "No autorizado"; } using (Entities_Bio db = new Entities_Bio()) { using (var transaction = db.Database.BeginTransaction()) { try { Articulo articulo = db.Articuloes.Find(id); if (articulo == null) { return "Articulo no existe"; } db.Entry(articulo).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); transaction.Commit(); return "Eliminación correcta"; } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
}//end public static bool LogOut(int?id) { //se crea una instancia o un iniverso dentro de otro universo //esta instancia se termina aqui using (Entities_Bio db = new Entities_Bio()) { try { using (var transaction = db.Database.BeginTransaction()) { Usuario userLogin = db.Usuarios.Find(id); userLogin.token_temp = null; db.Usuarios.Attach(userLogin); db.Entry(userLogin).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); transaction.Commit(); return(true); } } catch (Exception ex) { throw; } } }//end
public static SegurityViewModel Login(string email, string password) { //se crea una instancia o un iniverso dentro de otro universo //esta instancia se termina aqui using (Entities_Bio db = new Entities_Bio()) { SegurityViewModel token = new SegurityViewModel(); try { var lst = db.Usuarios.Where(d => d.email == email && d.contrasena == password); //var lst = db.Usuarios.Where(d => d.email == email && d.contrasena == email && d.estado == "a"); if (lst.Count() > 0) { using (var transaction = db.Database.BeginTransaction()) { Usuario userLogin = lst.First(); if (userLogin.token_temp == null) { userLogin.token_temp = Guid.NewGuid().ToString(); } db.Usuarios.Attach(userLogin); db.Entry(userLogin).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); transaction.Commit(); token.token = userLogin.token_temp; token.id_logueado = userLogin.idUsuario; token.nombre = userLogin.nombres + " " + userLogin.apellidos; //token.pathIMG = userLogin.imgPath; return(token); } } else { return(token); } } catch (Exception ex) { throw; } } }//end
//BLL Bussiness Logic Layer //Capa de Logica del Negocio public static void Create(Articulo a) { using (Entities_Bio db = new Entities_Bio()) { using (var transaction = db.Database.BeginTransaction()) { try { db.Articuloes.Add(a); db.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
public static void Update(Usuario Usuario) { using (Entities_Bio db = new Entities_Bio()) { using (var transaction = db.Database.BeginTransaction()) { try { db.Usuarios.Attach(Usuario); db.Entry(Usuario).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
public static void Delete(int?id) { using (Entities_Bio db = new Entities_Bio()) { using (var transaction = db.Database.BeginTransaction()) { try { Usuario alumno = db.Usuarios.Find(id); db.Entry(alumno).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
//BLL Bussiness Logic Layer //Capa de Logica del Negocio public static void Create(Venta a) { using (Entities_Bio db = new Entities_Bio()) { using (var transaction = db.Database.BeginTransaction()) { try { //DateTime thisDay = DateTime.Today; a.fechaVenta = DateTime.Today; db.Ventas.Add(a); db.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }
public static bool Update(Articulo articulo) { using (Entities_Bio db = new Entities_Bio()) { using (var transaction = db.Database.BeginTransaction()) { try { db.Articuloes.Attach(articulo); db.Entry(articulo).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); transaction.Commit(); return true; } catch (Exception ex) { transaction.Rollback(); return false; //throw ex; } } } }
public static string Add(ArticuloViewModel modelo) { using (Entities_Bio db = new Entities_Bio()) { if (!UsuarioBLL.Verify(modelo.token))//No autorizado { return "No autorizado"; } using (var transaction = db.Database.BeginTransaction()) { try { Articulo a = new Articulo(); a.nombre = modelo.nombre; a.precio = modelo.precio; a.categoria = modelo.categoria; a.detalle = modelo.detalle; a.estado = modelo.estado; a.idUsuario = modelo.idUsuario; a.fecha = modelo.fecha_ingreso; a.imagen = modelo.imagen; db.Articuloes.Add(a); db.SaveChanges(); transaction.Commit(); return "Articulo creado exitosamente"; } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }//end Create