-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
48 lines (45 loc) · 1.29 KB
/
user.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
package db
import "github.com/rs/zerolog/log"
// User is a type that represents a user of the app.
type User struct {
Email string `json:"email"`
Name string `json:"name"`
Community string `json:"community"`
Password []byte `json:"-"`
Tokens uint64 `json:"tokens"`
Active bool `json:"active"`
Rating int32 `json:"rating"`
AvatarHash HexBytes `json:"avatarHash" genji:"avatarHash"` // hash of the image
Location Location `json:"location"`
Verified bool `json:"-"`
}
func createUserTables(db *Database) error {
log.Info().Msg("creating user tables")
if err := db.Exec(`
CREATE TABLE IF NOT EXISTS user (
email TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
community TEXT,
password BLOB NOT NULL,
tokens INTEGER DEFAULT 1000,
active BOOLEAN DEFAULT TRUE,
rating INTEGER DEFAULT 50,
avatarHash BLOB,
verified BOOLEAN DEFAULT FALSE,
location (
latitude INTEGER NOT NULL,
longitude INTEGER NOT NULL
),
CONSTRAINT rating_check CHECK(rating >= 0 AND rating <= 100),
CONSTRAINT length_check CHECK(
len(name) > 2 AND
len(name) < 30 AND
len(email) > 8 AND
len(email) < 30)
)`); err != nil {
return err
}
return db.Exec(`
CREATE INDEX IF NOT EXISTS user_name ON user (name);
`)
}