This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
124 lines (99 loc) · 2.96 KB
/
database.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
package main
import (
"encoding/gob"
"time"
)
func init() {
gob.Register(User{})
}
/****************************************************
* DATABASE *
****************************************************/
type User struct {
ID int `db:"id"`
Auth0 string `db:"auth0"`
Name string `db:"name"`
Email string `db:"email"`
Picture string `db:"picture"`
Ratings int `db:"ratings"`
Reviews int `db:"reviews"`
}
func (u User) Authenticated() bool {
return u.ID != 0
}
type Whisky struct {
ID int `db:"id" form:"id"`
Type string `db:"type" form:"type"`
Distillery string `db:"distillery" form:"distillery"`
Name string `db:"name" form:"name"`
Age int `db:"age" form:"age"`
ABV float64 `db:"abv" form:"abv"`
Description string `db:"description" form:"description"`
Picture string `db:"picture" form:"picture"`
Thumbnail string `db:"thumbnail" form:"thumbnail"`
Rating float64 `db:"rating"`
Ratings int `db:"ratings"`
Reviews int `db:"reviews"`
}
type Review struct {
ID int `db:"id" form:"id"`
Date time.Time `db:"date"`
UserID int `db:"user_id"`
WhiskyID int `db:"whisky_id" form:"whisky"`
Rating int `db:"rating" form:"rating"`
Description string `db:"description" form:"description"`
User *User
Whisky *Whisky
}
var (
UserStoreKey = "github.com/sprungknoedl/whiskee/user-store"
WhiskyStoreKey = "github.com/sprungknoedl/whiskee/whisky-store"
ReviewStoreKey = "github.com/sprungknoedl/whiskee/review-store"
)
type UserStore interface {
GetUser(int) (User, error)
GetAllUser() ([]User, error)
SaveUser(User) (User, error)
}
type WhiskyStore interface {
GetWhisky(int) (Whisky, error)
GetAllWhiskies() ([]Whisky, error)
SaveWhisky(Whisky) (Whisky, error)
GetTrending(limit int) ([]Whisky, error)
}
type ReviewStore interface {
GetReview(int) (Review, error)
SaveReview(Review) (Review, error)
GetAllReviews(whiskyid int, limit int) ([]Review, error)
GetActivity(userid int, limit int) ([]Review, error)
}
/****************************************************
* VALIDATION *
****************************************************/
var (
ValidationRequired = "cannot be empty"
ValidationNonZero = "cannot be zero"
ValidationNonNegative = "must be greater than zero"
)
func ValidateWhisky(form Whisky) (bool, map[string]string) {
errors := map[string]string{}
if form.Type == "" {
errors["type"] = ValidationRequired
}
if form.Distillery == "" {
errors["distillery"] = ValidationRequired
}
if form.Age == 0 {
errors["age"] = ValidationNonZero
} else if form.Age < 0 {
errors["age"] = ValidationNonNegative
}
if form.ABV < 0 {
errors["abv"] = ValidationNonNegative
}
return len(errors) == 0, errors
}
func ValidateReview(form Review) (bool, map[string]string) {
errors := map[string]string{}
return len(errors) == 0, errors
}