本文整理匯總了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
}