-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (95 loc) · 3.18 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
package main
import (
"encoding/json"
"log"
"net/http"
"math/rand"
"strconv"
"github.com/gorilla/mux"
)
// Book Struct (Model)
type Book struct {
ID string `json:"id"`
Isbn string `json:"isbn"`
Title string `json:"title"`
Author *Author `json:"author"`
}
type Author struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
// initialize the books var as a slice Book struct
var books []Book
// 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")
params := mux.Vars(r) // Get params
// Loop through books and find one with the id from the params
for _, item := range books {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&Book{})
}
// Create a new book
func createBook(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(10000000)) // Mock ID - not safe
books = append(books, book)
json.NewEncoder(w).Encode(book)
}
// Update a book
func updateBook(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 book
func deleteBook(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() {
// Initialize the router
router := mux.NewRouter()
// Mock data - @todo - implement database
books = append(books, Book{ID: "1", Isbn: "448743", Title: "Book One", Author: &Author{Firstname: "John", Lastname: "Doe"}})
books = append(books, Book{ID: "2", Isbn: "847564", Title: "Book Two", Author: &Author{Firstname: "Steve", Lastname: "Smith"}})
books = append(books, Book{ID: "3", Isbn: "648743", Title: "Book Three", Author: &Author{Firstname: "Jane", Lastname: "Doe"}})
books = append(books, Book{ID: "4", Isbn: "847643", Title: "Book Four", Author: &Author{Firstname: "John", Lastname: "Doe"}})
books = append(books, Book{ID: "5", Isbn: "847643", Title: "Book Five", Author: &Author{Firstname: "John", Lastname: "Doe"}})
// route handlers / endpoints
router.HandleFunc("/api/books", getBooks).Methods("GET")
router.HandleFunc("/api/books/{id}", getBook).Methods("GET")
router.HandleFunc("/api/books", createBook).Methods("POST")
router.HandleFunc("/api/books/{id}", updateBook).Methods("PUT")
router.HandleFunc("/api/books/{id}", deleteBook).Methods("DELETE")
log.Fatal(http.ListenAndServe(":8080", router))
}