improves crud example
This commit is contained in:
parent
1f1bad15ee
commit
d7203062b5
BIN
demo/crud/.crud
BIN
demo/crud/.crud
Binary file not shown.
@ -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
|
||||
}
|
||||
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Binary file not shown.
@ -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))
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user