go: more refactoring
* rename validation => verification * still some disconnects between the JS and abckend
This commit is contained in:
parent
ed411d5a5c
commit
2912909957
|
@ -5,7 +5,7 @@ export default {
|
||||||
return axios.get("/v1/moods", {
|
return axios.get("/v1/moods", {
|
||||||
headers: {
|
headers: {
|
||||||
'X-user-id': userInfo.ID,
|
'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, {
|
return axios.post("/v1/moods", mood, {
|
||||||
headers: {
|
headers: {
|
||||||
'X-user-id': userInfo.ID,
|
'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", {
|
return axios.get("/v1/user/info", {
|
||||||
headers: {
|
headers: {
|
||||||
'X-user-id': userInfo.ID,
|
'X-user-id': userInfo.ID,
|
||||||
'X-user-validation': userInfo.Validation
|
'X-user-verification': userInfo.Verification
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ Vue.use(Vuex);
|
||||||
const store = new Vuex.Store({
|
const store = new Vuex.Store({
|
||||||
state: {
|
state: {
|
||||||
errs: [],
|
errs: [],
|
||||||
userInfo: {ID: "", Validation: ""},
|
userInfo: {ID: "", Verification: ""},
|
||||||
moods: [],
|
moods: [],
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
4
serve.go
4
serve.go
|
@ -84,9 +84,5 @@ func main() {
|
||||||
log.Debug().Msg("mail disabled")
|
log.Debug().Msg("mail disabled")
|
||||||
}
|
}
|
||||||
s := web.New(*httpAddr, "pub", db, *salt, h, box, mailClient)
|
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()
|
s.Serve()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,34 @@
|
||||||
package web
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"code.chrissexton.org/cws/happy/user"
|
"github.com/speps/go-hashids"
|
||||||
|
|
||||||
|
"code.chrissexton.org/cws/happy/db"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (web *Web) recordMood(mood getMoodsResponse, who user.User) error {
|
// lol
|
||||||
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())
|
type UserFactory struct {
|
||||||
return err
|
db *db.Database
|
||||||
|
salt string
|
||||||
|
h *hashids.HashID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (web *Web) NewUser() (user.User, error) {
|
func NewFactory(db *db.Database, salt string, h *hashids.HashID) *UserFactory {
|
||||||
uid := user.New(web.db, web.salt, web.h)
|
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 (?, ?)`
|
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 {
|
if err != nil {
|
||||||
return uid, err
|
return uid, err
|
||||||
}
|
}
|
||||||
|
@ -29,23 +40,23 @@ func (web *Web) NewUser() (user.User, error) {
|
||||||
return uid, nil
|
return uid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (web *Web) FromStr(uid, verification string) (user.User, error) {
|
func (uf *UserFactory) FromStr(uid, verification string) (User, error) {
|
||||||
id := user.New(web.db, web.salt, web.h)
|
id := New(uf.db, uf.salt, uf.h)
|
||||||
if uid == "" || verification == "" {
|
if uid == "" || verification == "" {
|
||||||
return id, fmt.Errorf("user ID and verification not given.")
|
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 {
|
if err != nil {
|
||||||
return id, err
|
return id, err
|
||||||
}
|
}
|
||||||
q := `select id,email,verification,datecreated,lastlogin from users where id=?`
|
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().
|
log.Error().
|
||||||
Err(err).
|
Err(err).
|
||||||
Msg("unable to select")
|
Msg("unable to select")
|
||||||
return id, err
|
return id, err
|
||||||
}
|
}
|
||||||
verify, err := web.h.EncodeInt64([]int64{id.Verification})
|
verify, err := uf.h.EncodeInt64([]int64{id.Verification})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().
|
log.Error().
|
||||||
Err(err).
|
Err(err).
|
|
@ -5,23 +5,18 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"code.chrissexton.org/cws/happy/mood"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"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) {
|
func (web *Web) getMoods(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Debug().Interface("req", r).Msg("getMoods")
|
log.Debug().Interface("req", r).Msg("getMoods")
|
||||||
q := `select mc.id category_id,mc.name category_name,mct.key,mct.value
|
q := `select mc.id category_id,mc.name category_name,mct.key,mct.value
|
||||||
from mood_categories mc
|
from mood_categories mc
|
||||||
inner join mood_category_texts mct
|
inner join mood_category_texts mct
|
||||||
on mc.id=mct.mood_category_id`
|
on mc.id=mct.mood_category_id`
|
||||||
recs := []getMoodsResponse{}
|
recs := []mood.GetMoodsResponse{}
|
||||||
err := web.db.Select(&recs, q)
|
err := web.db.Select(&recs, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("could not retrieve mood categories and texts")
|
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) {
|
func (web *Web) handleMood(w http.ResponseWriter, r *http.Request) {
|
||||||
uid := r.Header.Get("X-user-id")
|
uid := r.Header.Get("X-user-id")
|
||||||
verify := r.Header.Get("X-user-validation")
|
verify := r.Header.Get("X-user-verification")
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Str("uid", uid).
|
Str("uid", uid).
|
||||||
Str("verify", verify).
|
Str("verify", verify).
|
||||||
Msg("handleMood")
|
Msg("handleMood")
|
||||||
dec := json.NewDecoder(r.Body)
|
dec := json.NewDecoder(r.Body)
|
||||||
happyReq := getMoodsResponse{}
|
happyReq := mood.GetMoodsResponse{}
|
||||||
err := dec.Decode(&happyReq)
|
err := dec.Decode(&happyReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("error with happy")
|
log.Error().Err(err).Msg("error with happy")
|
||||||
|
@ -58,7 +53,7 @@ func (web *Web) handleMood(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Interface("mood", happyReq).
|
Interface("mood", happyReq).
|
||||||
Msg("mood")
|
Msg("mood")
|
||||||
user, err := web.FromStr(uid, verify)
|
user, err := web.uf.FromStr(uid, verify)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().
|
log.Error().
|
||||||
Err(err).
|
Err(err).
|
||||||
|
@ -67,7 +62,7 @@ func (web *Web) handleMood(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, "Error: %s", err)
|
fmt.Fprintf(w, "Error: %s", err)
|
||||||
return
|
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")
|
log.Error().Err(err).Msg("error saving mood")
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
fmt.Fprintf(w, "Error: %s", err)
|
fmt.Fprintf(w, "Error: %s", err)
|
||||||
|
|
|
@ -3,6 +3,10 @@ package web
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"code.chrissexton.org/cws/happy/mood"
|
||||||
|
|
||||||
|
"code.chrissexton.org/cws/happy/user"
|
||||||
|
|
||||||
packr "github.com/gobuffalo/packr/v2"
|
packr "github.com/gobuffalo/packr/v2"
|
||||||
|
|
||||||
"code.chrissexton.org/cws/happy/email"
|
"code.chrissexton.org/cws/happy/email"
|
||||||
|
@ -22,6 +26,8 @@ type Web struct {
|
||||||
h *hashids.HashID
|
h *hashids.HashID
|
||||||
box *packr.Box
|
box *packr.Box
|
||||||
email *email.EMailClient
|
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 {
|
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,
|
addr: addr,
|
||||||
assetPath: assetPath,
|
assetPath: assetPath,
|
||||||
db: db,
|
db: db,
|
||||||
salt: salt,
|
|
||||||
h: h,
|
|
||||||
box: box,
|
box: box,
|
||||||
email: email,
|
email: email,
|
||||||
|
uf: user.NewFactory(db, salt, h),
|
||||||
|
mood: mood.New(db),
|
||||||
}
|
}
|
||||||
if err := db.MakeDB(); err != nil {
|
if err := db.MakeDB(); err != nil {
|
||||||
log.Fatal().
|
log.Fatal().
|
||||||
Err(err).
|
Err(err).
|
||||||
Msg("could not create database")
|
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
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ type RegisterResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (web *Web) handlerRegisterCode(w http.ResponseWriter, r *http.Request) {
|
func (web *Web) handlerRegisterCode(w http.ResponseWriter, r *http.Request) {
|
||||||
uid, err := web.NewUser()
|
uid, err := web.uf.NewUser()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
log.Error().Err(err).Msg("error from NewUserID")
|
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) {
|
func (web *Web) checkUser(w http.ResponseWriter, r *http.Request) {
|
||||||
uid := r.Header.Get("X-user-id")
|
uid := r.Header.Get("X-user-id")
|
||||||
verify := r.Header.Get("X-user-validation")
|
verify := r.Header.Get("X-user-verification")
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Str("uid", uid).
|
Str("uid", uid).
|
||||||
Str("verify", verify).
|
Str("verify", verify).
|
||||||
Msg("checkUser")
|
Msg("checkUser")
|
||||||
user, err := web.FromStr(uid, verify)
|
user, err := web.uf.FromStr(uid, verify)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("user not known")
|
log.Error().Err(err).Msg("user not known")
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
|
Loading…
Reference in New Issue