diff --git a/cmd/main.go b/cmd/main.go index e381568..3aedb03 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -21,7 +21,13 @@ func main() { // Initialize Gin router router := gin.Default() - // Initialize API handlers + // Load HTML templates + router.LoadHTMLGlob("templates/*") + + // Serve static files (CSS) + router.Static("/static", "./static") + + // Initialize API handlers (MOVE THIS UP!) apiHandler := api.NewAPIHandler(database) // Define API routes @@ -35,6 +41,10 @@ func main() { apiGroup.DELETE("/:id", apiHandler.DeleteNombre) } + // HTML Routes (MOVE THIS DOWN!) + apiGroup.GET("/html", apiHandler.GetNombresHTML) + apiGroup.GET("/html/:id", apiHandler.GetNombreByIDHTML) + // Start the server if err := router.Run(":8080"); err != nil { log.Fatalf("Failed to start server: %v", err) diff --git a/internal/api/handlers.go b/internal/api/handlers.go index dad15c1..43264b6 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -2,6 +2,7 @@ package api import ( "database/sql" + "log" "net/http" "strconv" @@ -159,3 +160,51 @@ func (h *APIHandler) DeleteNombre(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Nombre deleted"}) } + +func (h *APIHandler) GetNombresHTML(c *gin.Context) { + log.Println("GetNombresHTML called") // Add logging + rows, err := h.DB.Query("SELECT NombreID, FamiliaID, Nombre, Fecha, ProveedorID, Precio, Inactivo FROM Nombres") + if err != nil { + log.Println("Database query error:", err) // Add logging + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + defer rows.Close() + + var nombres []Nombre + for rows.Next() { + var nombre Nombre + if err := rows.Scan(&nombre.NombreID, &nombre.FamiliaID, &nombre.Nombre, &nombre.Fecha, &nombre.ProveedorID, &nombre.Precio, &nombre.Inactivo); err != nil { + log.Println("Row scan error:", err) // Add logging + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + nombres = append(nombres, nombre) + } + + log.Println("Retrieved nombres:", nombres) // Add logging + c.HTML(http.StatusOK, "nombres.html", nombres) + log.Println("nombres.html rendered") //add log +} + +func (h *APIHandler) GetNombreByIDHTML(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, "nombre.html", nombre) +} diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..15cd132 --- /dev/null +++ b/static/style.css @@ -0,0 +1,22 @@ +body { + font-family: sans-serif; +} + +table { + border-collapse: collapse; + width: 100%; +} + +th, td { + border: 1px solid #ddd; + padding: 8px; + text-align: left; +} + +th { + background-color: #f2f2f2; +} + +tr:nth-child(even) { + background-color: #f9f9f9; +} \ No newline at end of file diff --git a/templates/nombre.html b/templates/nombre.html new file mode 100644 index 0000000..5b991ad --- /dev/null +++ b/templates/nombre.html @@ -0,0 +1,21 @@ + + + + + Nombre + + + + +

Nombre Details

+

NombreID: {{ .NombreID }}

+

FamiliaID: {{ .FamiliaID }}

+

Nombre: {{ .Nombre }}

+

Fecha: {{ .Fecha }}

+

ProveedorID: {{ .ProveedorID }}

+

Precio: {{ .Precio }}

+

Inactivo: {{ .Inactivo }}

+ Back to List + + + \ No newline at end of file diff --git a/templates/nombres.html b/templates/nombres.html new file mode 100644 index 0000000..b5debde --- /dev/null +++ b/templates/nombres.html @@ -0,0 +1,13 @@ + + {{ range . }} + + {{ .NombreID }} + {{ .FamiliaID }} + {{ .Nombre }} + {{ .Fecha }} + {{ .ProveedorID }} + {{ .Precio }} + {{ .Inactivo }} + + {{ end }} + \ No newline at end of file