Advertisement
Guest User

Untitled

a guest
Apr 15th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. package mongo
  2.  
  3. import (
  4. "go_web_server/pkg"
  5.  
  6. "gopkg.in/mgo.v2"
  7. "gopkg.in/mgo.v2/bson"
  8. )
  9.  
  10. type UserService struct {
  11. collection *mgo.Collection
  12. hash root.Hash
  13. }
  14.  
  15. func NewUserService(session *Session, dbName string, collectionName string, hash root.Hash) *UserService {
  16. collection := session.GetCollection(dbName, collectionName)
  17. collection.EnsureIndex(userModelIndex())
  18. return &UserService{collection, hash}
  19. }
  20.  
  21. func (p *UserService) Create(u *root.User) error {
  22. user := newUserModel(u)
  23. hashedPassword, err := p.hash.Generate(user.Password)
  24. if err != nil {
  25. return err
  26. }
  27. user.Password = hashedPassword
  28. return p.collection.Insert(&user)
  29. }
  30.  
  31. func (p *UserService) GetByUsername(username string) (*root.User, error) {
  32. model := userModel{}
  33. err := p.collection.Find(bson.M{"username": username}).One(&model)
  34. return model.toRootUser(), err
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement