-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (63 loc) · 1.91 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
package main
import (
"encoding/json"
"math/rand"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
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"`
}
var books []Book
func getBooks(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(books)
}
func getBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "applcation/json")
params := mux.Vars(r)
for _, item := range books {
if item.ID == params["id"] {
json.NewEncoder(w).Encode(item)
return
}
}
json.NewEncoder(w).Encode(&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(1000000))
books = append(books, book)
json.NewEncoder(w).Encode(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() {
r := mux.NewRouter()
// books = append(books, Book{ID: "1", Isbn: "2342", Title: "the hate story", Author: &Author{Firstname: "kunal", Lastname: "Kumar"}})
// books = append(books, Book{ID: "2", Isbn: "6775", Title: "the hate story 2", Author: &Author{Firstname: "kunal kusma", Lastname: "Kumar"}})
r.HandleFunc("/api/book", getBooks).Methods("GET")
r.HandleFunc("/api/book/{id}", getBook).Methods("GET")
r.HandleFunc("/api/book", createBook).Methods("POST")
r.HandleFunc("/api/book/{id}", deleteBook).Methods("DELETE")
http.ListenAndServe(":8002", r)
}