diff --git a/frontend/src/api.js b/frontend/src/api.js index 9792a15..fad3f0c 100644 --- a/frontend/src/api.js +++ b/frontend/src/api.js @@ -5,7 +5,7 @@ export default { return axios.get("/v1/moods", { headers: { 'X-user-id': userInfo.ID, - 'X-user-validation': userInfo.Validation + 'X-user-verification': userInfo.Verification } }) }, @@ -13,7 +13,7 @@ export default { return axios.post("/v1/moods", mood, { headers: { 'X-user-id': userInfo.ID, - 'X-user-validation': userInfo.Validation + 'X-user-verification': userInfo.Verification } }) }, @@ -24,7 +24,7 @@ export default { return axios.get("/v1/user/info", { headers: { 'X-user-id': userInfo.ID, - 'X-user-validation': userInfo.Validation + 'X-user-verification': userInfo.Verification } }) } diff --git a/frontend/src/store.js b/frontend/src/store.js index 2663b09..f85f4ee 100644 --- a/frontend/src/store.js +++ b/frontend/src/store.js @@ -8,7 +8,7 @@ Vue.use(Vuex); const store = new Vuex.Store({ state: { errs: [], - userInfo: {ID: "", Validation: ""}, + userInfo: {ID: "", Verification: ""}, moods: [], }, actions: { diff --git a/mood/mood.go b/mood/mood.go new file mode 100644 index 0000000..8720335 --- /dev/null +++ b/mood/mood.go @@ -0,0 +1,29 @@ +package mood + +import ( + "time" + + "code.chrissexton.org/cws/happy/db" + "code.chrissexton.org/cws/happy/user" +) + +type Mood struct { + db *db.Database +} + +func New(db *db.Database) *Mood { + return &Mood{db} +} + +type GetMoodsResponse struct { + CategoryID int64 `db:"category_id"` + CategoryName string `db:"category_name"` + Key string + Value int64 +} + +func (m *Mood) RecordMood(mood GetMoodsResponse, who user.User) error { + q := `insert into moods (user_id,mood_category_id,value,time) values (?,?,?,?)` + _, err := m.db.Exec(q, who.ID, mood.CategoryID, mood.Value, time.Now().Unix()) + return err +} diff --git a/serve.go b/serve.go index 57af5e5..2323edf 100644 --- a/serve.go +++ b/serve.go @@ -84,9 +84,5 @@ func main() { log.Debug().Msg("mail disabled") } s := web.New(*httpAddr, "pub", db, *salt, h, box, mailClient) - if mailClient != nil { - u, _ := s.NewUser() - mailClient.SendNewUserMail("chris@chrissexton.org", u, "http://happy.chrissexton.org") - } s.Serve() } diff --git a/web/user.go b/user/user_factory.go similarity index 56% rename from web/user.go rename to user/user_factory.go index 7287bc7..1fd5f84 100644 --- a/web/user.go +++ b/user/user_factory.go @@ -1,23 +1,34 @@ -package web +package user import ( "fmt" - "time" - "code.chrissexton.org/cws/happy/user" + "github.com/speps/go-hashids" + + "code.chrissexton.org/cws/happy/db" "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 +// lol + +type UserFactory struct { + db *db.Database + salt string + h *hashids.HashID } -func (web *Web) NewUser() (user.User, error) { - uid := user.New(web.db, web.salt, web.h) +func NewFactory(db *db.Database, salt string, h *hashids.HashID) *UserFactory { + return &UserFactory{ + db: db, + salt: salt, + h: h, + } +} + +func (uf *UserFactory) NewUser() (User, error) { + uid := New(uf.db, uf.salt, uf.h) q := `insert into users (verification,dateCreated) values (?, ?)` - res, err := web.db.Exec(q, uid.Verification, uid.DateCreated) + res, err := uf.db.Exec(q, uid.Verification, uid.DateCreated) if err != nil { return uid, err } @@ -29,23 +40,23 @@ func (web *Web) NewUser() (user.User, error) { return uid, nil } -func (web *Web) FromStr(uid, verification string) (user.User, error) { - id := user.New(web.db, web.salt, web.h) +func (uf *UserFactory) FromStr(uid, verification string) (User, error) { + id := New(uf.db, uf.salt, uf.h) if uid == "" || verification == "" { return id, fmt.Errorf("user ID and verification not given.") } - idInt, err := web.h.DecodeInt64WithError(uid) + idInt, err := uf.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 { + if err := uf.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}) + verify, err := uf.h.EncodeInt64([]int64{id.Verification}) if err != nil { log.Error(). Err(err). diff --git a/web/mood_handlers.go b/web/mood_handlers.go index 911e825..d7dad5b 100644 --- a/web/mood_handlers.go +++ b/web/mood_handlers.go @@ -5,23 +5,18 @@ import ( "fmt" "net/http" + "code.chrissexton.org/cws/happy/mood" + "github.com/rs/zerolog/log" ) -type getMoodsResponse struct { - CategoryID int64 `db:"category_id"` - CategoryName string `db:"category_name"` - Key string - Value int64 -} - func (web *Web) getMoods(w http.ResponseWriter, r *http.Request) { log.Debug().Interface("req", r).Msg("getMoods") q := `select mc.id category_id,mc.name category_name,mct.key,mct.value from mood_categories mc inner join mood_category_texts mct on mc.id=mct.mood_category_id` - recs := []getMoodsResponse{} + recs := []mood.GetMoodsResponse{} err := web.db.Select(&recs, q) if err != nil { log.Error().Err(err).Msg("could not retrieve mood categories and texts") @@ -41,13 +36,13 @@ func (web *Web) getMoods(w http.ResponseWriter, r *http.Request) { func (web *Web) handleMood(w http.ResponseWriter, r *http.Request) { uid := r.Header.Get("X-user-id") - verify := r.Header.Get("X-user-validation") + verify := r.Header.Get("X-user-verification") log.Debug(). Str("uid", uid). Str("verify", verify). Msg("handleMood") dec := json.NewDecoder(r.Body) - happyReq := getMoodsResponse{} + happyReq := mood.GetMoodsResponse{} err := dec.Decode(&happyReq) if err != nil { log.Error().Err(err).Msg("error with happy") @@ -58,7 +53,7 @@ func (web *Web) handleMood(w http.ResponseWriter, r *http.Request) { log.Debug(). Interface("mood", happyReq). Msg("mood") - user, err := web.FromStr(uid, verify) + user, err := web.uf.FromStr(uid, verify) if err != nil { log.Error(). Err(err). @@ -67,7 +62,7 @@ func (web *Web) handleMood(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Error: %s", err) return } - if err := web.recordMood(happyReq, user); err != nil { + if err := web.mood.RecordMood(happyReq, user); err != nil { log.Error().Err(err).Msg("error saving mood") w.WriteHeader(500) fmt.Fprintf(w, "Error: %s", err) diff --git a/web/routes.go b/web/routes.go index 8bd23d8..112229d 100644 --- a/web/routes.go +++ b/web/routes.go @@ -3,6 +3,10 @@ package web import ( "time" + "code.chrissexton.org/cws/happy/mood" + + "code.chrissexton.org/cws/happy/user" + packr "github.com/gobuffalo/packr/v2" "code.chrissexton.org/cws/happy/email" @@ -22,6 +26,8 @@ type Web struct { h *hashids.HashID box *packr.Box email *email.EMailClient + uf *user.UserFactory + mood *mood.Mood } func New(addr, assetPath string, db *db.Database, salt string, h *hashids.HashID, box *packr.Box, email *email.EMailClient) *Web { @@ -29,16 +35,20 @@ func New(addr, assetPath string, db *db.Database, salt string, h *hashids.HashID addr: addr, assetPath: assetPath, db: db, - salt: salt, - h: h, box: box, email: email, + uf: user.NewFactory(db, salt, h), + mood: mood.New(db), } if err := db.MakeDB(); err != nil { log.Fatal(). Err(err). Msg("could not create database") } + if email != nil { + u, _ := w.uf.NewUser() + w.email.SendNewUserMail("chris@chrissexton.org", u, "http://happy.chrissexton.org") + } return w } diff --git a/web/user_handlers.go b/web/user_handlers.go index 9e9d713..d15b394 100644 --- a/web/user_handlers.go +++ b/web/user_handlers.go @@ -17,7 +17,7 @@ type RegisterResponse struct { } func (web *Web) handlerRegisterCode(w http.ResponseWriter, r *http.Request) { - uid, err := web.NewUser() + uid, err := web.uf.NewUser() if err != nil { w.WriteHeader(500) log.Error().Err(err).Msg("error from NewUserID") @@ -49,12 +49,12 @@ func (web *Web) handlerRegisterCode(w http.ResponseWriter, r *http.Request) { func (web *Web) checkUser(w http.ResponseWriter, r *http.Request) { uid := r.Header.Get("X-user-id") - verify := r.Header.Get("X-user-validation") + verify := r.Header.Get("X-user-verification") log.Debug(). Str("uid", uid). Str("verify", verify). Msg("checkUser") - user, err := web.FromStr(uid, verify) + user, err := web.uf.FromStr(uid, verify) if err != nil { log.Error().Err(err).Msg("user not known") w.WriteHeader(http.StatusUnauthorized)