-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
173 lines (145 loc) · 3.68 KB
/
handlers.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
// "database/sql"
"encoding/json"
"time"
// "fmt"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
func Index(w http.ResponseWriter, r *http.Request) {
// index route
msg := Msg{Msg: "Welcome to the shades!", Time: time.Now()}
// http-headers
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(msg); err != nil {
panic(err)
}
}
func ShadesIndex(w http.ResponseWriter, r *http.Request) {
// index of all shades
db, err := Connect()
if err != nil {
log.Fatal(err)
}
// db query
rows, err := db.Query("SELECT Id, Grey, Hex FROM shades")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
shades := []Shade{}
for rows.Next() {
var r Shade
err = rows.Scan(&r.Id, &r.Grey, &r.Hex)
if err != nil {
log.Fatal(err)
}
shades = append(shades, r)
}
// http-headers
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(shades); err != nil {
panic(err)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
db.Close()
}
func ShadeShow(w http.ResponseWriter, r *http.Request) {
// show a particular shade
vars := mux.Vars(r)
Pattern := vars["Pattern"]
FilterType := vars["FilterType"]
db, err := Connect()
if err != nil {
log.Fatal(err)
}
var res Shade
if FilterType == "id" {
err = db.QueryRow("SELECT Id, Grey, Hex FROM shades WHERE Id = ?", Pattern).Scan(&res.Id, &res.Grey, &res.Hex)
} else if FilterType == "hex" {
Pattern = "#" + Pattern
err = db.QueryRow("SELECT Id, Grey, Hex FROM shades WHERE Hex = ?", Pattern).Scan(&res.Id, &res.Grey, &res.Hex)
}
// http-headers
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err != nil {
// log.Fatal("Hello", err)
errMsg := Error{Error: "Query was not successful", Time: time.Now()}
if err := json.NewEncoder(w).Encode(errMsg); err != nil {
panic(err)
}
} else {
if err := json.NewEncoder(w).Encode(res); err != nil {
panic(err)
}
}
}
func ShadeRand(w http.ResponseWriter, r *http.Request) {
// show a random shade
db, err := Connect()
if err != nil {
log.Fatal(err)
}
var res Shade
err = db.QueryRow("SELECT Id, Grey, Hex FROM shades ORDER BY RAND() LIMIT 1").Scan(&res.Id, &res.Grey, &res.Hex)
// http-headers
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err != nil {
// log.Fatal("Hello", err)
errMsg := Error{Error: "Query was not successful", Time: time.Now()}
if err := json.NewEncoder(w).Encode(errMsg); err != nil {
panic(err)
}
} else {
if err := json.NewEncoder(w).Encode(res); err != nil {
panic(err)
}
}
}
func ShadesRand(w http.ResponseWriter, r *http.Request) {
// show a number of random shades
vars := mux.Vars(r)
RandAmount := vars["RandAmount"]
db, err := Connect()
if err != nil {
log.Fatal(err)
}
rows, err := db.Query("SELECT Id, Grey, Hex FROM shades ORDER BY RAND() LIMIT ?", RandAmount)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
shades := []Shade{}
for rows.Next() {
var res Shade
err = rows.Scan(&res.Id, &res.Grey, &res.Hex)
if err != nil {
log.Fatal(err)
}
shades = append(shades, res)
}
// http-headers
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err != nil {
// log.Fatal("Hello", err)
errMsg := Error{Error: "Query was not successful", Time: time.Now()}
if err := json.NewEncoder(w).Encode(errMsg); err != nil {
panic(err)
}
} else {
if err := json.NewEncoder(w).Encode(shades); err != nil {
panic(err)
}
}
}