-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
118 lines (100 loc) · 2.84 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
package main
import (
"encoding/json"
"github.com/gorilla/mux"
"log"
"math/rand"
"net/http"
"strconv"
)
// Init book var as a slice Book struct
var books []Book
// Book struct ( Model )
type Book struct {
ID string `json:"id"`
Isbn string `json:"isbn"`
Title string `json:"title"`
Author *Author `json:"author"`
}
// Author struct ( Model )
type Author struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
// Get all books
func getBooks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "Application/json")
json.NewEncoder(w).Encode(books)
}
// Get a single book
func getBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "Application/json")
// Get params
params := mux.Vars(r)
// Loop through books and find id
for _, item := range books {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&Book{})
}
// Create a single book
func createBooks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "Application/json")
var book Book
_ = json.NewDecoder(r.Body).Decode(&book)
book.ID = strconv.Itoa(rand.Intn(1000000))
books = append(books, book)
json.NewEncoder(w).Encode(book)
}
// Update a single book
func updateBooks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "Application/json")
params:= mux.Vars(r)
for index, item := range books {
if item.ID == params["id"] {
books = append(books[:index], books[index+1:]...)
var book Book
_ = json.NewDecoder(r.Body).Decode(&book)
book.ID = params["id"]
books = append(books, book)
json.NewEncoder(w).Encode(book)
return
}
}
json.NewEncoder(w).Encode(books)
}
// Delete a single book
func deleteBooks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "Application/json")
params := mux.Vars(r)
for index, item := range books {
if item.ID == params["id"] {
books = append(books[:index], books[index+1:]...)
break
}
}
json.NewEncoder(w).Encode(books)
}
func main() {
// Init router
r := mux.NewRouter()
// Mock data
// TODO implement Database
books = append(books, Book{"1", "447541", "Everything I Never Told You",
&Author{"Celeste", "Celeste"}})
books = append(books, Book{"2", "447431", "Are You There, Vodka? It’s Me, Chelsea",
&Author{"Chelsea", "Handler"}})
books = append(books, Book{"3", "447001", "Love in the Time of Cholera",
&Author{"Gabriel", "Márquez"}})
// Route handlers
r.HandleFunc("/api/books", getBooks).Methods("GET")
r.HandleFunc("/api/books/{id}", getBook).Methods("GET")
r.HandleFunc("/api/books", createBooks).Methods("POST")
r.HandleFunc("/api/books/{id}", updateBooks).Methods("PUT")
r.HandleFunc("/api/books/{id}", deleteBooks).Methods("DELETE")
// Start the server
log.Fatal(http.ListenAndServe(":8000", r))
}