2025-11-09 11:34:11 +01:00

66 lines
1.7 KiB
Go

package api
import (
"context"
"crud/sqlite"
"html/template"
"net/http"
)
func Board(ctx context.Context, db *sqlite.Database, templ *template.Template) http.Handler {
// Implementation of Board handler
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
query := `SELECT u.id, u.name, u.username, u.email, u.phone, u.website, a.street, a.suite, a.zipcode, a.city, c.name as company, c.catch_phrase, c.bs
FROM user u
JOIN company c ON u.company_id = c.id
JOIN address a ON u.address_id = a.id;
`
w.Header().Set("Content-Type", "text/html")
// w.Header().Set("Content-Type", "application/json")
records, err := db.ReadRecords(ctx, query)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create a data structure to pass to template
data := map[string]interface{}{
"records": records,
}
// Execute template with proper error handling
if err := templ.ExecuteTemplate(w, "board", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
},
)
}
func DeleteRecord(ctx context.Context, db *sqlite.Database, templ *template.Template) http.Handler {
// Implementation of DeleteRecord handler
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "Missing id parameter", http.StatusBadRequest)
return
}
if err := db.DeleteRecord(ctx, "users", "id", id); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Redirect or respond with success
http.Redirect(w, r, "/board", http.StatusSeeOther)
},
)
}