-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_contact_update.go
73 lines (62 loc) · 1.53 KB
/
web_contact_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"database/sql"
"fmt"
"net/http"
)
func init() {
router.PATCH("/contact", app.ContactUpdate)
}
// ContactUpdate updates an existing contact in the database.
//
// > PATCH /contact HTTP/1.1
// > Content-Type: application/x-www-form-urlencoded; charset=utf-8
// > Authorization: Basic Zm9vQGV4YW1wbGUuY29tOnBhc3N3b3Jk
// > Host: localhost:3000
// > Connection: close
// >
// > id=1&firstname=Yorman&lastname=Arias&phone=6045551234&address=350+W+Georgia+St%2C+Vancouver%2C+BC&email=john%40example.com
func (app *Application) ContactUpdate(w http.ResponseWriter, r *http.Request) {
var err error
var id string
var stmt *sql.Stmt
if err = r.ParseForm(); err != nil {
fail(w, r, fmt.Errorf("r.ParseForm %s", err))
return
}
if id = r.Form.Get("id"); id == "" {
fail(w, r, fmt.Errorf("missing `id` form value"))
return
}
c := Contact{
Firstname: r.Form.Get("firstname"),
Lastname: r.Form.Get("lastname"),
Phone: r.Form.Get("phone"),
Address: r.Form.Get("address"),
Email: r.Form.Get("email"),
}
if err = c.Valid(); err != nil {
fail(w, r, err)
return
}
if stmt, err = app.db.Prepare("UPDATE contacts SET firstname=?, lastname=?, phone=?, address=?, email=? WHERE id=?"); err != nil {
fail(w, r, err)
return
}
if _, err = stmt.Exec(
c.Firstname,
c.Lastname,
c.Phone,
c.Address,
c.Email,
id,
); err != nil {
fail(w, r, err)
return
}
if c, err = contactRead(app.db, id); err != nil {
fail(w, r, err)
return
}
write(w, r, Response{Ok: true, Data: c})
}