瀏覽代碼

agregarProyectoRancio

Angelo-T01 1 年之前
當前提交
412f8ee6a3
共有 9 個文件被更改,包括 439 次插入0 次删除
  1. 15 0
      Dockerfile
  2. 14 0
      go.mod
  3. 16 0
      go.sum
  4. 176 0
      main.go
  5. 75 0
      plantillas/editar.html
  6. 14 0
      plantillas/footer.html
  7. 68 0
      plantillas/form.html
  8. 15 0
      plantillas/header.html
  9. 46 0
      plantillas/home.html

+ 15 - 0
Dockerfile

@@ -0,0 +1,15 @@
+FROM golang:1.23.1 AS builder
+RUN apt-get update
+ENV GO111MODULE=on \
+    CGO_ENABLED=0 \
+    GOOS=linux \
+    GOARCH=amd64
+WORKDIR /go/src/GOCRUD
+COPY go.mod .
+RUN go mod download
+COPY . .
+RUN go build main.go
+
+FROM scratch
+COPY --from=builder /go/src .
+ENTRYPOINT  ["./main"]

+ 14 - 0
go.mod

@@ -0,0 +1,14 @@
+module GOCRUD
+
+go 1.23.1
+
+require (
+	filippo.io/edwards25519 v1.1.0 // indirect
+	github.com/go-sql-driver/mysql v1.8.1 // indirect
+	github.com/jinzhu/inflection v1.0.0 // indirect
+	github.com/jinzhu/now v1.1.5 // indirect
+	github.com/mattn/go-sqlite3 v1.14.24 // indirect
+	golang.org/x/text v0.19.0 // indirect
+	gorm.io/driver/sqlite v1.5.6 // indirect
+	gorm.io/gorm v1.25.12 // indirect
+)

+ 16 - 0
go.sum

@@ -0,0 +1,16 @@
+filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
+filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
+github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
+github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
+github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
+github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
+github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
+github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
+github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
+github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
+golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
+golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
+gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
+gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
+gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
+gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=

+ 176 - 0
main.go

@@ -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)
+	}
+
+}

+ 75 - 0
plantillas/editar.html

@@ -0,0 +1,75 @@
+{{define "editar"}}
+{{template "header"}}
+
+<div class="section is-medium">
+
+        <h2 class="title is-3">Editar alumno</h2>
+    <form method="POST" action="/actualizar">
+        
+        <div class="field">
+            <div class="control">
+            <input class="input" type="hidden" name="id" id="id" value="{{.Id}}" >
+            </div>
+        </div>
+        
+        <div class="field">
+            <label class="label">Name</label>
+            <div class="control">
+            <input class="input" type="text" name="nombre" id="nombre" value="{{.Nombre}}">
+            </div>
+        </div>
+        <div class="field">
+            <label for="" class="label">Apellidos</label>
+            <div class="control">
+            <input class="input" type="text"  name="apellido" id="apellido"  value="{{.Apellido}}">
+            </div>
+        </div>
+    
+        <div class="field">
+            <label for="" class="label">Economia</label>
+            <div class="select">
+            <select name="economia" id="economia">
+            <option value="Estable" {{if eq .Economia "Estable"}}selected{{end}} >Estable</option>
+            <option value="Bajos recursos" {{if eq .Economia "Bajos recursos"}}selected{{end}}>Bajos recursos</option>
+            <option value="Pudiente" {{if eq .Economia "Pudiente"}}selected{{end}}>Pudiente</option>
+            </select>
+        </div>
+        </div>
+    <div class="field">
+        <label for="" class="label"  value=".Discapacidad" >Discapacidad</label>
+           <div class="select">
+        <select name="discapacidad" id="discapacidad">
+          <option value="neurodivergente" {{if eq .Discapacidad "Neurodivergente"}}selected{{end}}>Neurodivergente</option>
+          <option value="Ninguna" {{if eq .Discapacidad "Ninguna"}}selected{{end}}>Ninguna</option>
+        </select>
+      </div>
+    </div>
+    <div class="field">
+        <label for="" class="label">Pertenecia cultural</label>
+           <div class="select">
+        <select name="pertenecia" id="pertenecia"  value=".Pertenecia" >
+          <option value="afrodescendiente" {{if eq .Pertenecia "Afrodescendiente"}}selected{{end}}>Afrodescendiente</option>
+          <option value="Indigena" {{if eq .Pertenecia "Indigena"}}selected{{end}}>Indigena</option>
+          <option value="Ninguna" {{if eq .Pertenecia "Ninguna"}}selected{{end}}>Ninguna</option>
+        </select>
+      </div>
+    </div>
+    
+      <div class="field is-grouped">
+        <div class="control">
+          <button class="button is-link" class="Submit" >Actualizar</button>
+        </div>
+        <div class="control">
+          <a class="button is-link is-light" href="/" >Cancel</a>
+        </div>
+      </div>
+</form>
+    
+</div>
+
+
+
+
+{{template "footer"}}
+
+{{end}}

+ 14 - 0
plantillas/footer.html

@@ -0,0 +1,14 @@
+{{define "footer"}}
+
+</body>
+<footer class="footer" >
+    <div class="content has-text-centered">
+      <p>
+        <strong>
+        <a> WSS(wonderful software studio.INC.)</a>
+        </strong>
+      </p>
+    </div>
+  </footer>
+</html>
+{{end}}

+ 68 - 0
plantillas/form.html

@@ -0,0 +1,68 @@
+{{define "form"}}
+{{template "header"}}
+
+<div class="section is-medium">
+
+    <h2 class="title is-3">Agregar nuevo alumno</h2>
+<form method="POST" action="/agregar">
+    <div class="field">
+        <label class="label">Name</label>
+        <div class="control">
+          <input class="input" type="text" name="nombre" id="nombre" placeholder="Nombre">
+        </div>
+    </div>
+    <div class="field">
+        <label for="" class="label">Apellido</label>
+        <div class="control">
+          <input class="input" type="text"  name="apellido" id="apellido" placeholder="Apellidos">
+        </div>
+    </div>
+  
+    <div class="field">
+        <label for="" class="label">Economia</label>
+           <div class="select">
+        <select name="economia" id="economia">
+          <option value="Estable" >Estable</option>
+          <option value="Bajos recursos" >Bajos recursos</option>
+          <option value="Pudiente">Pudiente</option>
+        </select>
+      </div>
+    </div>
+    <div class="field">
+        <label for="" class="label">Discapacidad</label>
+           <div class="select">
+        <select name="discapacidad" id="discapacidad">
+          <option value="neurodivergente" >Neurodivergente</option>
+          <option value="Ninguna">Ninguna</option>
+        </select>
+      </div>
+    </div>
+    <div class="field">
+        <label for="" class="label">Pertenecia cultural</label>
+           <div class="select">
+        <select name="pertenecia" id="pertenecia">
+          <option value="afrodescendiente">Afrodescendiente</option>
+          <option value="Indigena">Indigena</option>
+          <option value="Ninguna">Ninguna</option>
+        </select>
+      </div>
+    </div>
+    
+      <div class="field is-grouped">
+        <div class="control">
+          <button class="button is-link" class="Submit" >Submit</button>
+        </div>
+        <div class="control">
+          <button class="button is-link is-light">Cancel</button>
+        </div>
+      </div>
+</form>
+    
+</div>
+
+<body>
+
+
+{{template "footer"}}
+
+{{end}}

文件差異過大導致無法顯示
+ 15 - 0
plantillas/header.html


+ 46 - 0
plantillas/home.html

@@ -0,0 +1,46 @@
+{{ define "home"}}
+{{template "header"}}
+
+
+
+<div class="section">
+    
+    
+
+    <div class="container">
+        <a class="button is-primary" href="/form"><strong>Agregar</strong></a>
+        <table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
+            <thead>
+                <tr class="is-link">
+                <th>ID</th>
+                <td>Nombre</td>
+                <td>Apellidos</td>
+                <td>Economia</td>
+                <td>Cultura</td>
+                <td>Discapacidad</td>
+                <td>Acciones</td>
+                </tr>
+            </thead>
+            <tbody>
+                {{range.}}
+                <tr>
+                <th>{{.Id}}</th>
+                <td>{{.Nombre}}</td>
+                <td>{{.Apellido}}</td>
+                <td>{{.Economia}}</td>
+                <td>{{.Pertenecia}}</td>
+                <td>{{.Discapacidad}}</td>
+                <td> 
+                    
+                    <a class="button is-warning" href="/editar?id={{.Id}}">Editar</a>
+                    
+                   <a class="button is-danger" href="/borrar?id={{.Id}}">Borrar</a></td>
+                </tr>
+                {{end}}
+            </tbody>
+        </table>
+    </div>
+</div>
+
+{{ template "footer"}}
+{{end}}

部分文件因文件數量過多而無法顯示