-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
121 lines (104 loc) · 3.07 KB
/
handler.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"html/template"
"net/http"
"strconv"
"strings"
)
type details = struct {
Profile UserProfile
People []Person
}
// Handler handles the /contact/ request
func Handler(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.Path, "/")
if len(parts) >= 3 {
logger.Info("Handling contact request")
contactID, err := strconv.Atoi(parts[2])
if err != nil {
http.Error(w, "Invalid contact ID", http.StatusBadRequest)
return
}
logger.Info("Query the database to retrieve the person by ID.")
person, err := getPersonByID(contactID)
if err != nil {
http.Error(w, "Failed to retrieve contact", http.StatusInternalServerError)
return
}
if len(parts) == 4 {
logger.Info("Handling contact request with action")
action := parts[3]
if action == "edit" {
logger.Info("Handling contact request with action edit")
t := template.Must(template.ParseFiles("edit.html"))
t.ExecuteTemplate(w, "person", person)
return
}
}
if r.Method == "PUT" {
logger.Info("Handling contact request with PUT")
err := updatePerson(person, r.FormValue("name"), r.FormValue("email"))
if err != nil {
http.Error(w, "Failed to update contact", http.StatusInternalServerError)
return
}
}
t := template.Must(template.ParseFiles("row.html"))
t.ExecuteTemplate(w, "person", person)
return
}
logger.Info("Handling /contact/ request with no ID")
if r.Method == "POST" {
logger.Info("Handling contact request with POST")
}
logger.Info("Query the database to retrieve all people.")
people, err := getAllPeople()
if err != nil {
logger.Error("Failed to retrieve people", "error", err)
http.Error(w, "Failed to retrieve people", http.StatusInternalServerError)
return
}
logger.Debug("People", "people", len(people))
t := template.Must(template.ParseFiles("base.html", "contact.html", "row.html"))
profile := getProfile(r)
details := details{
Profile: profile,
People: people,
}
t.Execute(w, details)
return
}
func getPersonByID(id int) (Person, error) {
var person Person
logger.Debug("Getting person by ID", "id", id, "query", "SELECT id, name, email FROM people WHERE id = $1")
err := db.QueryRow("SELECT id, name, email FROM people WHERE id = $1", id).Scan(&person.Id, &person.Name, &person.Email)
if err != nil {
return person, err
}
return person, nil
}
func updatePerson(person Person, newName, newEmail string) error {
query := "UPDATE people SET name = $1, email = $2 WHERE id = $3"
logger.Debug("Updating person", "query", query, "$1", newName, "$2", newEmail, "$3", person.Id)
_, err := db.Exec(query, newName, newEmail, person.Id)
return err
}
func getAllPeople() ([]Person, error) {
query := "SELECT id, name, email FROM people"
logger.Debug("Getting all people", "query", query)
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
var people []Person
for rows.Next() {
var person Person
err := rows.Scan(&person.Id, &person.Name, &person.Email)
if err != nil {
return nil, err
}
people = append(people, person)
}
return people, nil
}