func getToken(db *pg.Session, key string) (*Model, error) { resetToken, err := db.FindOne(&Model{}, "key = $1", key) if err != nil { return nil, err } return resetToken.(*Model), nil }
func GetByEmail(db *pg.Session, email string) (*Model, error) { user, err := db.FindOne(&Model{}, "email = $1", email) if err != nil { return nil, err } return user.(*Model), nil }
func updateToken(db *pg.Session, token *Model) error { err := db.Update(token) if err != nil { return fmt.Errorf("Could not persist token: %s", err) } return nil }
func GetById(db *pg.Session, id int64) (*Model, error) { user, err := db.FindOne(&Model{}, "id = $1", id) if err != nil { return nil, err } return user.(*Model), nil }
func Update(db *pg.Session, user *Model) error { err := db.Update(user) if err != nil { return fmt.Errorf("Could not persist user: %s", err) } return nil }
func Create(db *pg.Session, email, pw string, userJsonData *UserJsonData) (*Model, error) { // encode password var saltedPassword password.Salted err := saltedPassword.Encode(pw) if err != nil { return nil, errors.New("Could not encode password") } // create new user structure user := &Model{ Id: null.NewInt(0, false), State: UserStateActive, Email: email, Password: saltedPassword, } // fill user structure with additional data err = user.JsonData.Encode(userJsonData) if err != nil { return nil, fmt.Errorf("Failed to marshal json data: %s", err) } // create new user in database err = db.Create(user) if err != nil { return nil, fmt.Errorf("Could not persist user: %s", err) } return user, nil }
func GetById(db *pg.Session, id int) (*Model, error) { entity, err := db.FindOne(&Model{}, "id=$1", id) if err != nil { return nil, err } return entity.(*Model), nil }
func createToken(db *pg.Session, userId int64) (*Model, error) { // generate key key, err := random.New(32) if err != nil { return nil, err } // create new reset token structure resetToken := &Model{ Id: null.NewInt(0, false), State: ResetTokenActive, Key: key, Expiration: time.Now().Add(time.Minute * 10), UserId: userId, } // create new user in database err = db.Create(resetToken) if err != nil { return nil, fmt.Errorf("Could not persist reset token: %s", err) } return resetToken, nil }