diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index b5e1981..a0cf5b9 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -28,6 +28,7 @@ type Database interface { GetAllUsersApplication() ([]string, error) GetProjectsForUser(username string) ([]types.Project, error) GetAllProjects() ([]types.Project, error) + GetProject(projectId int) (types.Project, error) GetUserRole(username string, projectname string) (string, error) } @@ -100,6 +101,12 @@ func (d *Db) GetAllProjects() ([]types.Project, error) { return projects, err } +func (d *Db) GetProject(projectId int) (types.Project, error) { + var project types.Project + err := d.Select(&project, "SELECT * FROM projects WHERE id = ?") + return project, err +} + func (d *Db) AddTimeReport(projectName string, userName string, start time.Time, end time.Time) error { // WIP _, err := d.Exec(addTimeReport, userName, projectName, start, end) return err diff --git a/backend/internal/database/db_test.go b/backend/internal/database/db_test.go index 7650739..117c08a 100644 --- a/backend/internal/database/db_test.go +++ b/backend/internal/database/db_test.go @@ -343,3 +343,38 @@ func TestGetProjectsForUser(t *testing.T) { t.Error("GetProjectsForUser failed: expected 1, got", len(projects)) } } + +func TestAddProject(t *testing.T) { + db, err := setupState() + if err != nil { + t.Error("setupState failed:", err) + } + + err = db.AddUser("testuser", "password") + if err != nil { + t.Error("AddUser failed:", err) + } + + err = db.AddProject("testproject", "description", "testuser") + if err != nil { + t.Error("AddProject failed:", err) + } + + // Retrieve the added project to verify its existence + projects, err := db.GetAllProjects() + if err != nil { + t.Error("GetAllProjects failed:", err) + } + + // Check if the project was added successfully + found := false + for _, project := range projects { + if project.Name == "testproject" { + found = true + break + } + } + if !found { + t.Error("Added project not found") + } +}