This commit is contained in:
borean 2024-03-07 20:58:50 +01:00
parent ae9ee91bc4
commit 7ed986e4eb
21 changed files with 547 additions and 93 deletions

View file

@ -14,9 +14,13 @@ import (
type Database interface {
AddUser(username string, password string) error
RemoveUser(username string) error
PromoteToAdmin(username string) error
GetUserId(username string) (int, error)
AddProject(name string, description string, username string) error
Migrate(dirname string) error
// AddTimeReport(projectname string, start time.Time, end time.Time) error
// AddUserToProject(username string, projectname string) error
// ChangeUserRole(username string, projectname string, role string) error
}
// This struct is a wrapper type that holds the database connection
@ -30,6 +34,11 @@ var scripts embed.FS
const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)"
const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?"
const promoteToAdmin = "INSERT INTO site_admin (admin_id) SELECT id FROM users WHERE username = ?"
// const addTimeReport = ""
// const addUserToProject = ""
// const changeUserRole = ""
// DbConnect connects to the database
func DbConnect(dbpath string) Database {
@ -48,6 +57,18 @@ func DbConnect(dbpath string) Database {
return &Db{db}
}
// func (d *Db) AddTimeReport(projectname string, start time.Time, end time.Time) error {
// }
// func (d *Db) AddUserToProject(username string, projectname string) error {
// }
// func (d *Db) ChangeUserRole(username string, projectname string, role string) error {
// }
// AddUser adds a user to the database
func (d *Db) AddUser(username string, password string) error {
_, err := d.Exec(userInsert, username, password)
@ -60,6 +81,11 @@ func (d *Db) RemoveUser(username string) error {
return err
}
func (d *Db) PromoteToAdmin(username string) error {
_, err := d.Exec(promoteToAdmin, username)
return err
}
func (d *Db) GetUserId(username string) (int, error) {
var id int
err := d.Get(&id, "SELECT id FROM users WHERE username = ?", username)

View file

@ -74,3 +74,32 @@ func TestDbRemoveUser(t *testing.T) {
t.Error("RemoveUser failed:", err)
}
}
func TestPromoteToAdmin(t *testing.T) {
db, err := setupState()
if err != nil {
t.Error("setupState failed:", err)
}
err = db.AddUser("test", "password")
if err != nil {
t.Error("AddUser failed:", err)
}
err = db.PromoteToAdmin("test")
if err != nil {
t.Error("PromoteToAdmin failed:", err)
}
}
// func TestAddTimeReport(t *testing.T) {
// }
// func TestAddUserToProject(t *testing.T) {
// }
// func TestChangeUserRole(t *testing.T) {
// }

View file

@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS site_admin (
admin_id INTEGER PRIMARY KEY,
FOREIGN KEY (admin_id) REFERENCES users (id) ON DELETE CASCADE
)