/
book.go
114 lines (95 loc) · 2.47 KB
/
book.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
package api
import "github.com/kataras/iris"
// Book type with Name, Author and ISBN
type Book struct {
Title string `json:"title"`
Author string `json:"author"`
ISBN string `json:"isbn"`
Description string `json:"description,omitempty"`
}
var books = map[string]Book{
"0345391802": Book{Title: "The Hitchhiker's Guide to the Galaxy", Author: "Douglas Adams", ISBN: "0345391802"},
"0000000000": Book{Title: "Cloud Native Go", Author: "M.-Leander Reimer", ISBN: "0000000000"},
}
// AllBooks returns a slice of all books
func AllBooks() []Book {
values := make([]Book, len(books))
idx := 0
for _, book := range books {
values[idx] = book
idx++
}
return values
}
// AllBooksHandler to be used as Handler for Book API
func AllBooksHandler(ctx iris.Context) {
ctx.JSON(AllBooks())
}
// CreateBookHandler to be used as Handler for Book API
func CreateBookHandler(ctx iris.Context) {
book := Book{}
if err := ctx.ReadJSON(book); err != nil {
ctx.StatusCode(400)
return
}
isbn, created := CreateBook(book)
if created {
ctx.Redirect("/api/books/"+isbn, iris.StatusCreated)
} else {
ctx.StatusCode(iris.StatusConflict)
}
}
// GetBookHandler to be used as Handler for Book API
func GetBookHandler(ctx iris.Context) {
isbn := ctx.Params().Get("isbn")
book, found := GetBook(isbn)
if !found {
ctx.StatusCode(iris.StatusNotFound)
return
}
ctx.JSON(book)
}
// UpdateBookHandler to be used as Handler for Book API
func UpdateBookHandler(ctx iris.Context) {
isbn := ctx.Params().Get("isbn")
book := Book{ISBN: isbn}
if err := ctx.ReadJSON(book); err != nil {
ctx.StatusCode(400)
return
}
exists := UpdateBook(isbn, book)
if !exists {
ctx.StatusCode(iris.StatusNotFound)
}
}
// DeleteBookHandler to be used as Handler for Book API
func DeleteBookHandler(ctx iris.Context) {
isbn := ctx.Params().Get("isbn")
DeleteBook(isbn)
}
// GetBook returns the book for a given ISBN
func GetBook(isbn string) (Book, bool) {
book, found := books[isbn]
return book, found
}
// CreateBook creates a new Book if it does not exist
func CreateBook(book Book) (string, bool) {
_, exists := books[book.ISBN]
if exists {
return "", false
}
books[book.ISBN] = book
return book.ISBN, true
}
// UpdateBook updates an existing book
func UpdateBook(isbn string, book Book) bool {
_, exists := books[isbn]
if exists {
books[isbn] = book
}
return exists
}
// DeleteBook removes a book from the map by ISBN key
func DeleteBook(isbn string) {
delete(books, isbn)
}