Exemple #1
0
// remove token
func RemoveToken(token string) error {
	if _, err := app.Db.Where("value = ?", token).Delete(new(Token)); err != nil {
		log.Error("Db|RemoveToken|%s", err.Error())
		return err
	}
	return nil
}
Exemple #2
0
// save new token, only insert, not update
func SaveToken(t *Token) error {
	if _, err := app.Db.Insert(t); err != nil {
		log.Error("Db|SaveToken|%s", err.Error())
		return err
	}
	return nil
}
Exemple #3
0
// get user by column and value
func GetUserBy(col string, value interface{}) (*User, error) {
	u := new(User)
	if _, err := app.Db.Where(col+" = ?", value).Get(u); err != nil {
		log.Error("Db|GetUserBy|%s", err.Error())
		return nil, err
	}
	return u, nil
}
Exemple #4
0
// update user password
func UpdateUserPassword(u *User) error {
	u.Password, u.Salt = EncodePassword(u.Password)
	// update password
	if _, err := app.Db.Cols("password,salt").Where("id = ?", u.Id).Update(u); err != nil {
		log.Error("Db|UpdateUserPassword|%s", err.Error())
		return err
	}
	return nil
}
Exemple #5
0
// check article link unique
func GetArticleByLink(link string) (*Article, error) {
	a := new(Article)
	if _, err := app.Db.Where("link = ?", link).Get(a); err != nil {
		log.Error("Db|GetArticleByLink|%s", err.Error())
		return nil, err
	}
	if a.Id == 0 {
		return nil, nil
	}
	return a, nil
}
Exemple #6
0
// get an user with name but without id
func GetUserByUniqueName(id int64, name string) (*User, error) {
	u := new(User)
	if _, err := app.Db.Where("id != ? AND name = ?", id, name).Get(u); err != nil {
		log.Error("Db|GetUserByUniqueName|%s", err.Error())
		return nil, err
	}
	if u.Id == 0 {
		return nil, nil
	}
	return u, nil
}
Exemple #7
0
// get an user with email but without id
func GetUserByUniqueEmail(id int64, email string) (*User, error) {
	u := new(User)
	if _, err := app.Db.Where("id != ? AND email = ?", id, email).Get(u); err != nil {
		log.Error("Db|GetUserByUniqueEmail|%s", err.Error())
		return nil, err
	}
	if u.Id == 0 {
		return nil, nil
	}
	return u, nil
}
Exemple #8
0
// get token and check expire time
func GetAndValidateToken(token string) (*Token, error) {
	t := new(Token)
	if _, err := app.Db.Where("value = ?", token).Get(t); err != nil {
		log.Error("Db|GetAndValidateToken|%s", err.Error())
		return nil, err
	}
	if time.Now().Unix() > t.ExpireTime {
		return nil, errors.New("expired")
	}
	return t, nil
}
Exemple #9
0
// save article
func SaveArticle(a *Article) error {
	var err error
	if a.Id > 0 {
		_, err = app.Db.Where("id = ?", a.Id).Update(a)
	} else {
		_, err = app.Db.Insert(a)
	}
	if err != nil {
		log.Error("Db|SaveArticle|%s", err.Error())
		return err
	}
	return nil
}
Exemple #10
0
// save user
func SaveUser(u *User) error {
	var err error
	if u.Id > 0 {
		_, err = app.Db.Where("id = ?", u.Id).Update(u)
	} else {
		_, err = app.Db.Insert(u)
	}
	if err != nil {
		log.Error("Db|SaveUser|%s", err.Error())
		return err
	}
	return nil
}
Exemple #11
0
// update user profile
func UpdateUserProfile(u *User) (*User, error) {
	// update
	if _, err := app.Db.Cols("name,nick,email,url,bio").Where("id = ?", u.Id).Update(u); err != nil {
		log.Error("Db|UpdateUserProfile|%s", err.Error())
		return nil, err
	}
	// return updated user data
	user, err := GetUserBy("id", u.Id)
	if err != nil {
		return nil, err
	}
	return user, nil
}