crud improved
This commit is contained in:
parent
df23a8b604
commit
3e508412e1
@ -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,55 +117,45 @@ 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
|
||||
@ -175,18 +163,15 @@ func PatchRecord(ctx context.Context, db *sqlite.Database, templ *template.Templ
|
||||
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)
|
||||
},
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
<script type="module" src="datastar.js"></script>
|
||||
<style>
|
||||
#board {
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user