diff --git a/demo/crud/.server/api/board.go b/demo/crud/.server/api/board.go index c4b68cb..4429584 100644 --- a/demo/crud/.server/api/board.go +++ b/demo/crud/.server/api/board.go @@ -105,22 +105,90 @@ func PatchRecord(ctx context.Context, db *sqlite.Database, templ *template.Templ func(w http.ResponseWriter, r *http.Request) { - fmt.Println("PatchRecord handler called") + // fmt.Println("PatchRecord handler called") - // id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) - // if err != nil { - // w.WriteHeader(http.StatusInternalServerError) - // w.Write([]byte(err.Error())) - // } + id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } - // if err := db.DeleteRecord(ctx, "user", "id", id); err != nil { - // http.Error(w, err.Error(), http.StatusInternalServerError) - // return - // } + // Parse form values + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + // Print id and form values to server log + // fmt.Printf("patch record %d\n", id) + formForm := sqlite.Record{} + formForm["id"] = id + for key, vals := range r.Form { + if len(vals) > 0 { + formForm[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"] + + 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"] + + 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"] + + // Upsert address + 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/crud b/demo/crud/.server/crud index 3a961ef..19991af 100755 Binary files a/demo/crud/.server/crud and b/demo/crud/.server/crud differ diff --git a/demo/crud/.server/main_dev.go b/demo/crud/.server/main_dev.go index 7aa0e5e..502ecbe 100644 --- a/demo/crud/.server/main_dev.go +++ b/demo/crud/.server/main_dev.go @@ -39,7 +39,7 @@ var templatesCompiled = template.Must(template.ParseFS(templates, "templates/*.h // Calling the run() function. func main() { - fmt.Println("Developement mode") + // fmt.Println("Developement mode") ctx := context.Background() @@ -195,17 +195,17 @@ func printFSTree(efs fs.FS, root string, indentLevel int) error { // authMiddleware is a simple authentication middleware func headerMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Println("Request URL:", r.URL.String()) - fmt.Println("Request Headers:") - for key, values := range r.Header { - for _, value := range values { - if key == "Referer" || strings.HasPrefix(key, "Hx") { + // fmt.Println("Request URL:", r.URL.String()) + // fmt.Println("Request Headers:") + // for key, values := range r.Header { + // for _, value := range values { + // if key == "Referer" || strings.HasPrefix(key, "Hx") { - fmt.Printf("%s: %s\n", key, value) - } - } - } - fmt.Println() + // // fmt.Printf("%s: %s\n", key, value) + // } + // } + // } + // fmt.Println() next.ServeHTTP(w, r) }) } diff --git a/demo/crud/.server/templates/board-content.html b/demo/crud/.server/templates/board-content.html index de44aec..a967498 100644 --- a/demo/crud/.server/templates/board-content.html +++ b/demo/crud/.server/templates/board-content.html @@ -66,54 +66,54 @@
{{.username}}
-