| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- package main
- import (
- "database/sql"
- "fmt"
- "log"
- "net/http"
- "text/template"
- _ "github.com/go-sql-driver/mysql"
- )
- func ConexionBD() (conexion *sql.DB) {
- Driver := "mysql"
- User := "root"
- Password := ""
- Name := "alumnos"
- conexion, err := sql.Open(Driver, User+":"+Password+"@tcp(127.0.0.1)/"+Name)
- if err != nil {
- panic(err.Error())
- }
- return conexion
- }
- var plantillas = template.Must(template.ParseGlob("plantillas/*"))
- func main() {
- http.HandleFunc("/", Home)
- http.HandleFunc("/form", Form)
- http.HandleFunc("/agregar", Agregar)
- http.HandleFunc("/borrar", Borrar)
- http.HandleFunc("/editar", Editar)
- http.HandleFunc("/actualizar", Actualizar)
- log.Println("running")
- http.ListenAndServe(":8080", nil)
- }
- type Alumnos struct {
- Id int
- Nombre string
- Apellido string
- Economia string
- Pertenecia string
- Discapacidad string
- }
- func Home(w http.ResponseWriter, r *http.Request) {
- conexionestablecida := ConexionBD()
- registro, err := conexionestablecida.Query("SELECT * FROM alumnos")
- if err != nil {
- panic(err.Error())
- }
- alumno := Alumnos{}
- arregloAlumno := []Alumnos{}
- for registro.Next() {
- var id int
- var nombre, apellido, economia, pertenecia, discapacidad string
- err = registro.Scan(&id, &nombre, &apellido, &economia, &pertenecia, &discapacidad)
- if err != nil {
- panic(err.Error())
- }
- alumno.Id = id
- alumno.Nombre = nombre
- alumno.Apellido = apellido
- alumno.Economia = economia
- alumno.Pertenecia = pertenecia
- alumno.Discapacidad = discapacidad
- arregloAlumno = append(arregloAlumno, alumno)
- }
- // fmt.Println(arregloAlumno)
- plantillas.ExecuteTemplate(w, "home", arregloAlumno)
- }
- func Form(w http.ResponseWriter, r *http.Request) {
- //fmt.Fprintf(w, "hola mundo")
- plantillas.ExecuteTemplate(w, "form", nil)
- }
- func Agregar(w http.ResponseWriter, r *http.Request) {
- if r.Method == "POST" {
- nombre := r.FormValue("nombre")
- apellido := r.FormValue("apellido")
- economia := r.FormValue("economia")
- pertenecia := r.FormValue("pertenecia")
- discapacidad := r.FormValue("discapacidad")
- conexionestablecida := ConexionBD()
- AgregarRegistro, err := conexionestablecida.Prepare("INSERT INTO alumnos(nombre,apellido,economia,pertenecia,discapacidad,) VALUES(?,?,?,?,?)")
- if err != nil {
- panic(err.Error())
- }
- AgregarRegistro.Exec(nombre, apellido, economia, pertenecia, discapacidad)
- http.Redirect(w, r, "/", 301)
- }
- }
- func Borrar(w http.ResponseWriter, r *http.Request) {
- idAlumno := r.URL.Query().Get("id")
- //fmt.Println(idAlumno)
- conexionestablecida := ConexionBD()
- AgregarRegistro, err := conexionestablecida.Prepare("DELETE FROM alumnos WHERE id=?")
- if err != nil {
- panic(err.Error())
- }
- AgregarRegistro.Exec(idAlumno)
- http.Redirect(w, r, "/", 301)
- }
- func Editar(w http.ResponseWriter, r *http.Request) {
- idAlumno := r.URL.Query().Get("id")
- fmt.Println(idAlumno)
- conexionestablecida := ConexionBD()
- unregistro, err := conexionestablecida.Query("SELECT * FROM alumnos WHERE id=?", idAlumno)
- alumno := Alumnos{}
- for unregistro.Next() {
- var id int
- var nombre, apellido, economia, pertenecia, discapacidad string
- err = unregistro.Scan(&id, &nombre, &apellido, &economia, &pertenecia, &discapacidad)
- if err != nil {
- panic(err.Error())
- }
- alumno.Id = id
- alumno.Nombre = nombre
- alumno.Apellido = apellido
- alumno.Economia = economia
- alumno.Pertenecia = pertenecia
- alumno.Discapacidad = discapacidad
- }
- fmt.Println(alumno)
- plantillas.ExecuteTemplate(w, "editar", alumno)
- }
- func Actualizar(w http.ResponseWriter, r *http.Request) {
- if r.Method == "POST" {
- id := r.FormValue("id")
- nombre := r.FormValue("nombre")
- apellido := r.FormValue("apellido")
- economia := r.FormValue("economia")
- pertenecia := r.FormValue("pertenecia")
- discapacidad := r.FormValue("discapacidad")
- conexionestablecida := ConexionBD()
- fmt.Println("Valores recibidos:")
- fmt.Printf("id: %s, nombre: %s, apellido: %s, economia: %s, pertenecia: %s, discapacidad: %s\n",
- id, nombre, apellido, economia, pertenecia, discapacidad)
- if id == "" || nombre == "" || apellido == "" || economia == "" || pertenecia == "" || discapacidad == "" {
- http.Error(w, "Todos los campos son obligatorios", http.StatusBadRequest)
- return
- }
- ModificarRegistro, err := conexionestablecida.Prepare("UPDATE alumnos SET nombre=?,apellido=?,economia=?,pertenecia=?,discapacidad=? WHERE id=?")
- if err != nil {
- panic(err.Error())
- }
- fmt.Println(ModificarRegistro)
- ModificarRegistro.Exec(nombre, apellido, economia, pertenecia, discapacidad, id)
- http.Redirect(w, r, "/", 301)
- }
- }
|