This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
108 lines (94 loc) · 2.46 KB
/
models.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
package books
import (
"errors"
"github.com/GoesToEleven/golang-web-dev/046_mongodb/16_go-mongo/config"
"gopkg.in/mgo.v2/bson"
"net/http"
"strconv"
)
type Book struct {
// add ID and tags if you need them
// ID bson.ObjectId // `json:"id" bson:"_id"`
Isbn string // `json:"isbn" bson:"isbn"`
Title string // `json:"title" bson:"title"`
Author string // `json:"author" bson:"author"`
Price float32 // `json:"price" bson:"price"`
}
func AllBooks() ([]Book, error) {
bks := []Book{}
err := config.Books.Find(bson.M{}).All(&bks)
if err != nil {
return nil, err
}
return bks, nil
}
func OneBook(r *http.Request) (Book, error) {
bk := Book{}
isbn := r.FormValue("isbn")
if isbn == "" {
return bk, errors.New("400. Bad Request.")
}
err := config.Books.Find(bson.M{"isbn": isbn}).One(&bk)
if err != nil {
return bk, err
}
return bk, nil
}
func PutBook(r *http.Request) (Book, error) {
// get form values
bk := Book{}
bk.Isbn = r.FormValue("isbn")
bk.Title = r.FormValue("title")
bk.Author = r.FormValue("author")
p := r.FormValue("price")
// validate form values
if bk.Isbn == "" || bk.Title == "" || bk.Author == "" || p == "" {
return bk, errors.New("400. Bad request. All fields must be complete.")
}
// convert form values
f64, err := strconv.ParseFloat(p, 32)
if err != nil {
return bk, errors.New("406. Not Acceptable. Price must be a number.")
}
bk.Price = float32(f64)
// insert values
err = config.Books.Insert(bk)
if err != nil {
return bk, errors.New("500. Internal Server Error." + err.Error())
}
return bk, nil
}
func UpdateBook(r *http.Request) (Book, error) {
// get form values
bk := Book{}
bk.Isbn = r.FormValue("isbn")
bk.Title = r.FormValue("title")
bk.Author = r.FormValue("author")
p := r.FormValue("price")
if bk.Isbn == "" || bk.Title == "" || bk.Author == "" || p == "" {
return bk, errors.New("400. Bad Request. Fields can't be empty.")
}
// convert form values
f64, err := strconv.ParseFloat(p, 32)
if err != nil {
return bk, errors.New("406. Not Acceptable. Enter number for price.")
}
bk.Price = float32(f64)
// update values
err = config.Books.Update(bson.M{"isbn": bk.Isbn}, &bk)
if err != nil {
return bk, err
}
return bk, nil
}
func DeleteBook(r *http.Request) error {
isbn := r.FormValue("isbn")
if isbn == "" {
return errors.New("400. Bad Request.")
}
err := config.Books.Remove(bson.M{"isbn": isbn})
if err != nil {
return errors.New("500. Internal Server Error")
}
return nil
}