-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbookshandler.go
93 lines (78 loc) · 2.39 KB
/
bookshandler.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
package main
import (
"strings"
"github.com/jirenius/go-res"
"github.com/jirenius/go-res/store"
"github.com/rs/xid"
)
// BooksHandler is a handler for book collection requests.
type BooksHandler struct {
BookStore *BookStore
}
// SetOption sets the res.Handler options.
func (h *BooksHandler) SetOption(rh *res.Handler) {
rh.Option(
// Handler handels a collection
res.Collection,
// QueryStore handler that handles get requests and change events.
store.QueryHandler{
QueryStore: h.BookStore.BooksByTitle,
// The transformer transforms the QueryStore's resulting collection
// of id strings, []string{"1","2"}, into a collection of resource
// references, []res.Ref{"library.book.1","library.book.2"}.
Transformer: store.IDToRIDCollectionTransformer(func(id string) string {
return "library.book." + id
}),
},
// New call method handler, for creating new books.
res.Call("new", h.newBook),
// Delete call method handler, for deleting books.
res.Call("delete", h.deleteBook),
)
}
// newBook handles new call requests on the book collection.
func (h *BooksHandler) newBook(r res.CallRequest) {
// Parse request parameters into a book model
var book Book
r.ParseParams(&book)
// Trim whitespace
book.Title = strings.TrimSpace(book.Title)
book.Author = strings.TrimSpace(book.Author)
// Check if we received both title and author
if book.Title == "" || book.Author == "" {
r.InvalidParams("Must provide both title and author")
return
}
// Create a new ID for the book
book.ID = xid.New().String()
// Create a store write transaction
txn := h.BookStore.Write(book.ID)
defer txn.Close()
// Add the book to the store.
// This will produce an add event for the books collection.
if err := txn.Create(book); err != nil {
r.Error(err)
return
}
// Return a resource reference to a new book
r.Resource("library.book." + book.ID)
}
// deleteBook handles delete call requests on the book collection.
func (h *BooksHandler) deleteBook(r res.CallRequest) {
// Unmarshal parameters to an anonymous struct
var p struct {
ID string `json:"id,omitempty"`
}
r.ParseParams(&p)
// Create a store write transaction
txn := h.BookStore.Write(p.ID)
defer txn.Close()
// Delete the book from the store.
// This will produce a remove event for the books collection.
if err := txn.Delete(); err != nil {
r.Error(err)
return
}
// Send success response.
r.OK(nil)
}