/
models.go
43 lines (35 loc) · 912 Bytes
/
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
package api
import (
"errors"
"math"
"github.com/Kamva/mgm"
"github.com/dgrijalva/jwt-go"
)
// TokenClaims is used to create JWT token
type TokenClaims struct {
UserID string
jwt.StandardClaims
}
// User is used to store user information
type User struct {
mgm.DefaultModel `bson:",inline"`
Email string `json:"email"`
Password string `json:"-"`
GoogleUserID string `json:"-" bson:"google_user_id"`
}
// Product is a sellable element
type Product struct {
mgm.DefaultModel `bson:",inline"`
Title string `json:"title"`
Price float64 `json:"price"`
CreatedBy string `json:"created_by"`
}
func (p Product) validate() error {
if len(p.Title) < 5 {
return errors.New("The title has to have at leat 5 characters")
}
if p.Price != math.Round(p.Price*100)/100 {
return errors.New("The price has to many decimal places")
}
return nil
}