Skip to content

Instantly share code, notes, and snippets.

@eamonnmcevoy
Created April 15, 2017 05:54
Show Gist options
  • Save eamonnmcevoy/80697660e1055fcb97443c2762bc8bb6 to your computer and use it in GitHub Desktop.
Save eamonnmcevoy/80697660e1055fcb97443c2762bc8bb6 to your computer and use it in GitHub Desktop.
package mongo
import (
"go_web_server/pkg"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
type UserService struct {
collection *mgo.Collection
hash root.Hash
}
func NewUserService(session *Session, dbName string, collectionName string, hash root.Hash) *UserService {
collection := session.GetCollection(dbName, collectionName)
collection.EnsureIndex(userModelIndex())
return &UserService{collection, hash}
}
func (p *UserService) Create(u *root.User) error {
user := newUserModel(u)
hashedPassword, err := p.hash.Generate(user.Password)
if err != nil {
return err
}
user.Password = hashedPassword
return p.collection.Insert(&user)
}
func (p *UserService) GetByUsername(username string) (*root.User, error) {
model := userModel{}
err := p.collection.Find(bson.M{"username": username}).One(&model)
return model.toRootUser(), err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment