// CreateUser converts an incoming JSON body into a User struct
func registerUser(usr *gardenswap.User) error {

	err := usr.SetPassword(usr.Password)
	if err != nil {
		return err
	}

	err = usr.RotateJWT()
	if err != nil {
		return err
	}

	query, _, err := sq.Insert(UserType).
		Columns("id", "name", "email", "jwt", "password").
		Values(usr.ID, usr.Name, usr.Email, usr.JWT, usr.PasswordHash).
		ToSql()

	if err != nil {
		return fmt.Errorf("Error creating SQL: %s", err.Error())
	}

	_, err = db.Get().Exec(query)
	if err != nil {
		return fmt.Errorf("Error inserting new user: %s", err.Error())
	}

	return nil
}
示例#2
0
// ListUsers returns a list of all users
func ListUsers() ([]*User, error) {

	users := []*User{}
	err := db.Get().Select(&users, "SELECT * FROM users")
	if err != nil {
		return nil, fmt.Errorf("Error retrieving users: %s", err.Error())
	}

	return users, nil
}
示例#3
0
// FetchUser attempts to find a single user based on ID
func FetchUser(id string) (*User, error) {
	user := &User{}
	err := db.Get().Get(&user, "SELECT * FROM users WHERE id=$1", id)
	if err != nil {
		return nil, fmt.Errorf(
			"Error attempting to fetch user by id: %s", err.Error(),
		)
	}

	return user, nil
}