Ejemplo n.º 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
}
Ejemplo n.º 2
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
}