+31
Go/webserver/search.hurl
+31
Go/webserver/search.hurl
···
1
+
GET http://localhost:8000/pessoas
2
+
HTTP 400
3
+
[Asserts]
4
+
body contains "Missing query param 't' in request"
5
+
6
+
GET http://localhost:8000/pessoas
7
+
[QueryStringParams]
8
+
t: invalid
9
+
HTTP 200
10
+
[Asserts]
11
+
header "Content-Type" == "application/json"
12
+
jsonpath "$" count == 0
13
+
14
+
GET http://localhost:8000/pessoas
15
+
[QueryStringParams]
16
+
t: berto
17
+
HTTP 200
18
+
[Asserts]
19
+
header "Content-Type" == "application/json"
20
+
jsonpath "$" count == 1
21
+
jsonpath "$[0].nickname" == "josé"
22
+
23
+
GET http://localhost:8000/pessoas
24
+
[QueryStringParams]
25
+
t: node
26
+
HTTP 200
27
+
[Asserts]
28
+
header "Content-Type" == "application/json"
29
+
jsonpath "$" count == 1
30
+
jsonpath "$[0].nickname" == "josé"
31
+
+31
Go/webserver/src/db.go
+31
Go/webserver/src/db.go
···
70
70
return person, nil
71
71
}
72
72
73
+
func (db *DB) SearchPerson(param string) ([]Person, error) {
74
+
rows, err := db.connection.Query(`
75
+
SELECT
76
+
id,
77
+
name,
78
+
nickname,
79
+
birthdate,
80
+
stacks
81
+
FROM people
82
+
WHERE LOWER(name) LIKE '%' || ? || '%'
83
+
OR LOWER(nickname) LIKE '%' || ? || '%'
84
+
OR LOWER(stacks) LIKE '%' || ? || '%';`, param, param, param)
85
+
86
+
if err != nil {
87
+
return nil, err
88
+
}
89
+
defer rows.Close()
90
+
91
+
people := []Person{}
92
+
for rows.Next() {
93
+
i := Person{}
94
+
if err = rows.Scan(&i.Id, &i.Name, &i.Nickname, &i.Birthdate, &i.Stack); err != nil {
95
+
return nil, err
96
+
}
97
+
98
+
people = append(people, i)
99
+
}
100
+
101
+
return people, nil
102
+
}
103
+
73
104
func (db *DB) CountAllPeople() (int, error) {
74
105
row := db.connection.QueryRow(`SELECT COUNT(*) FROM people;`)
75
106
+27
-1
Go/webserver/src/http.go
+27
-1
Go/webserver/src/http.go
···
10
10
11
11
var jsonContentType = "application/json"
12
12
13
+
// ERRORS
14
+
var ErrIDNotFound = fmt.Errorf("ID not found")
15
+
var ErrInsertPerson = fmt.Errorf("Error inserting person")
16
+
var ErrQueryParamObrigatory = fmt.Errorf("Missing query param 't' in request")
17
+
var ErrSearchPeople = fmt.Errorf("Failed to search for people")
18
+
13
19
type httpServer struct {
14
20
People *People
15
21
db *DB
···
24
30
r := &http.ServeMux{}
25
31
r.HandleFunc("GET /pessoas/{id}", server.handleGet)
26
32
r.HandleFunc("POST /pessoas", server.handlePost)
33
+
r.HandleFunc("GET /pessoas", server.handleSearch)
27
34
r.HandleFunc("GET /contagem-pessoas", server.handleCount)
28
35
29
36
return &http.Server{
···
61
68
id, err := h.People.Insert(h.db, newPerson)
62
69
if err != nil {
63
70
fmt.Printf("Error insert person %s\n", err)
64
-
http.Error(w, "Error inserting person", http.StatusBadRequest)
71
+
http.Error(w, ErrInsertPerson.Error(), http.StatusBadRequest)
65
72
return
66
73
}
67
74
···
74
81
val, _ := h.db.CountAllPeople()
75
82
io.WriteString(w, strconv.Itoa(val))
76
83
}
84
+
85
+
func (h *httpServer) handleSearch(w http.ResponseWriter, req *http.Request) {
86
+
searchParam := req.URL.Query().Get("t")
87
+
88
+
if searchParam == "" {
89
+
http.Error(w, ErrQueryParamObrigatory.Error(), http.StatusBadRequest)
90
+
return
91
+
}
92
+
93
+
people, err := h.People.Search(h.db, searchParam)
94
+
if err != nil {
95
+
fmt.Printf("Error searching for people %s\n", err)
96
+
http.Error(w, ErrSearchPeople.Error(), http.StatusBadRequest)
97
+
return
98
+
}
99
+
100
+
w.Header().Set("Content-Type", jsonContentType)
101
+
json.NewEncoder(w).Encode(people)
102
+
}
+9
-2
Go/webserver/src/people.go
+9
-2
Go/webserver/src/people.go
···
73
73
return id.String(), nil
74
74
}
75
75
76
-
var ErrIDNotFound = fmt.Errorf("ID not found")
77
-
78
76
func (p *People) Get(db *DB, id string) (Person, error) {
79
77
d, err := db.GetPersonById(id)
80
78
if err != nil {
···
83
81
84
82
return d, nil
85
83
}
84
+
85
+
func (p *People) Search(db *DB, param string) ([]Person, error) {
86
+
d, err := db.SearchPerson(param)
87
+
if err != nil {
88
+
return nil, err
89
+
}
90
+
91
+
return d, nil
92
+
}