crud improved

This commit is contained in:
thomashamburg 2025-11-19 17:28:03 +01:00
parent df23a8b604
commit 3e508412e1
2 changed files with 17 additions and 32 deletions

View File

@ -105,8 +105,6 @@ func PatchRecord(ctx context.Context, db *sqlite.Database, templ *template.Templ
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
// fmt.Println("PatchRecord handler called")
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@ -119,55 +117,45 @@ func PatchRecord(ctx context.Context, db *sqlite.Database, templ *template.Templ
return return
} }
// Print id and form values to server log formData := sqlite.Record{}
// fmt.Printf("patch record %d\n", id) formData["id"] = id
formForm := sqlite.Record{}
formForm["id"] = id
for key, vals := range r.Form { for key, vals := range r.Form {
if len(vals) > 0 { if len(vals) > 0 {
formForm[key] = vals[0] formData[key] = vals[0]
} }
} }
// fmt.Println(formForm)
user, err := db.GetRecord(ctx, "user", "id", id) user, err := db.GetRecord(ctx, "user", "id", id)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
// fmt.Println(user) user["email"] = formData["email"]
// user["name"] = formForm["name"] user["phone"] = formData["phone"]
// user["username"] = formForm["username"] user["website"] = formData["website"]
user["email"] = formForm["email"]
user["phone"] = formForm["phone"]
user["website"] = formForm["website"]
address, err := db.GetRecord(ctx, "address", "id", user["address_id"]) address, err := db.GetRecord(ctx, "address", "id", user["address_id"])
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
// fmt.Println(address) address["street"] = formData["street"]
address["street"] = formForm["street"] address["suite"] = formData["suite"]
address["suite"] = formForm["suite"] address["city"] = formData["city"]
address["city"] = formForm["city"] address["zipcode"] = formData["zipcode"]
address["zipcode"] = formForm["zipcode"]
company, err := db.GetRecord(ctx, "company", "id", user["company_id"]) company, err := db.GetRecord(ctx, "company", "id", user["company_id"])
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
// fmt.Println(company) company["name"] = formData["company"]
company["name"] = formForm["company"]
// Upsert address // 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) fmt.Println("Error updating address:", err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} else {
// fmt.Printf("Updated address result: %+v\n", res)
} }
// Upsert company // Upsert company
@ -175,18 +163,15 @@ func PatchRecord(ctx context.Context, db *sqlite.Database, templ *template.Templ
fmt.Println("Error updating company:", err) fmt.Println("Error updating company:", err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} else {
// fmt.Printf("Updated company result: %+v\n", res)
} }
// Update the record in the database // Update the record in the database
if _, err := db.UpsertRecord(ctx, "user", "id", user); err != nil { if _, err := db.UpsertRecord(ctx, "user", "id", user); err != nil {
fmt.Println("Error updating record:", err) fmt.Println("Error updating record:", err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} else {
// fmt.Printf("Updated record result: %+v\n", res)
} }
// Redirect or respond with success // Redirect or respond with success
http.Redirect(w, r, "/board", http.StatusSeeOther) http.Redirect(w, r, "/board", http.StatusSeeOther)
}, },

View File

@ -10,7 +10,7 @@
<script type="module" src="datastar.js"></script> <script type="module" src="datastar.js"></script>
<style> <style>
#board { #board {
grid-template-columns: repeat(4, 1fr); grid-template-columns: repeat(3, 1fr);
} }
</style> </style>
</head> </head>