diff --git a/demo/crud/.crud b/demo/crud/.crud index 0958104..3a961ef 100755 Binary files a/demo/crud/.crud and b/demo/crud/.crud differ diff --git a/demo/crud/.server/api/board.go b/demo/crud/.server/api/board.go index b6d354f..c4b68cb 100644 --- a/demo/crud/.server/api/board.go +++ b/demo/crud/.server/api/board.go @@ -2,9 +2,10 @@ package api import ( "context" + "crud/sqlite" + "encoding/json" "fmt" "html/template" - "crud/sqlite" "net/http" "strconv" ) @@ -123,3 +124,34 @@ func PatchRecord(ctx context.Context, db *sqlite.Database, templ *template.Templ }, ) } + +func RecordJson(ctx context.Context, db *sqlite.Database, templ *template.Template) http.Handler { + // Implementation of EditRecord handler + return http.HandlerFunc( + + func(w http.ResponseWriter, r *http.Request) { + + id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + } + + records, err := db.ReadRecords(ctx, baseQuery+" WHERE u.id = ?;", id) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + rec := records[0] + + w.Header().Set("Content-Type", "application/json") + + if err := json.NewEncoder(w).Encode(rec); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + }, + ) +} diff --git a/demo/crud/.server/crud b/demo/crud/.server/crud index 0958104..3a961ef 100755 Binary files a/demo/crud/.server/crud and b/demo/crud/.server/crud differ diff --git a/demo/crud/.server/routes.go b/demo/crud/.server/routes.go index 012b0c2..308ff19 100644 --- a/demo/crud/.server/routes.go +++ b/demo/crud/.server/routes.go @@ -25,4 +25,6 @@ func addRoutes( mux.Handle("GET /edit-record/{id}", api.EditRecord(ctx, database, templ)) mux.Handle("PATCH /patch-record/{id}", api.PatchRecord(ctx, database, templ)) mux.Handle("DELETE /delete-record/{id}", api.DeleteRecord(ctx, database, templ)) + mux.Handle("GET /html/{id}", api.EditRecord(ctx, database, templ)) + mux.Handle("GET /json/{id}", api.RecordJson(ctx, database, templ)) } diff --git a/demo/crud/sample.go b/demo/crud/sample.go index 49afd35..80e1b35 100644 --- a/demo/crud/sample.go +++ b/demo/crud/sample.go @@ -26,14 +26,14 @@ func Board(ctx context.Context, db *sqlite.Database, templ *template.Template) h func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/html") - records, err := sqlite.NoRowsOk(db.ReadRecords(ctx, query)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } + w.Header().Set("Content-Type", "text/html") + // Execute template with proper error handling if err := templ.ExecuteTemplate(w, "board", sqlite.Record{"records": records}); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -42,3 +42,25 @@ func Board(ctx context.Context, db *sqlite.Database, templ *template.Template) h }, ) } + +// load user data and return json +func BoardJson(ctx context.Context, db *sqlite.Database, templ *template.Template) http.Handler { + return http.HandlerFunc( + + func(w http.ResponseWriter, r *http.Request) { + + records, err := sqlite.NoRowsOk(db.ReadRecords(ctx, query)) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + + if err := json.NewEncoder(w).Encode(records); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + }, + ) +}