main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "text/template"
  8. _ "github.com/go-sql-driver/mysql"
  9. )
  10. func ConexionBD() (conexion *sql.DB) {
  11. Driver := "mysql"
  12. User := "root"
  13. Password := ""
  14. Name := "alumnos"
  15. conexion, err := sql.Open(Driver, User+":"+Password+"@tcp(127.0.0.1)/"+Name)
  16. if err != nil {
  17. panic(err.Error())
  18. }
  19. return conexion
  20. }
  21. var plantillas = template.Must(template.ParseGlob("plantillas/*"))
  22. func main() {
  23. http.HandleFunc("/", Home)
  24. http.HandleFunc("/form", Form)
  25. http.HandleFunc("/agregar", Agregar)
  26. http.HandleFunc("/borrar", Borrar)
  27. http.HandleFunc("/editar", Editar)
  28. http.HandleFunc("/actualizar", Actualizar)
  29. log.Println("running")
  30. http.ListenAndServe(":8080", nil)
  31. }
  32. type Alumnos struct {
  33. Id int
  34. Nombre string
  35. Apellido string
  36. Economia string
  37. Pertenecia string
  38. Discapacidad string
  39. }
  40. func Home(w http.ResponseWriter, r *http.Request) {
  41. conexionestablecida := ConexionBD()
  42. registro, err := conexionestablecida.Query("SELECT * FROM alumnos")
  43. if err != nil {
  44. panic(err.Error())
  45. }
  46. alumno := Alumnos{}
  47. arregloAlumno := []Alumnos{}
  48. for registro.Next() {
  49. var id int
  50. var nombre, apellido, economia, pertenecia, discapacidad string
  51. err = registro.Scan(&id, &nombre, &apellido, &economia, &pertenecia, &discapacidad)
  52. if err != nil {
  53. panic(err.Error())
  54. }
  55. alumno.Id = id
  56. alumno.Nombre = nombre
  57. alumno.Apellido = apellido
  58. alumno.Economia = economia
  59. alumno.Pertenecia = pertenecia
  60. alumno.Discapacidad = discapacidad
  61. arregloAlumno = append(arregloAlumno, alumno)
  62. }
  63. // fmt.Println(arregloAlumno)
  64. plantillas.ExecuteTemplate(w, "home", arregloAlumno)
  65. }
  66. func Form(w http.ResponseWriter, r *http.Request) {
  67. //fmt.Fprintf(w, "hola mundo")
  68. plantillas.ExecuteTemplate(w, "form", nil)
  69. }
  70. func Agregar(w http.ResponseWriter, r *http.Request) {
  71. if r.Method == "POST" {
  72. nombre := r.FormValue("nombre")
  73. apellido := r.FormValue("apellido")
  74. economia := r.FormValue("economia")
  75. pertenecia := r.FormValue("pertenecia")
  76. discapacidad := r.FormValue("discapacidad")
  77. conexionestablecida := ConexionBD()
  78. AgregarRegistro, err := conexionestablecida.Prepare("INSERT INTO alumnos(nombre,apellido,economia,pertenecia,discapacidad,) VALUES(?,?,?,?,?)")
  79. if err != nil {
  80. panic(err.Error())
  81. }
  82. AgregarRegistro.Exec(nombre, apellido, economia, pertenecia, discapacidad)
  83. http.Redirect(w, r, "/", 301)
  84. }
  85. }
  86. func Borrar(w http.ResponseWriter, r *http.Request) {
  87. idAlumno := r.URL.Query().Get("id")
  88. //fmt.Println(idAlumno)
  89. conexionestablecida := ConexionBD()
  90. AgregarRegistro, err := conexionestablecida.Prepare("DELETE FROM alumnos WHERE id=?")
  91. if err != nil {
  92. panic(err.Error())
  93. }
  94. AgregarRegistro.Exec(idAlumno)
  95. http.Redirect(w, r, "/", 301)
  96. }
  97. func Editar(w http.ResponseWriter, r *http.Request) {
  98. idAlumno := r.URL.Query().Get("id")
  99. fmt.Println(idAlumno)
  100. conexionestablecida := ConexionBD()
  101. unregistro, err := conexionestablecida.Query("SELECT * FROM alumnos WHERE id=?", idAlumno)
  102. alumno := Alumnos{}
  103. for unregistro.Next() {
  104. var id int
  105. var nombre, apellido, economia, pertenecia, discapacidad string
  106. err = unregistro.Scan(&id, &nombre, &apellido, &economia, &pertenecia, &discapacidad)
  107. if err != nil {
  108. panic(err.Error())
  109. }
  110. alumno.Id = id
  111. alumno.Nombre = nombre
  112. alumno.Apellido = apellido
  113. alumno.Economia = economia
  114. alumno.Pertenecia = pertenecia
  115. alumno.Discapacidad = discapacidad
  116. }
  117. fmt.Println(alumno)
  118. plantillas.ExecuteTemplate(w, "editar", alumno)
  119. }
  120. func Actualizar(w http.ResponseWriter, r *http.Request) {
  121. if r.Method == "POST" {
  122. id := r.FormValue("id")
  123. nombre := r.FormValue("nombre")
  124. apellido := r.FormValue("apellido")
  125. economia := r.FormValue("economia")
  126. pertenecia := r.FormValue("pertenecia")
  127. discapacidad := r.FormValue("discapacidad")
  128. conexionestablecida := ConexionBD()
  129. fmt.Println("Valores recibidos:")
  130. fmt.Printf("id: %s, nombre: %s, apellido: %s, economia: %s, pertenecia: %s, discapacidad: %s\n",
  131. id, nombre, apellido, economia, pertenecia, discapacidad)
  132. if id == "" || nombre == "" || apellido == "" || economia == "" || pertenecia == "" || discapacidad == "" {
  133. http.Error(w, "Todos los campos son obligatorios", http.StatusBadRequest)
  134. return
  135. }
  136. ModificarRegistro, err := conexionestablecida.Prepare("UPDATE alumnos SET nombre=?,apellido=?,economia=?,pertenecia=?,discapacidad=? WHERE id=?")
  137. if err != nil {
  138. panic(err.Error())
  139. }
  140. fmt.Println(ModificarRegistro)
  141. ModificarRegistro.Exec(nombre, apellido, economia, pertenecia, discapacidad, id)
  142. http.Redirect(w, r, "/", 301)
  143. }
  144. }