-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
63 lines (49 loc) · 1.61 KB
/
Copy pathcreate.go
File metadata and controls
63 lines (49 loc) · 1.61 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
package main
import (
"encoding/json"
"log"
"net/http"
)
// CreateTempOne function that receieves a HTTP POST request and stores data in database.
// Analogous to the import script we had earlier in the project
func CreateTempOne(w http.ResponseWriter, r *http.Request) {
var entry Temp
var count int
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&entry)
if err != nil {
log.Println("Couldn't decode the JSONs!")
json.NewEncoder(w).Encode(JSONResponse{http.StatusBadRequest})
return
}
query1, err := session.Query("SELECT COUNT(*) FROM `pi` WHERE `uuid` = ?", entry.UUID)
defer query1.Close()
if err != nil {
log.Println("Database goofed: query1")
json.NewEncoder(w).Encode(JSONResponse{http.StatusBadRequest})
return
}
// check to see if any rows match the supplied UUID
for query1.Next() {
if err := query1.Scan(&count) ; err != nil {
log.Println("Scanning the database did a thing scanning the UUIDs")
}
}
// if the count from the scan is 0, insert the UUID in its place
if count == 0 {
query2, err := session.Query("INSERT INTO `pi` (uuid) VALUES (?)", entry.UUID)
defer query2.Close()
if err != nil {
log.Println("Database goofed: query2")
json.NewEncoder(w).Encode(JSONResponse{http.StatusBadRequest})
return
}
}
query3, err := session.Query("INSERT INTO `tempdata` (uuid, date, time, temp_fahrenheit, temp_celsius) VALUES (?, ?, ?, ?, ?)", entry.UUID, entry.Date, entry.Time, entry.TempF, entry.TempC)
defer query3.Close()
if err != nil {
log.Println("Database goofed: query3")
json.NewEncoder(w).Encode(JSONResponse{http.StatusBadRequest})
return
}
}