|
@@ -0,0 +1,176 @@
|
|
|
|
|
+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)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|