/
user.go
47 lines (41 loc) · 1.21 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
package main
import(
"fmt"
"github.com/gomodule/redigo/redis"
)
type User struct {
FirstName string `form:"firstname"`
LastName string `form:"lastname"`
Email string `form:"email"`
Password string `form:"pass"`
}
// IsValid returns a bool that indicates whether or not all the values that
// have been unmarshelled into the struct are valid.
func (u User) IsValid() bool {
if u.FirstName == "" {
return false
} else if u.LastName == "" {
return false
} else if u.Email == "" {
return false
} else if u.Password == "" {
return false
} else {
return true
}
}
// Save takes a User struct and puts it into the database. This will create
// a new entry to update an existing entry.
func (u User) Save() {
store.Do("HSET", fmt.Sprintf("user:%s", u.Email), "firstname", u.FirstName)
store.Do("HSET", fmt.Sprintf("user:%s", u.Email), "lastname", u.LastName)
store.Do("HSET", fmt.Sprintf("user:%s", u.Email), "password", u.Password)
}
// DoesExist returns true if the user exists in the database. Else false.
func (u User) DoesExist() (exists bool) {
exists, err := redis.Bool(store.Do("EXISTS", fmt.Sprintf("user:%s", u.Email)))
if err != nil {
panic(err)
}
return
}