-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
101 lines (76 loc) · 2.47 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"encoding/json"
// Type go get -u github.com/gorilla/mux to install
"github.com/gorilla/mux" // Unused packages will create compilation error
)
type Item struct {
UID string `json:"UID"`
Name string `json:"Name"`
Desc string `json:"Desc"`
Price float64 `json:"Price"`
}
var inventory []Item
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Function Called: homePage()")
}
func getInventory(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Println("Function Called: getInventory()")
json.NewEncoder(w).Encode(inventory)
}
func createItem(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var item Item
_ = json.NewDecoder(r.Body).Decode(&item) // Obtain item from request JSON
inventory = append(inventory, item) // Add item to inventory
json.NewEncoder(w).Encode(item) // Show item in response JSON for verification
}
func deleteItem(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
_deleteItemAtUid(params["uid"])
json.NewEncoder(w).Encode(inventory)
}
func _deleteItemAtUid(uid string) {
for index, item := range inventory {
if item.UID == uid {
// Delete item from Slice
inventory = append(inventory[:index], inventory[index+1:]...)
break
}
}
}
func updateItem(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var item Item
_ = json.NewDecoder(r.Body).Decode(&item) // Obtain item from request JSON
params := mux.Vars(r)
_deleteItemAtUid(params["uid"]) // Delete item
inventory = append(inventory, item) // Create it again with data from request
json.NewEncoder(w).Encode(inventory)
}
func handleRequests(){
// := is the short variable declaration operator
// Automatically determines type for variable
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", homePage).Methods("GET")
router.HandleFunc("/inventory", getInventory).Methods("GET")
router.HandleFunc("/inventory/{uid}", updateItem).Methods("PUT")
router.HandleFunc("/inventory/{uid}", deleteItem).Methods("DELETE")
router.HandleFunc("/inventory", createItem).Methods("POST")
log.Fatal(http.ListenAndServe(":8000", router))
}
func main() {
// Data store
inventory = append(inventory, Item{
UID: "0",
Name: "Cheese",
Desc: "A fine block of cheese.",
Price: 4.99,
})
handleRequests()
}