package web import ( "fmt" "time" "code.chrissexton.org/cws/happy/user" "github.com/rs/zerolog/log" ) func (web *Web) recordMood(mood getMoodsResponse, who user.User) error { q := `insert into moods (user_id,mood_category_id,value,time) values (?,?,?,?)` _, err := web.db.Exec(q, who.ID, mood.CategoryID, mood.Value, time.Now().Unix()) return err } func (web *Web) NewUser() (user.User, error) { uid := user.New(web.db, web.salt, web.h) q := `insert into users (verification,dateCreated) values (?, ?)` res, err := web.db.Exec(q, uid.Verification, uid.DateCreated) if err != nil { return uid, err } id, err := res.LastInsertId() if err != nil { return uid, err } uid.ID = id return uid, nil } func (web *Web) FromStr(uid, verification string) (user.User, error) { id := user.New(web.db, web.salt, web.h) if uid == "" || verification == "" { return id, fmt.Errorf("user ID and verification not given.") } idInt, err := web.h.DecodeInt64WithError(uid) if err != nil { return id, err } q := `select id,email,verification,datecreated,lastlogin from users where id=?` if err := web.db.Get(&id, q, idInt[0]); err != nil { log.Error(). Err(err). Msg("unable to select") return id, err } verify, err := web.h.EncodeInt64([]int64{id.Verification}) if err != nil { log.Error(). Err(err). Str("verify", verify). Str("id.Verification", verification). Msg("unable to encode") return id, err } if verify != verification { log.Debug(). Str("verify", verify). Str("id.Verification", verification). Msg("verification mismatch") return id, fmt.Errorf("Invalid verification token") } return id, nil }