-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
84 lines (66 loc) · 1.96 KB
/
Copy pathrequest.go
File metadata and controls
84 lines (66 loc) · 1.96 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
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
)
var session *sql.DB
// JSONResponse struct: Response (tyipcally an error) back to the client in integer form
type JSONResponse struct {
Response int `json:"response"`
}
// TempResponse struct: Response back for getting one temperature
type TempResponse struct {
Temp Temp `json:"response"`
}
// TempsResponse struct: Response back for getting all of the temperatures
type TempsResponse struct {
Temps []Temp `json:"response"`
}
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "PiBakeAPI v2, written and developed by Wyatt J. Miller, copyright 2019")
}
// GetTemps function: it's only a placeholder currently
func GetTemps(w http.ResponseWriter, r *http.Request) {
var entry Temp
var temps []Temp
query, err := session.Query("SELECT * FROM `tempdata`")
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{http.StatusBadRequest})
return
}
for query.Next() {
err = query.Scan(&entry.UUID, &entry.Time, &entry.Date, &entry.TempF, &entry.TempC)
temps = append(temps, entry)
}
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{http.StatusBadGateway})
} else {
json.NewEncoder(w).Encode(TempsResponse{temps})
}
}
// GetTempOne function: WIP
func GetTempOne(w http.ResponseWriter, r *http.Request) {
var entry Temp
vars := mux.Vars(r)
count := vars["id"]
query, err := session.Query("SELECT * FROM `tempdata` WHERE `uuid` = ?", count)
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{http.StatusBadRequest})
return
}
for query.Next() {
err = query.Scan(&entry.UUID, &entry.Time, &entry.Date, &entry.TempF, &entry.TempC)
}
if err != nil {
json.NewEncoder(w).Encode(JSONResponse{http.StatusBadGateway})
} else {
json.NewEncoder(w).Encode(TempResponse{entry})
}
}
// DeleteTempOne function: it's only a placeholder currently
func DeleteTempOne(w http.ResponseWriter, r *http.Request) {
// delete one code goes here
}