diff --git a/demo/crud/.server/api/board.go b/demo/crud/.server/api/board.go index 4429584..8b3a211 100644 --- a/demo/crud/.server/api/board.go +++ b/demo/crud/.server/api/board.go @@ -105,8 +105,6 @@ func PatchRecord(ctx context.Context, db *sqlite.Database, templ *template.Templ func(w http.ResponseWriter, r *http.Request) { - // fmt.Println("PatchRecord handler called") - id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -119,74 +117,61 @@ func PatchRecord(ctx context.Context, db *sqlite.Database, templ *template.Templ return } - // Print id and form values to server log - // fmt.Printf("patch record %d\n", id) - formForm := sqlite.Record{} - formForm["id"] = id + formData := sqlite.Record{} + formData["id"] = id for key, vals := range r.Form { if len(vals) > 0 { - formForm[key] = vals[0] + formData[key] = vals[0] } } - // fmt.Println(formForm) user, err := db.GetRecord(ctx, "user", "id", id) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - // fmt.Println(user) - // user["name"] = formForm["name"] - // user["username"] = formForm["username"] - user["email"] = formForm["email"] - user["phone"] = formForm["phone"] - user["website"] = formForm["website"] + user["email"] = formData["email"] + user["phone"] = formData["phone"] + user["website"] = formData["website"] address, err := db.GetRecord(ctx, "address", "id", user["address_id"]) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - // fmt.Println(address) - address["street"] = formForm["street"] - address["suite"] = formForm["suite"] - address["city"] = formForm["city"] - address["zipcode"] = formForm["zipcode"] + address["street"] = formData["street"] + address["suite"] = formData["suite"] + address["city"] = formData["city"] + address["zipcode"] = formData["zipcode"] company, err := db.GetRecord(ctx, "company", "id", user["company_id"]) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - // fmt.Println(company) - company["name"] = formForm["company"] + company["name"] = formData["company"] // Upsert address - if _, err := db.UpsertRecord(ctx, "address", "id", address); err != nil { + if _,err := db.UpsertRecord(ctx, "address", "id", address); err != nil { fmt.Println("Error updating address:", err) http.Error(w, err.Error(), http.StatusInternalServerError) return - } else { - // fmt.Printf("Updated address result: %+v\n", res) - } + } // Upsert company if _, err := db.UpsertRecord(ctx, "company", "id", company); err != nil { fmt.Println("Error updating company:", err) http.Error(w, err.Error(), http.StatusInternalServerError) return - } else { - // fmt.Printf("Updated company result: %+v\n", res) - } + } + // Update the record in the database if _, err := db.UpsertRecord(ctx, "user", "id", user); err != nil { fmt.Println("Error updating record:", err) http.Error(w, err.Error(), http.StatusInternalServerError) return - } else { + } - // fmt.Printf("Updated record result: %+v\n", res) - } // Redirect or respond with success http.Redirect(w, r, "/board", http.StatusSeeOther) }, diff --git a/demo/crud/.server/frontend/index.html b/demo/crud/.server/frontend/index.html index 673cf85..cd1552a 100644 --- a/demo/crud/.server/frontend/index.html +++ b/demo/crud/.server/frontend/index.html @@ -10,7 +10,7 @@