public ActionResult Editar(Libro libro, int[] Id_Generos, int[] Id_Autores)
 {
     if (this.ModelState.IsValid)
     {
         libro.Pertenece.Clear();
         foreach (int id_genero in Id_Generos)
         {
             Pertenece pertenece = new Pertenece();
             pertenece.Id_Genero = id_genero;
             pertenece.Id_Libro  = libro.Id;
             libro.Pertenece.Add(pertenece);
         }
         libro.EscritoPor.Clear();
         foreach (int id_autor in Id_Autores)
         {
             if (!(id_autor == -1))
             {
                 EscritoPor escritoPor = new EscritoPor();
                 escritoPor.Id_Autor = id_autor;
                 escritoPor.Id_Libro = libro.Id;
                 libro.EscritoPor.Add(escritoPor);
             }
         }
         db.Libro.Attach(libro);
         db.Entry(libro).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return(Content("Libro editado satisfactoriamente."));
     }
     return(new HttpStatusCodeResult(505, "Internal server Error"));
 }
        public bool Insert(Pertenece p)
        {
            try
            {
                Conexion.ConnectionString = "server=localhost; database=panaderia; uid=root; pwd=Root123;";
            }
            catch (Exception)
            {
                throw;
            }
            try
            {
                Conexion.Open();
                string insertQuery = "INSERT INTO Pertenece (Id_Venta,Id_Pan, Nombre, Precio, Cantidad)" +
                                     "VALUES (@Id_Venta, @Id_Pan, @Nombre, @Precio,@Cantidad);";

                MySqlCommand SqlCom = new MySqlCommand(insertQuery, Conexion);
                SqlCom.Parameters.AddWithValue("@Id_Venta", p.Id_Venta);
                SqlCom.Parameters.AddWithValue("@Id_Pan", p.Id_Pan);
                SqlCom.Parameters.AddWithValue("@Nombre", p.Nombre);
                SqlCom.Parameters.AddWithValue("@Precio", p.Precio);
                SqlCom.Parameters.AddWithValue("@Cantidad", p.Cantidad);
                SqlCom.ExecuteNonQuery();
                Conexion.Close();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public bool Update(Pertenece p)
        {
            try
            {
                Conexion.ConnectionString = "server=localhost; database=panaderia; uid=root; pwd=Root123;";
            }
            catch (Exception)
            {
                throw;
            }
            try
            {
                Conexion.Open();
                string insertQuery = "UPDATE pertenece SET  Nombre = @Nombre, Precio = @Precio, Cantidad=@Cantidad " +
                                     "WHERE Id_Venta = @Id_Venta and Id_Pan =@Id_Pan ;";

                MySqlCommand SqlCom = new MySqlCommand(insertQuery, Conexion);
                SqlCom.Parameters.AddWithValue("@Id_Venta", p.Id_Venta);
                SqlCom.Parameters.AddWithValue("@Id_Pan", p.Id_Pan);
                SqlCom.Parameters.AddWithValue("@Nombre", p.Nombre);
                SqlCom.Parameters.AddWithValue("@Precio", p.Precio);
                SqlCom.Parameters.AddWithValue("@Cantidad", p.Cantidad);
                SqlCom.ExecuteNonQuery();
                Conexion.Close();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public List <Pertenece> GetAll(int Id_Venta)
        {
            List <Pertenece> lista = new List <Pertenece>();

            try
            {
                Conexion.ConnectionString = "server=localhost; database=panaderia; uid=root; pwd=Root123;";
            }
            catch (Exception)
            {
                throw;
            }
            try
            {
                Conexion.Open();

                DataSet          ds  = new DataSet();
                MySqlDataAdapter da  = new MySqlDataAdapter();
                MySqlCommand     cmd = new MySqlCommand();


                //cnn.Open();
                cmd.CommandText             = "SELECT * FROM pertenece where Id_Venta=" + Id_Venta;
                da.SelectCommand            = cmd;
                da.SelectCommand.Connection = Conexion;
                da.Fill(ds);
                DataSet datos = ds;

                DataTable dt = datos.Tables[0];
                Pertenece p;
                foreach (DataRow r in dt.Rows)
                {
                    p          = new Pertenece();
                    p.Id_Venta = (int)r.ItemArray[0];
                    p.Id_Pan   = (int)r.ItemArray[1];
                    p.Nombre   = (string)r.ItemArray[2];
                    p.Precio   = (double)r.ItemArray[3];
                    p.Cantidad = (int)r.ItemArray[4];
                    lista.Add(p);
                }
                Conexion.Close();
                return(lista);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Exemple #5
0
        private void carrito(int Indice, int cantidad)
        {
            bool      co  = true;
            Pertenece pan = new Pertenece();

            pan.Id_Venta = id;
            pan.Id_Pan   = int.Parse(dtgPanes.Rows[Indice].Cells[0].Value.ToString());
            pan.Nombre   = dtgPanes.Rows[Indice].Cells[1].Value.ToString();
            pan.Precio   = double.Parse(dtgPanes.Rows[Indice].Cells[2].Value.ToString());
            pan.Cantidad = cantidad;
            for (int i = 0; i < lista.Count; i++)
            {
                if (lista.ElementAt(i).Id_Pan == pan.Id_Pan)
                {
                    lista.ElementAt(i).Cantidad += cantidad;
                    co = false;
                    break;
                }
            }
            if (co)
            {
                lista.Add(pan);
            }
        }
        public ActionResult Crear(Libro libro, int[] Id_Generos, int[] Id_Autores)
        {
            if (this.ModelState.IsValid)
            {
                foreach (int id_genero in Id_Generos)
                {
                    Pertenece pertenece = new Pertenece();
                    pertenece.Id_Genero = id_genero;
                    libro.Pertenece.Add(pertenece);
                }

                foreach (int id_autor in Id_Autores)
                {
                    EscritoPor escritoPor = new EscritoPor();
                    escritoPor.Id_Autor = id_autor;
                    libro.EscritoPor.Add(escritoPor);
                }

                db.Libro.Add(libro);
                db.SaveChanges();
                return(Content("Libro creado satisfactoriamente."));
            }
            return(new HttpStatusCodeResult(505, "Internal server Error"));
        }