edit button works
This commit is contained in:
parent
6945e0822e
commit
54970ae013
4 changed files with 83 additions and 0 deletions
|
|
@ -47,6 +47,8 @@ func main() {
|
||||||
apiGroup.GET("/:id", apiHandler.GetNombreByID)
|
apiGroup.GET("/:id", apiHandler.GetNombreByID)
|
||||||
apiGroup.PUT("/:id", apiHandler.UpdateNombre)
|
apiGroup.PUT("/:id", apiHandler.UpdateNombre)
|
||||||
apiGroup.DELETE("/:id", apiHandler.DeleteNombre)
|
apiGroup.DELETE("/:id", apiHandler.DeleteNombre)
|
||||||
|
apiGroup.GET("/html/edit/:id", apiHandler.EditNombreHTML)
|
||||||
|
apiGroup.POST("/html/update/:id", apiHandler.UpdateNombreHTML)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTML Routes
|
// HTML Routes
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Nombre represents the structure of the Nombres table.
|
||||||
type Nombre struct {
|
type Nombre struct {
|
||||||
NombreID int `json:"NombreID"`
|
NombreID int `json:"NombreID"`
|
||||||
FamiliaID int `json:"FamiliaID"`
|
FamiliaID int `json:"FamiliaID"`
|
||||||
|
|
@ -208,3 +209,61 @@ func (h *APIHandler) GetNombreByIDHTML(c *gin.Context) {
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "nombre.html", nombre)
|
c.HTML(http.StatusOK, "nombre.html", nombre)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *APIHandler) EditNombreHTML(c *gin.Context) {
|
||||||
|
id, err := strconv.Atoi(c.Param("id"))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var nombre Nombre
|
||||||
|
err = h.DB.QueryRow("SELECT NombreID, FamiliaID, Nombre, Fecha, ProveedorID, Precio, Inactivo FROM Nombres WHERE NombreID = ?", id).Scan(
|
||||||
|
&nombre.NombreID, &nombre.FamiliaID, &nombre.Nombre, &nombre.Fecha, &nombre.ProveedorID, &nombre.Precio, &nombre.Inactivo)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Nombre not found"})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "edit_nombre.html", nombre)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *APIHandler) UpdateNombreHTML(c *gin.Context) {
|
||||||
|
id, err := strconv.Atoi(c.Param("id")) // Declare id and err
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Invalid ID:", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var nombre Nombre
|
||||||
|
if err := c.ShouldBind(&nombre); err != nil { // Declare err
|
||||||
|
log.Println("Binding error:", err)
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Updating nombre:", nombre)
|
||||||
|
|
||||||
|
// Handle empty date
|
||||||
|
fecha := nombre.Fecha
|
||||||
|
if fecha == "" {
|
||||||
|
fecha = "0000-00-00" // Or "NULL", if your database allows it
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = h.DB.Exec("UPDATE Nombres SET FamiliaID = ?, Nombre = ?, Fecha = ?, ProveedorID = ?, Precio = ?, Inactivo = ? WHERE NombreID = ?",
|
||||||
|
nombre.FamiliaID, nombre.Nombre, fecha, nombre.ProveedorID, nombre.Precio, nombre.Inactivo, id) // Use declared id and err
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Database update error:", err)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Nombre updated successfully")
|
||||||
|
|
||||||
|
c.Redirect(http.StatusFound, "/nombres/html/"+strconv.Itoa(id))
|
||||||
|
}
|
||||||
|
|
|
||||||
21
templates/edit_nombre.html
Normal file
21
templates/edit_nombre.html
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
<form action="/nombres/html/update/{{ .NombreID }}" method="POST">
|
||||||
|
<label for="FamiliaID">FamiliaID:</label>
|
||||||
|
<input type="number" id="FamiliaID" name="FamiliaID" value="{{ .FamiliaID }}"><br><br>
|
||||||
|
|
||||||
|
<label for="Nombre">Nombre:</label>
|
||||||
|
<input type="text" id="Nombre" name="Nombre" value="{{ .Nombre }}"><br><br>
|
||||||
|
|
||||||
|
<label for="Fecha">Fecha:</label>
|
||||||
|
<input type="date" id="Fecha" name="Fecha" value="{{ .Fecha }}"><br><br>
|
||||||
|
|
||||||
|
<label for="ProveedorID">ProveedorID:</label>
|
||||||
|
<input type="number" id="ProveedorID" name="ProveedorID" value="{{ .ProveedorID }}"><br><br>
|
||||||
|
|
||||||
|
<label for="Precio">Precio:</label>
|
||||||
|
<input type="number" id="Precio" name="Precio" value="{{ .Precio }}"><br><br>
|
||||||
|
|
||||||
|
<label for="Inactivo">Inactivo:</label>
|
||||||
|
<input type="checkbox" id="Inactivo" name="Inactivo" {{ if .Inactivo }}checked{{ end }}><br><br>
|
||||||
|
|
||||||
|
<input type="submit" value="Update">
|
||||||
|
</form>
|
||||||
|
|
@ -47,6 +47,7 @@
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<a href="/nombres/html">Back to List</a>
|
<a href="/nombres/html">Back to List</a>
|
||||||
|
<a href="/nombres/html/edit/{{ .NombreID }}">Edit</a>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
Loading…
Reference in a new issue