-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_example.go
122 lines (99 loc) · 2.8 KB
/
api_example.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
package main
import (
"encoding/json"
"log"
"net/http"
)
//This is just an API To carryout CRUD on Toys
type Owner struct {
Name string `json:"name"`
Age string `json:"age"`
}
// Definition of toys
type Toy struct {
ID string `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
}
type ToyController struct {
Repository []Toy
}
// Initialize our ToyController
func NewToyController() *ToyController {
return &ToyController{
Repository: []Toy{
Toy{Name: "Cows", Color: "red", ID: "123456"},
},
}
}
//Responsible for retrieving all toys
func (h *ToyController) get(w http.ResponseWriter, r *http.Request) {
toys := h.Repository
encoder := json.NewEncoder(w)
encoder.Encode(toys)
}
func (h *ToyController) add(w http.ResponseWriter, r *http.Request) {
//initialize the toy
var toy Toy
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&toy)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
}
toys := append(h.Repository, toy)
h.Repository = toys
encoder := json.NewEncoder(w)
encoder.Encode(toys)
}
// Delete toys by name
func (h *ToyController) delete(w http.ResponseWriter, r *http.Request) {
// get id from the url query
id := r.URL.Query().Get("id")
//we loop through the controller repository and remove the item with ID
for index, value := range h.Repository {
if value.ID == id {
//TODO remove the item from the array
h.Repository = append(h.Repository[:index], h.Repository[index+1:]...)
break
}
}
// return whats left of the h.Repository
encoder := json.NewEncoder(w)
encoder.Encode(h.Repository)
}
func (h *ToyController) update(w http.ResponseWriter, r *http.Request) {
// get id from the url query
id := r.URL.Query().Get("id")
//retrieve the request body
var toy Toy
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&toy)
if err != nil {
http.Error(w, "An error occured", http.StatusBadRequest)
}
//we loop through the controller repository and remove the item with ID
for index, value := range h.Repository {
if value.ID == id {
//TODO remove the item from the array
h.Repository = append(h.Repository[:index], h.Repository[index+1:]...)
h.Repository = append(h.Repository, toy)
break
}
}
// return whats left of the h.Repository
encoder := json.NewEncoder(w)
encoder.Encode(h.Repository)
}
func main() {
//we declare the server port
port := 9000
toyController := NewToyController()
http.HandleFunc("/toys", toyController.get)
http.HandleFunc("/toys/add", toyController.add)
http.HandleFunc("/toys/delete", toyController.delete)
http.HandleFunc("/toys/update", toyController.update)
// We tell what port the server is listening
log.Printf("Server is started and is listening on port %v", port)
// Start the server and listen on a port
log.Fatal(http.ListenAndServe(":9000", nil))
}