crud example patch completed
This commit is contained in:
parent
a833546227
commit
df23a8b604
@ -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)
|
||||
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Binary file not shown.
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
@ -66,54 +66,54 @@
|
||||
<h3>{{.name}}</h3>
|
||||
<p>{{.username}}</p>
|
||||
</header>
|
||||
<form>
|
||||
<form id="form">
|
||||
<label>
|
||||
Email
|
||||
<input type="email" value="{{.email}}" />
|
||||
<input name="email" type="email" value="{{.email}}" />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Phone
|
||||
<input type="tel" value="{{.phone}}" />
|
||||
<input name="phone" type="tel" value="{{.phone}}" />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Website
|
||||
<input type="url" value="{{.website}}" />
|
||||
<input name="website" type="text" value="{{.website}}" />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Company
|
||||
<input type="text" value="{{.company}}" />
|
||||
<input name="company" type="text" value="{{.company}}" />
|
||||
</label>
|
||||
|
||||
<fieldset role="group">
|
||||
<label>
|
||||
Street
|
||||
<input type="text" value="{{.street}}" />
|
||||
<input name="street" type="text" value="{{.street}}" />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Suite
|
||||
<input type="text" value="{{.suite}}" />
|
||||
<input name="suite" type="text" value="{{.suite}}" />
|
||||
</label>
|
||||
</fieldset>
|
||||
|
||||
<fieldset role="group">
|
||||
<label>
|
||||
City
|
||||
<input type="text" value="{{.city}}" />
|
||||
<input name="city" type="text" value="{{.city}}" />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
Zipcode
|
||||
<input type="text" value="{{.zipcode}}" />
|
||||
<input name="zipcode" type="text" value="{{.zipcode}}" />
|
||||
</label>
|
||||
</fieldset>
|
||||
</label>
|
||||
</form>
|
||||
<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>
|
||||
</article>
|
||||
{{end}}
|
||||
Loading…
x
Reference in New Issue
Block a user