Added GetAllUsersProject and corresponding test
This commit is contained in:
parent
555a3fa7ec
commit
0e1ea15cc9
2 changed files with 105 additions and 0 deletions
|
@ -23,6 +23,7 @@ type Database interface {
|
||||||
AddTimeReport(projectName string, userName string, start time.Time, end time.Time) error
|
AddTimeReport(projectName string, userName string, start time.Time, end time.Time) error
|
||||||
AddUserToProject(username string, projectname string, role string) error
|
AddUserToProject(username string, projectname string, role string) error
|
||||||
ChangeUserRole(username string, projectname string, role string) error
|
ChangeUserRole(username string, projectname string, role string) error
|
||||||
|
GetAllUsersProject(projectname string) ([]UserProjectMember, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This struct is a wrapper type that holds the database connection
|
// This struct is a wrapper type that holds the database connection
|
||||||
|
@ -31,6 +32,11 @@ type Db struct {
|
||||||
*sqlx.DB
|
*sqlx.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserProjectMember struct {
|
||||||
|
Username string `db:"username"`
|
||||||
|
UserRole string `db:"p_role"`
|
||||||
|
}
|
||||||
|
|
||||||
//go:embed migrations
|
//go:embed migrations
|
||||||
var scripts embed.FS
|
var scripts embed.FS
|
||||||
|
|
||||||
|
@ -136,6 +142,39 @@ func (d *Db) AddProject(name string, description string, username string) error
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// Reads a directory of migration files and applies them to the database.
|
// Reads a directory of migration files and applies them to the database.
|
||||||
// This will eventually be used on an embedded directory
|
// This will eventually be used on an embedded directory
|
||||||
func (d *Db) Migrate(dirname string) error {
|
func (d *Db) Migrate(dirname string) error {
|
||||||
|
|
|
@ -174,3 +174,69 @@ func TestChangeUserRole(t *testing.T) {
|
||||||
t.Error("ChangeUserRole failed:", err)
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue