-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlers.go
More file actions
72 lines (55 loc) · 1.74 KB
/
Handlers.go
File metadata and controls
72 lines (55 loc) · 1.74 KB
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
package main
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
)
func Index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
allAirports := GetAllRows("airport")
if err := json.NewEncoder(w).Encode(allAirports); err != nil {
panic(err)
}
}
func Show(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
airportCode := strings.Trim(vars["airportCode"], " ")
airportInfo := GetRows("airport", "IATA", airportCode)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(airportInfo); err != nil {
panic(err)
}
}
func ShowById(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := strings.Trim(vars["Id"], " ")
airportInfo := GetRows("airport", "Id", id)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(airportInfo); err != nil {
panic(err)
}
}
func City(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
city := strings.Trim(vars["city"], " ")
airportInfo := GetRows("airport", "City", city)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(airportInfo); err != nil {
panic(err)
}
}
func Country(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
country := strings.Trim(vars["country"], " ")
airportInfo := GetRows("airport", "Country", country)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(airportInfo); err != nil {
panic(err)
}
}