2019-10-29 04:52:11 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2019-10-29 05:19:38 +00:00
|
|
|
"code.chrissexton.org/cws/happy/db"
|
2019-10-29 04:52:11 +00:00
|
|
|
|
2019-10-29 05:19:38 +00:00
|
|
|
"github.com/speps/go-hashids"
|
2019-10-29 04:52:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
2019-10-29 05:19:38 +00:00
|
|
|
db *db.Database
|
2019-10-29 04:52:11 +00:00
|
|
|
|
|
|
|
ID int64
|
|
|
|
Email sql.NullString
|
|
|
|
Verification int64
|
|
|
|
DateCreated sql.NullInt64 `db:"dateCreated"`
|
|
|
|
LastLogin sql.NullInt64 `db:"lastLogin"`
|
|
|
|
|
|
|
|
salt string
|
|
|
|
str string
|
|
|
|
}
|
|
|
|
|
2019-10-29 05:19:38 +00:00
|
|
|
func New(db *db.Database, salt string, h *hashids.HashID) User {
|
2019-10-29 04:52:11 +00:00
|
|
|
u := User{
|
|
|
|
db: db,
|
|
|
|
salt: salt,
|
|
|
|
|
|
|
|
DateCreated: sql.NullInt64{time.Now().Unix(), true},
|
|
|
|
LastLogin: sql.NullInt64{time.Now().Unix(), true},
|
|
|
|
Verification: rand.Int63(),
|
|
|
|
}
|
|
|
|
u.UpdateID(h)
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) UpdateID(h *hashids.HashID) error {
|
|
|
|
idstr, err := h.EncodeInt64([]int64{u.ID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.str = idstr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u User) String() string {
|
|
|
|
return u.str
|
|
|
|
}
|