Created
April 15, 2017 05:54
-
-
Save eamonnmcevoy/80697660e1055fcb97443c2762bc8bb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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