-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
126 lines (101 loc) · 3.08 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
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
package main
import (
"encoding/json"
"log"
"math/rand"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
//House Struct(Model)
type House struct {
ID string `json:"id"`
Rooms string `json:"rooms"`
Location string `json:"location"`
Owner *Owner `json:"owner"`
}
// Author Struct
type Owner struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
// init houses var as slice house struct
var houses []House
// get all houses
func getHouse(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r) //get houses
//Loops through houses and find with id
for _, item := range houses {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&House{})
}
//get house
func getHouses(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(houses)
}
//create house
func createHouse(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var house House
_ = json.NewDecoder(r.Body).Decode(&house)
house.ID = strconv.Itoa(rand.Intn(100000))
houses = append(houses, house)
json.NewEncoder(w).Encode(house)
}
//update house
func updateHouse(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range houses {
if item.ID == params["id"] {
houses = append(houses[:index], houses[index+1:]...)
var house House
_ = json.NewDecoder(r.Body).Decode(&house)
house.ID = params["id"]
houses = append(houses, house)
json.NewEncoder(w).Encode(house)
return
}
}
json.NewEncoder(w).Encode(houses)
}
func deleteHouse(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
for index, item := range houses {
if item.ID == params["id"] {
houses = append(houses[:index], houses[index+1:]...)
break
}
}
json.NewEncoder(w).Encode(houses)
}
func main() {
// init the mux router
router := mux.NewRouter()
//Mock Data @todo implement data
houses = append(houses, House{ID: "1", Location: "juja", Rooms: "12", Owner: &Owner{Firstname: "kelvin", Lastname: "onkundi"}})
houses = append(houses, House{ID: "2", Location: "Juja", Rooms: "12", Owner: &Owner{Firstname: "Kelvin", Lastname: "Onkundi"}})
//create router houndlers/ Endpoings
router.HandleFunc("/api/houses", getHouses).Methods("GET")
router.HandleFunc("/api/houses/{id}", getHouse).Methods("GET")
router.HandleFunc("/api/houses", createHouse).Methods("POST")
router.HandleFunc("/api/houses/{id}", updateHouse).Methods("PUT")
router.HandleFunc("/api/houses/{id}", deleteHouse).Methods("DELETE")
handler := cors.Default().Handler(router)
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:4200/"},
AllowCredentials: true,
// Enable Debugging for testing, consider disabling in production
Debug: true,
})
handler = c.Handler(handler)
log.Fatal(http.ListenAndServe(":8000", handler))
}