diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index f05530a..5221e4d 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -23,8 +23,6 @@ type Database interface { AddTimeReport(projectName string, userName string, start time.Time, end time.Time) error AddUserToProject(username string, projectname string, role string) error ChangeUserRole(username string, projectname string, role string) error - GetAllUsersProject(projectname string) ([]UserProjectMember, error) - GetAllUsersApplication() ([]string, error) } // This struct is a wrapper type that holds the database connection @@ -33,11 +31,6 @@ type Db struct { *sqlx.DB } -type UserProjectMember struct { - Username string `db:"username"` - UserRole string `db:"p_role"` -} - //go:embed migrations var scripts embed.FS @@ -143,69 +136,6 @@ func (d *Db) AddProject(name string, description string, username string) error return err } -func (d *Db) GetAllUsersProject(projectname string) ([]UserProjectMember, error) { - // Define the SQL query to fetch users and their roles for a given project - query := ` - SELECT u.username, ur.p_role - FROM users u - INNER JOIN user_roles ur ON u.id = ur.user_id - INNER JOIN projects p ON ur.project_id = p.id - WHERE p.name = ? - ` - - // Execute the query - rows, err := d.Queryx(query, projectname) - if err != nil { - return nil, err - } - defer rows.Close() - - // Iterate over the rows and populate the result slice - var users []UserProjectMember - for rows.Next() { - var user UserProjectMember - if err := rows.StructScan(&user); err != nil { - return nil, err - } - users = append(users, user) - } - if err := rows.Err(); err != nil { - return nil, err - } - - return users, nil -} - -// GetAllUsersApplication retrieves all usernames from the database -func (d *Db) GetAllUsersApplication() ([]string, error) { - // Define the SQL query to fetch all usernames - query := ` - SELECT username FROM users - ` - - // Execute the query - rows, err := d.Queryx(query) - if err != nil { - return nil, err - } - defer rows.Close() - - // Iterate over the rows and populate the result slice - var usernames []string - for rows.Next() { - var username string - if err := rows.Scan(&username); err != nil { - return nil, err - } - usernames = append(usernames, username) - } - if err := rows.Err(); err != nil { - return nil, err - } - - return usernames, nil -} - // Reads a directory of migration files and applies them to the database. // This will eventually be used on an embedded directory func (d *Db) Migrate(dirname string) error { diff --git a/backend/internal/database/db_test.go b/backend/internal/database/db_test.go index e5aceb2..b5fe49f 100644 --- a/backend/internal/database/db_test.go +++ b/backend/internal/database/db_test.go @@ -174,115 +174,3 @@ func TestChangeUserRole(t *testing.T) { t.Error("ChangeUserRole failed:", err) } } - -func TestGetAllUsersProject(t *testing.T) { - db, err := setupState() - if err != nil { - t.Error("setupState failed:", err) - } - - err = db.AddUser("testuser1", "password") - if err != nil { - t.Error("AddUser failed:", err) - } - - err = db.AddUser("testuser2", "password") - if err != nil { - t.Error("AddUser failed:", err) - } - - err = db.AddProject("testproject", "description", "testuser1") - if err != nil { - t.Error("AddProject failed:", err) - } - - err = db.AddUserToProject("testuser1", "testproject", "project_manager") - if err != nil { - t.Error("AddUserToProject failed:", err) - } - - err = db.AddUserToProject("testuser2", "testproject", "user") - if err != nil { - t.Error("AddUserToProject failed:", err) - } - - users, err := db.GetAllUsersProject("testproject") - if err != nil { - t.Error("GetAllUsersProject failed:", err) - } - - // Check if both users are returned with their roles - if len(users) != 2 { - t.Errorf("Expected 2 users, got %d", len(users)) - } - - // Check if testuser1 has project manager role - foundProjectManager := false - for _, user := range users { - if user.Username == "testuser1" && user.UserRole == "project_manager" { - foundProjectManager = true - break - } - } - if !foundProjectManager { - t.Error("Project Manager user not found") - } - - // Check if testuser2 has user role - foundUser := false - for _, user := range users { - if user.Username == "testuser2" && user.UserRole == "user" { - foundUser = true - break - } - } - if !foundUser { - t.Error("User user not found") - } -} - -func TestGetAllUsersApplication(t *testing.T) { - db, err := setupState() - if err != nil { - t.Error("setupState failed:", err) - } - - err = db.AddUser("testuser1", "password") - if err != nil { - t.Error("AddUser failed:", err) - } - - err = db.AddUser("testuser2", "password") - if err != nil { - t.Error("AddUser failed:", err) - } - - users, err := db.GetAllUsersApplication() - if err != nil { - t.Error("GetAllUsersApplication failed:", err) - } - - // Check if both users are returned - if len(users) != 2 { - t.Errorf("Expected 2 users, got %d", len(users)) - } - - // Check if the test users are included in the list - foundTestUser1 := false - foundTestUser2 := false - for _, user := range users { - if user == "testuser1" { - foundTestUser1 = true - } - if user == "testuser2" { - foundTestUser2 = true - } - } - - if !foundTestUser1 { - t.Error("testuser1 not found") - } - if !foundTestUser2 { - t.Error("testuser2 not found") - } -}