X-Git-Url: https://code.octet-stream.net/broadcaster/blobdiff_plain/7b615b3c71825b5b229b78509a16db37e1d3f38d..92da3eadf29c9e90cd67fd15dba3b8487ce4a918:/server/database.go?ds=inline diff --git a/server/database.go b/server/database.go index 1312208..c41b1ad 100644 --- a/server/database.go +++ b/server/database.go @@ -42,6 +42,8 @@ func InitDatabase() { CREATE TABLE IF NOT EXISTS playlist_entries (id INTEGER PRIMARY KEY AUTOINCREMENT, playlist_id INTEGER, position INTEGER, filename TEXT, delay_seconds INTEGER, is_relative INTEGER, CONSTRAINT fk_playlists FOREIGN KEY (playlist_id) REFERENCES playlists(id) ON DELETE CASCADE); CREATE TABLE IF NOT EXISTS radios (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, token TEXT); CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password_hash TEXT, is_admin INTEGER); + + DELETE FROM sessions WHERE expiry < CURRENT_TIMESTAMP; ` _, err = db.sqldb.Exec(sqlStmt) if err != nil { @@ -79,6 +81,15 @@ func (d *Database) GetUser(username string) (User, error) { 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") @@ -104,6 +115,10 @@ func (d *Database) ClearOtherSessions(username string, token string) { d.sqldb.Exec("DELETE FROM sessions WHERE username = ? AND token != ?", username, token) } +func (d *Database) ClearSession(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) } @@ -142,7 +157,7 @@ func (d *Database) DeletePlaylist(playlistId int) { func (d *Database) GetPlaylists() []Playlist { ret := make([]Playlist, 0) - rows, err := d.sqldb.Query("SELECT id, enabled, name, start_time FROM playlists ORDER BY id ASC") + rows, err := d.sqldb.Query("SELECT id, enabled, name, start_time FROM playlists ORDER BY start_time DESC") if err != nil { return ret }