happy/user/user.go

51 lines
826 B
Go
Raw Normal View History

2019-10-29 04:52:11 +00:00
package user
import (
"database/sql"
"math/rand"
"time"
"github.com/speps/go-hashids"
"github.com/jmoiron/sqlx"
)
type User struct {
db *sqlx.DB
ID int64
Email sql.NullString
Verification int64
DateCreated sql.NullInt64 `db:"dateCreated"`
LastLogin sql.NullInt64 `db:"lastLogin"`
salt string
str string
}
func New(db *sqlx.DB, salt string, h *hashids.HashID) User {
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
}