+func (d *Database) GetUser(username string) (User, error) {
+ var user User
+ err := d.sqldb.QueryRow("SELECT id, username, password_hash, is_admin FROM users WHERE username = ?", username).Scan(&user.Id, &user.Username, &user.PasswordHash, &user.IsAdmin)
+ if err != nil {
+ return User{}, errors.New("no user with that username")
+ }
+ return user, nil
+}
+
+func (d *Database) GetUserById(id int) (User, error) {
+ var user User
+ err := d.sqldb.QueryRow("SELECT id, username, password_hash, is_admin FROM users WHERE id = ?", id).Scan(&user.Id, &user.Username, &user.PasswordHash, &user.IsAdmin)
+ if err != nil {
+ return User{}, errors.New("no user with that id")
+ }
+ return user, nil
+}
+
+func (d *Database) GetUsers() []User {
+ ret := make([]User, 0)
+ rows, err := d.sqldb.Query("SELECT id, username, password_hash, is_admin FROM users ORDER BY username ASC")
+ if err != nil {
+ return ret
+ }
+ defer rows.Close()
+ for rows.Next() {
+ var u User
+ if err := rows.Scan(&u.Id, &u.Username, &u.PasswordHash, &u.IsAdmin); err != nil {
+ return ret
+ }
+ ret = append(ret, u)
+ }
+ return ret
+}
+
+func (d *Database) SetUserPassword(username string, passwordHash string) {
+ d.sqldb.Exec("UPDATE users SET password_hash = ? WHERE username = ?", passwordHash, username)
+}
+
+func (d *Database) ClearOtherSessions(username string, token string) {
+ d.sqldb.Exec("DELETE FROM sessions WHERE username = ? AND token != ?", username, token)
+}
+
+func (d *Database) SetUserIsAdmin(username string, isAdmin bool) {
+ d.sqldb.Exec("UPDATE users SET is_admin = ? WHERE username = ?", isAdmin, username)
+}
+
+func (d *Database) CreateUser(user User) error {
+ _, err := d.sqldb.Exec("INSERT INTO users (username, password_hash, is_admin) values (?, ?, ?)", user.Username, user.PasswordHash, user.IsAdmin)
+ return err
+}
+
+func (d *Database) DeleteUser(username string) error {
+ _, err := d.sqldb.Exec("DELETE FROM users WHERE username = ?", username)
+ return err
+}
+