-
Notifications
You must be signed in to change notification settings - Fork 2
/
books.go
150 lines (130 loc) · 3.39 KB
/
books.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"errors"
"io"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/pborman/uuid"
)
// Book is the representation of a book
type Book struct {
ID string `json:"id" sql:"type:varchar(36);primary key" gorm:"primary_key"`
Name string `json:"name"`
Author string `json:"author"`
PublicationDate time.Time `json:"publicationDate"`
LibraryID string `json:"-"`
Library Library `json:"-"`
}
func getBooks(w http.ResponseWriter, r *http.Request) {
if libraryExists := checkLibrary(w, r); libraryExists == false {
return
}
var books []Book
db.Where("library_id = ?", mux.Vars(r)["libraryId"]).Find(&books)
writeJSON(w, books)
}
func createBook(w http.ResponseWriter, r *http.Request) {
if libraryExists := checkLibrary(w, r); libraryExists == false {
return
}
newBook, err := bookFromJSON(r.Body)
if err != nil {
http.Error(w, "Invalid JSON provided or invalid date time format", http.StatusBadRequest)
return
}
validationErrs := validateBook(*newBook)
if len(validationErrs) > 0 {
w.WriteHeader(http.StatusBadRequest)
writeJSON(w, validationErrs)
return
}
newBook.ID = uuid.NewUUID().String()
newBook.LibraryID = mux.Vars(r)["libraryId"]
db.Create(newBook)
w.Header().Set("Book-Id", newBook.ID)
w.WriteHeader(201)
}
func updateBook(w http.ResponseWriter, r *http.Request) {
if libraryExists := checkLibrary(w, r); libraryExists == false {
return
}
book, err := findOneBook(mux.Vars(r)["bookId"])
if err != nil {
http.Error(w, "", http.StatusNotFound)
return
}
update, err := bookFromJSON(r.Body)
if err != nil {
http.Error(w, "Invalid JSON provided or invalid date time format", http.StatusBadRequest)
return
}
book.Name = update.Name
book.Author = update.Author
book.PublicationDate = update.PublicationDate
validationErrs := validateBook(book)
if len(validationErrs) > 0 {
w.WriteHeader(http.StatusBadRequest)
writeJSON(w, validationErrs)
return
}
writeJSON(w, book)
}
func getOneBook(w http.ResponseWriter, r *http.Request) {
if libraryExists := checkLibrary(w, r); libraryExists == false {
return
}
book, err := findOneBook(mux.Vars(r)["bookId"])
if err != nil {
http.Error(w, "", http.StatusNotFound)
return
}
writeJSON(w, book)
}
func deleteOneBook(w http.ResponseWriter, r *http.Request) {
if libraryExists := checkLibrary(w, r); libraryExists == false {
return
}
book, err := findOneBook(mux.Vars(r)["bookId"])
if err != nil {
http.Error(w, "", http.StatusNotFound)
return
}
db.Delete(&book)
w.WriteHeader(http.StatusNoContent)
}
func checkLibrary(w http.ResponseWriter, r *http.Request) bool {
_, err := findOneLibrary(mux.Vars(r)["libraryId"])
if err != nil {
http.Error(w, "Library not found", http.StatusNotFound)
return false
}
return true
}
func bookFromJSON(data io.ReadCloser) (*Book, error) {
var book = new(Book)
decoder := json.NewDecoder(data)
err := decoder.Decode(book)
if err != nil {
return nil, err
}
return book, nil
}
func validateBook(book Book) (errs []string) {
if book.Name == "" {
errs = append(errs, "The name is required")
}
if book.Author == "" {
errs = append(errs, "The author is required")
}
return
}
func findOneBook(bookID string) (Book, error) {
var book Book
db.Where("id = ?", bookID).Find(&book)
if book.ID == "" {
return Book{}, errors.New("Book not found")
}
return book, nil
}