本文整理汇总了Golang中github.com/gotk/pg.Session类的典型用法代码示例。如果您正苦于以下问题:Golang Session类的具体用法?Golang Session怎么用?Golang Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getToken
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
}
示例2: GetByEmail
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
}
示例3: updateToken
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
}
示例4: GetById
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
}
示例5: Update
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
}
示例6: Create
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
}
示例7: GetById
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
}
示例8: createToken
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
}