crud example patch completed

This commit is contained in:
thomashamburg 2025-11-19 12:37:22 +01:00
parent a833546227
commit df23a8b604
4 changed files with 100 additions and 32 deletions

View File

@ -105,22 +105,90 @@ 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") // 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 {
// w.WriteHeader(http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
// w.Write([]byte(err.Error())) return
// } }
// if err := db.DeleteRecord(ctx, "user", "id", id); err != nil { // Parse form values
// http.Error(w, err.Error(), http.StatusInternalServerError) if err := r.ParseForm(); err != nil {
// return 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 // Redirect or respond with success
http.Redirect(w, r, "/board", http.StatusSeeOther) http.Redirect(w, r, "/board", http.StatusSeeOther)
}, },
) )
} }

Binary file not shown.

View File

@ -39,7 +39,7 @@ var templatesCompiled = template.Must(template.ParseFS(templates, "templates/*.h
// Calling the run() function. // Calling the run() function.
func main() { func main() {
fmt.Println("Developement mode") // fmt.Println("Developement mode")
ctx := context.Background() ctx := context.Background()
@ -195,17 +195,17 @@ func printFSTree(efs fs.FS, root string, indentLevel int) error {
// authMiddleware is a simple authentication middleware // authMiddleware is a simple authentication middleware
func headerMiddleware(next http.Handler) http.Handler { func headerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Request URL:", r.URL.String()) // fmt.Println("Request URL:", r.URL.String())
fmt.Println("Request Headers:") // fmt.Println("Request Headers:")
for key, values := range r.Header { // for key, values := range r.Header {
for _, value := range values { // for _, value := range values {
if key == "Referer" || strings.HasPrefix(key, "Hx") { // if key == "Referer" || strings.HasPrefix(key, "Hx") {
fmt.Printf("%s: %s\n", key, value) // // fmt.Printf("%s: %s\n", key, value)
} // }
} // }
} // }
fmt.Println() // fmt.Println()
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}) })
} }

View File

@ -66,54 +66,54 @@
<h3>{{.name}}</h3> <h3>{{.name}}</h3>
<p>{{.username}}</p> <p>{{.username}}</p>
</header> </header>
<form> <form id="form">
<label> <label>
Email Email
<input type="email" value="{{.email}}" /> <input name="email" type="email" value="{{.email}}" />
</label> </label>
<label> <label>
Phone Phone
<input type="tel" value="{{.phone}}" /> <input name="phone" type="tel" value="{{.phone}}" />
</label> </label>
<label> <label>
Website Website
<input type="url" value="{{.website}}" /> <input name="website" type="text" value="{{.website}}" />
</label> </label>
<label> <label>
Company Company
<input type="text" value="{{.company}}" /> <input name="company" type="text" value="{{.company}}" />
</label> </label>
<fieldset role="group"> <fieldset role="group">
<label> <label>
Street Street
<input type="text" value="{{.street}}" /> <input name="street" type="text" value="{{.street}}" />
</label> </label>
<label> <label>
Suite Suite
<input type="text" value="{{.suite}}" /> <input name="suite" type="text" value="{{.suite}}" />
</label> </label>
</fieldset> </fieldset>
<fieldset role="group"> <fieldset role="group">
<label> <label>
City City
<input type="text" value="{{.city}}" /> <input name="city" type="text" value="{{.city}}" />
</label> </label>
<label> <label>
Zipcode Zipcode
<input type="text" value="{{.zipcode}}" /> <input name="zipcode" type="text" value="{{.zipcode}}" />
</label> </label>
</fieldset> </fieldset>
</label> </label>
</form> </form>
<footer class="grid"> <footer class="grid">
<button class="primary" data-on:click="@patch('patch-record/{{.id}}')">Save</button> <button class="primary" data-on:click="@patch('patch-record/{{.id}}', {contentType: 'form', selector: '#form'})">Save</button>
<button class="secondary" data-on:click="@get('board')">Cancel</button> <button class="secondary" data-on:click="@get('board')">Cancel</button>
</article> </article>
{{end}} {{end}}