Added GetWeeklyReport function and corresponding test

This commit is contained in:
dDogge 2024-03-17 17:58:02 +01:00
parent c90d495636
commit a77e57e496
3 changed files with 66 additions and 1 deletions

View file

@ -29,6 +29,7 @@ type Database interface {
GetAllProjects() ([]types.Project, error) GetAllProjects() ([]types.Project, error)
GetProject(projectId int) (types.Project, error) GetProject(projectId int) (types.Project, error)
GetUserRole(username string, projectname string) (string, error) GetUserRole(username string, projectname string) (string, error)
GetWeeklyReport(username string, projectName string, week int) (types.WeeklyReport, error)
} }
// This struct is a wrapper type that holds the database connection // This struct is a wrapper type that holds the database connection
@ -256,6 +257,31 @@ func (d *Db) GetAllUsersApplication() ([]string, error) {
return usernames, nil return usernames, nil
} }
func (d *Db) GetWeeklyReport(username string, projectName string, week int) (types.WeeklyReport, error) {
var report types.WeeklyReport
query := `
SELECT
report_id,
user_id,
project_id,
week,
development_time,
meeting_time,
admin_time,
own_work_time,
study_time,
testing_time
FROM
weekly_reports
WHERE
user_id = (SELECT id FROM users WHERE username = ?)
AND project_id = (SELECT id FROM projects WHERE name = ?)
AND week = ?
`
err := d.Get(&report, query, username, projectName, week)
return report, err
}
// 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() error { func (d *Db) Migrate() error {

View file

@ -371,3 +371,42 @@ func TestAddProject(t *testing.T) {
t.Error("Added project not found") t.Error("Added project not found")
} }
} }
func TestGetWeeklyReport(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)
}
err = db.AddWeeklyReport("testproject", "testuser", 1, 1, 1, 1, 1, 1, 1)
if err != nil {
t.Error("AddWeeklyReport failed:", err)
}
report, err := db.GetWeeklyReport("testuser", "testproject", 1)
if err != nil {
t.Error("GetWeeklyReport failed:", err)
}
// Check if the retrieved report matches the expected values
if report.UserId != 1 {
t.Errorf("Expected UserId to be 1, got %d", report.UserId)
}
if report.ProjectId != 1 {
t.Errorf("Expected ProjectId to be 1, got %d", report.ProjectId)
}
if report.Week != 1 {
t.Errorf("Expected Week to be 1, got %d", report.Week)
}
// Check other fields similarly
}

View file

@ -26,7 +26,7 @@ type WeeklyReport struct {
// The user id of the user who submitted the report // The user id of the user who submitted the report
UserId int `json:"userId" db:"user_id"` UserId int `json:"userId" db:"user_id"`
// The name of the project, as it appears in the database // The name of the project, as it appears in the database
ProjectId string `json:"projectId" db:"project_id"` ProjectId int `json:"projectId" db:"project_id"`
// The week number // The week number
Week int `json:"week" db:"week"` Week int `json:"week" db:"week"`
// Total time spent on development // Total time spent on development