Exemple #1
7
func GetAllUsers() (users []*models.User, e error) {
	var rows *sql.Rows
	rows, e = _db.Query(`select id, name, account, password, created_at from users order by id`)
	if e != nil {
		log.Println("GetAllUsers", e)
		return
	}

	defer rows.Close()
	var createdAt float64
	for rows.Next() {
		u := new(models.User)
		e = rows.Scan(&u.Id, &u.Name, &u.Account, &u.Password, &createdAt)
		if e != nil {
			log.Println(e)
			return
		}

		u.CreatedAt = int64(createdAt)

		users = append(users, u)
	}

	return
}
func TestAddUser(t *testing.T) {
	u := new(models.User)
	u.Name = "tom"
	u.Account = "*****@*****.**" + strconv.FormatInt(time.Now().Unix(), 10)
	u.Password = "******"
	if AddUser(u) == nil {
		userId = u.Id
		t.Log("add user", u.Id, u.Account)
	} else {
		t.Error("failed to add user")
	}
}
Exemple #3
0
func AddUser(u *models.User) error {
	u.CreatedAt = time.Now().Unix()
	r, e := _db.Exec(`insert into users(name, account, password, created_at) values(?,?,?,?)`, u.Name, u.Account, u.Password, u.CreatedAt)
	if e != nil {
		log.Fatal(e)
		return e
	}

	u.Id, e = r.LastInsertId()
	return e
}