diff --git a/cmd/main.go b/cmd/main.go index 0c9ed95..b5660e5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -47,6 +47,8 @@ func main() { apiGroup.GET("/:id", apiHandler.GetNombreByID) apiGroup.PUT("/:id", apiHandler.UpdateNombre) apiGroup.DELETE("/:id", apiHandler.DeleteNombre) + apiGroup.GET("/html/edit/:id", apiHandler.EditNombreHTML) + apiGroup.POST("/html/update/:id", apiHandler.UpdateNombreHTML) } // HTML Routes diff --git a/internal/api/handlers.go b/internal/api/handlers.go index 43264b6..742e25e 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" ) +// Nombre represents the structure of the Nombres table. type Nombre struct { NombreID int `json:"NombreID"` FamiliaID int `json:"FamiliaID"` @@ -208,3 +209,61 @@ func (h *APIHandler) GetNombreByIDHTML(c *gin.Context) { 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)) +} diff --git a/templates/edit_nombre.html b/templates/edit_nombre.html new file mode 100644 index 0000000..c52fbf1 --- /dev/null +++ b/templates/edit_nombre.html @@ -0,0 +1,21 @@ +
+ +

+ + +

+ + +

+ + +

+ + +

+ + +

+ + +
\ No newline at end of file diff --git a/templates/nombre.html b/templates/nombre.html index d25b25f..ba50517 100644 --- a/templates/nombre.html +++ b/templates/nombre.html @@ -47,6 +47,7 @@ Back to List + Edit \ No newline at end of file