mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-17 21:34:56 +00:00
Users bugfixes + tests (#19)
Co-authored-by: Asim Aslam <asim@aslam.me>
This commit is contained in:
@@ -19,18 +19,33 @@ type Dao struct {
|
||||
users model.Table
|
||||
sessions model.Table
|
||||
passwords model.Table
|
||||
|
||||
nameIndex model.Index
|
||||
emailIndex model.Index
|
||||
idIndex model.Index
|
||||
}
|
||||
|
||||
func New() *Dao {
|
||||
nameIndex := model.ByEquality("name")
|
||||
nameIndex := model.ByEquality("username")
|
||||
nameIndex.Unique = true
|
||||
nameIndex.Order.Type = model.OrderTypeUnordered
|
||||
|
||||
emailIndex := model.ByEquality("email")
|
||||
emailIndex.Unique = true
|
||||
emailIndex.Order.Type = model.OrderTypeUnordered
|
||||
|
||||
// @todo there should be a better way to get the default index from model
|
||||
// than recreating the options here
|
||||
idIndex := model.ByEquality("id")
|
||||
idIndex.Order.Type = model.OrderTypeUnordered
|
||||
|
||||
return &Dao{
|
||||
users: model.NewTable(store.DefaultStore, "users", model.Indexes(nameIndex, emailIndex), nil),
|
||||
sessions: model.NewTable(store.DefaultStore, "sessions", nil, nil),
|
||||
passwords: model.NewTable(store.DefaultStore, "passwords", nil, nil),
|
||||
users: model.NewTable(store.DefaultStore, "users", model.Indexes(nameIndex, emailIndex), nil),
|
||||
sessions: model.NewTable(store.DefaultStore, "sessions", nil, nil),
|
||||
passwords: model.NewTable(store.DefaultStore, "passwords", nil, nil),
|
||||
nameIndex: nameIndex,
|
||||
emailIndex: emailIndex,
|
||||
idIndex: idIndex,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,13 +62,13 @@ func (dao *Dao) CreateSession(sess *user.Session) error {
|
||||
}
|
||||
|
||||
func (dao *Dao) DeleteSession(id string) error {
|
||||
return dao.sessions.Delete(model.Equals("id", id))
|
||||
return dao.sessions.Delete(dao.idIndex.ToQuery(id))
|
||||
}
|
||||
|
||||
func (dao *Dao) ReadSession(id string) (*user.Session, error) {
|
||||
sess := &user.Session{}
|
||||
// @todo there should be a Read in the model to get rid of this pattern
|
||||
return sess, dao.sessions.Read(model.Equals("id", id), &sess)
|
||||
return sess, dao.sessions.Read(dao.idIndex.ToQuery(id), &sess)
|
||||
}
|
||||
|
||||
func (dao *Dao) Create(user *user.User, salt string, password string) error {
|
||||
@@ -71,7 +86,7 @@ func (dao *Dao) Create(user *user.User, salt string, password string) error {
|
||||
}
|
||||
|
||||
func (dao *Dao) Delete(id string) error {
|
||||
return dao.users.Delete(model.Equals("id", id))
|
||||
return dao.users.Delete(dao.idIndex.ToQuery(id))
|
||||
}
|
||||
|
||||
func (dao *Dao) Update(user *user.User) error {
|
||||
@@ -81,15 +96,15 @@ func (dao *Dao) Update(user *user.User) error {
|
||||
|
||||
func (dao *Dao) Read(id string) (*user.User, error) {
|
||||
user := &user.User{}
|
||||
return user, dao.users.Read(model.Equals("id", id), user)
|
||||
return user, dao.users.Read(dao.idIndex.ToQuery(id), user)
|
||||
}
|
||||
|
||||
func (dao *Dao) Search(username, email string, limit, offset int64) ([]*user.User, error) {
|
||||
var query model.Query
|
||||
if len(username) > 0 {
|
||||
query = model.Equals("name", username)
|
||||
query = dao.nameIndex.ToQuery(username)
|
||||
} else if len(email) > 0 {
|
||||
query = model.Equals("email", email)
|
||||
query = dao.emailIndex.ToQuery(email)
|
||||
} else {
|
||||
return nil, errors.New("username and email cannot be blank")
|
||||
}
|
||||
@@ -109,9 +124,9 @@ func (dao *Dao) UpdatePassword(id string, salt string, password string) error {
|
||||
func (dao *Dao) SaltAndPassword(username, email string) (string, string, error) {
|
||||
var query model.Query
|
||||
if len(username) > 0 {
|
||||
query = model.Equals("name", username)
|
||||
query = dao.nameIndex.ToQuery(username)
|
||||
} else if len(email) > 0 {
|
||||
query = model.Equals("email", email)
|
||||
query = dao.emailIndex.ToQuery(email)
|
||||
} else {
|
||||
return "", "", errors.New("username and email cannot be blank")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user