From b13a8b54323db49cb72f7f248f4b867b7d993264 Mon Sep 17 00:00:00 2001 From: dDogge Date: Wed, 20 Mar 2024 00:35:37 +0100 Subject: [PATCH 01/14] Merge --- .../handlers/handlers_project_related.go | 25 +++++++++++++++++ backend/main.go | 2 ++ testing.py | 28 +++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/backend/internal/handlers/handlers_project_related.go b/backend/internal/handlers/handlers_project_related.go index 2ab5869..a92616d 100644 --- a/backend/internal/handlers/handlers_project_related.go +++ b/backend/internal/handlers/handlers_project_related.go @@ -118,6 +118,31 @@ func (gs *GState) ListAllUsersProject(c *fiber.Ctx) error { return c.Status(400).SendString("No project name provided") } + // Get the user token + userToken := c.Locals("user").(*jwt.Token) + claims := userToken.Claims.(jwt.MapClaims) + username := claims["name"].(string) + + // Check if the user is a project manager for the specified project + isManager, err := gs.Db.IsProjectManager(username, projectName) + if err != nil { + log.Info("Error checking project manager status:", err) + return c.Status(500).SendString(err.Error()) + } + + // If the user is not a project manager, check if the user is a site admin + if !isManager { + isAdmin, err := gs.Db.IsSiteAdmin(username) + if err != nil { + log.Info("Error checking admin status:", err) + return c.Status(500).SendString(err.Error()) + } + if !isAdmin { + log.Info("User is neither a project manager nor a site admin:", username) + return c.Status(403).SendString("User is neither a project manager nor a site admin") + } + } + // Get all users associated with the project from the database users, err := gs.Db.GetAllUsersProject(projectName) if err != nil { diff --git a/backend/main.go b/backend/main.go index 60027fd..b907177 100644 --- a/backend/main.go +++ b/backend/main.go @@ -97,6 +97,8 @@ func main() { server.Get("/api/getWeeklyReportsUser", gs.GetWeeklyReportsUserHandler) server.Get("api/checkIfProjectManager", gs.IsProjectManagerHandler) server.Post("/api/ProjectRoleChange", gs.ProjectRoleChange) + server.Get("/api/getUsersProject/:projectName", gs.ListAllUsersProject) + // Announce the port we are listening on and start the server err = server.Listen(fmt.Sprintf(":%d", conf.Port)) if err != nil { diff --git a/testing.py b/testing.py index 0b803f5..491419f 100644 --- a/testing.py +++ b/testing.py @@ -40,6 +40,7 @@ getUserProjectsPath = base_url + "/api/getUserProjects" getWeeklyReportsUserPath = base_url + "/api/getWeeklyReportsUser" checkIfProjectManagerPath = base_url + "/api/checkIfProjectManager" ProjectRoleChangePath = base_url + "/api/ProjectRoleChange" +getUsersProjectPath = base_url + "/api/getUsersProject" #ta bort auth i handlern för att få testet att gå igenom def test_ProjectRoleChange(): @@ -338,7 +339,33 @@ def test_check_if_project_manager(): assert response.status_code == 200, "Check if project manager failed" gprint("test_check_if_project_manager successful") +def test_list_all_users_project(): + # Log in as a user who is a member of the project + admin_username = randomString() + admin_password = "admin_password2" + dprint( + "Registering with username: ", admin_username, " and password: ", admin_password + ) + response = requests.post( + registerPath, json={"username": admin_username, "password": admin_password} + ) + dprint(response.text) + # Log in as the admin + admin_token = login(admin_username, admin_password).json()["token"] + response = requests.post( + promoteToAdminPath, + json={"username": admin_username}, + headers={"Authorization": "Bearer " + admin_token}, + ) + + # Make a request to list all users associated with the project + response = requests.get( + getUsersProjectPath + "/" + projectName, + headers={"Authorization": "Bearer " + admin_token}, + ) + assert response.status_code == 200, "List all users project failed" + gprint("test_list_all_users_project sucessful") if __name__ == "__main__": @@ -354,3 +381,4 @@ if __name__ == "__main__": test_get_weekly_reports_user() test_check_if_project_manager() test_ProjectRoleChange() + test_list_all_users_project() From a468b896db0018d70e6b350b76a4f0cdcd1f9143 Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 10:41:16 +0100 Subject: [PATCH 02/14] WIP for funcs that need to be implemented --- backend/internal/handlers/global_state.go | 27 +++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index 5d01368..1492a09 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -22,20 +22,19 @@ type GlobalState interface { PromoteToAdmin(c *fiber.Ctx) error GetWeeklyReportsUserHandler(c *fiber.Ctx) error IsProjectManagerHandler(c *fiber.Ctx) error - // GetProject(c *fiber.Ctx) error // To get a specific project - // UpdateProject(c *fiber.Ctx) error // To update a project - // DeleteProject(c *fiber.Ctx) error // To delete a project - // CreateTask(c *fiber.Ctx) error // To create a new task - // GetTasks(c *fiber.Ctx) error // To get all tasks - // GetTask(c *fiber.Ctx) error // To get a specific task - // UpdateTask(c *fiber.Ctx) error // To update a task - // DeleteTask(c *fiber.Ctx) error // To delete a task - // CreateCollection(c *fiber.Ctx) error // To create a new collection - // GetCollections(c *fiber.Ctx) error // To get all collections - // GetCollection(c *fiber.Ctx) error // To get a specific collection - // UpdateCollection(c *fiber.Ctx) error // To update a collection - // DeleteCollection(c *fiber.Ctx) error // To delete a collection - // SignCollection(c *fiber.Ctx) error // To sign a collection + // UpdateProject(c *fiber.Ctx) error // To update a project // WIP + // DeleteProject(c *fiber.Ctx) error // To delete a project // WIP + // CreateTask(c *fiber.Ctx) error // To create a new task // WIP + // GetTasks(c *fiber.Ctx) error // To get all tasks // WIP + // GetTask(c *fiber.Ctx) error // To get a specific task // WIP + // UpdateTask(c *fiber.Ctx) error // To update a task // WIP + // DeleteTask(c *fiber.Ctx) error // To delete a task // WIP + // CreateCollection(c *fiber.Ctx) error // To create a new collection // WIP + // GetCollections(c *fiber.Ctx) error // To get all collections // WIP + // GetCollection(c *fiber.Ctx) error // To get a specific collection // WIP + // UpdateCollection(c *fiber.Ctx) error // To update a collection // WIP + // DeleteCollection(c *fiber.Ctx) error // To delete a collection // WIP + // SignCollection(c *fiber.Ctx) error // To sign a collection // WIP ListAllUsers(c *fiber.Ctx) error // To get a list of all users in the application database ListAllUsersProject(c *fiber.Ctx) error // To get a list of all users for a specific project ProjectRoleChange(c *fiber.Ctx) error // To change a users role in a project From cd74758b2f647c291e86b96be1f8c6c3e345c0c1 Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 11:09:48 +0100 Subject: [PATCH 03/14] DeleteProject added in db.go, untested --- backend/internal/database/db.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index 842ffbd..92cbc71 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -3,6 +3,7 @@ package database import ( "embed" "errors" + "fmt" "path/filepath" "ttime/internal/types" @@ -19,6 +20,7 @@ type Database interface { PromoteToAdmin(username string) error GetUserId(username string) (int, error) AddProject(name string, description string, username string) error + // DeleteProject(name string, username string) error Migrate() error MigrateSampleData() error GetProjectId(projectname string) (int, error) @@ -70,6 +72,8 @@ const getProjectsForUser = `SELECT p.id, p.name, p.description FROM projects p JOIN user_roles ur ON p.id = ur.project_id JOIN users u ON ur.user_id = u.id WHERE u.username = ?` +const deleteProject = `DELETE FROM projects + WHERE id = ? AND owner_username = ?` // DbConnect connects to the database func DbConnect(dbpath string) Database { @@ -225,6 +229,21 @@ func (d *Db) AddProject(name string, description string, username string) error return err } +func (d *Db) DeleteProject(projectID string, username string) error { + tx := d.MustBegin() + + _, err := tx.Exec(deleteProject, projectID, username) + + if err != nil { + if rollbackErr := tx.Rollback(); rollbackErr != nil { + return fmt.Errorf("error rolling back transaction: %v, delete error: %v", rollbackErr, err) + } + panic(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 := ` From dce91943b329eb3a308d7bcbd72625959f59269a Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 11:22:33 +0100 Subject: [PATCH 04/14] Added DeleteProject handler, untested --- backend/internal/database/db.go | 2 +- .../internal/handlers/handlers_project_related.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index 92cbc71..680d7e2 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -20,7 +20,7 @@ type Database interface { PromoteToAdmin(username string) error GetUserId(username string) (int, error) AddProject(name string, description string, username string) error - // DeleteProject(name string, username string) error + DeleteProject(name string, username string) error Migrate() error MigrateSampleData() error GetProjectId(projectname string) (int, error) diff --git a/backend/internal/handlers/handlers_project_related.go b/backend/internal/handlers/handlers_project_related.go index a92616d..be4ffef 100644 --- a/backend/internal/handlers/handlers_project_related.go +++ b/backend/internal/handlers/handlers_project_related.go @@ -30,6 +30,18 @@ func (gs *GState) CreateProject(c *fiber.Ctx) error { return c.Status(200).SendString("Project added") } +func (gs *GState) DeleteProject(c *fiber.Ctx) error { + + projectID := c.Params("projectID") + username := c.Params("username") + + if err := gs.Db.DeleteProject(projectID, username); err != nil { + return c.Status(500).SendString((err.Error())) + } + + return c.Status(200).SendString("Project deleted") +} + // GetUserProjects returns all projects that the user is a member of func (gs *GState) GetUserProjects(c *fiber.Ctx) error { // First we get the username from the token From c01dd21b0a202220fb9cb8a6ef12d9e619fed2f9 Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 11:23:24 +0100 Subject: [PATCH 05/14] uncommented --- backend/internal/handlers/global_state.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index 1492a09..7a4213b 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -23,7 +23,7 @@ type GlobalState interface { GetWeeklyReportsUserHandler(c *fiber.Ctx) error IsProjectManagerHandler(c *fiber.Ctx) error // UpdateProject(c *fiber.Ctx) error // To update a project // WIP - // DeleteProject(c *fiber.Ctx) error // To delete a project // WIP + DeleteProject(c *fiber.Ctx) error // To delete a project // WIP // CreateTask(c *fiber.Ctx) error // To create a new task // WIP // GetTasks(c *fiber.Ctx) error // To get all tasks // WIP // GetTask(c *fiber.Ctx) error // To get a specific task // WIP From bb93cef1d3fbda86d34fb3053b4dd8ceeb4d305c Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 11:43:47 +0100 Subject: [PATCH 06/14] New deleteproject handler, WIP --- backend/internal/handlers/handlers_project_related.go | 5 +++++ backend/main.go | 1 + 2 files changed, 6 insertions(+) diff --git a/backend/internal/handlers/handlers_project_related.go b/backend/internal/handlers/handlers_project_related.go index be4ffef..7b95c26 100644 --- a/backend/internal/handlers/handlers_project_related.go +++ b/backend/internal/handlers/handlers_project_related.go @@ -226,3 +226,8 @@ func (gs *GState) IsProjectManagerHandler(c *fiber.Ctx) error { // Return the result as JSON return c.JSON(map[string]bool{"isProjectManager": isManager}) } + +func (gs *GState) CreateTask(c *fiber.Ctx) error { + + return nil +} diff --git a/backend/main.go b/backend/main.go index b907177..d0ccdb1 100644 --- a/backend/main.go +++ b/backend/main.go @@ -87,6 +87,7 @@ func main() { server.Get("/api/getUserProjects", gs.GetUserProjects) server.Post("/api/loginrenew", gs.LoginRenew) server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches + server.Delete("api/project", gs.DeleteProject) // WIP server.Post("/api/project", gs.CreateProject) server.Get("/api/project/:projectId", gs.GetProject) server.Get("/api/getWeeklyReport", gs.GetWeeklyReport) From 1919b7cb990d11f4769e713bcb5189b13f5fb0c7 Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 12:11:05 +0100 Subject: [PATCH 07/14] Added ChangeUserName in db --- backend/internal/database/db.go | 16 +++++++++ backend/internal/handlers/global_state.go | 1 + .../handlers/handlers_project_related.go | 34 +++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index 680d7e2..8b0c8c4 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -27,6 +27,7 @@ type Database interface { AddWeeklyReport(projectName string, userName string, week int, developmentTime int, meetingTime int, adminTime int, ownWorkTime int, studyTime int, testingTime int) error AddUserToProject(username string, projectname string, role string) error ChangeUserRole(username string, projectname string, role string) error + ChangeUserName(username string, newname string) error GetAllUsersProject(projectname string) ([]UserProjectMember, error) GetAllUsersApplication() ([]string, error) GetProjectsForUser(username string) ([]types.Project, error) @@ -67,6 +68,7 @@ const addWeeklyReport = `WITH UserLookup AS (SELECT id FROM users WHERE username VALUES ((SELECT id FROM ProjectLookup), (SELECT id FROM UserLookup),?, ?, ?, ?, ?, ?, ?);` const addUserToProject = "INSERT INTO user_roles (user_id, project_id, p_role) VALUES (?, ?, ?)" // WIP const changeUserRole = "UPDATE user_roles SET p_role = ? WHERE user_id = ? AND project_id = ?" +const changeUserName = "UPDATE user SET username = ? WHERE user_id = ?" // WIP const getProjectsForUser = `SELECT p.id, p.name, p.description FROM projects p JOIN user_roles ur ON p.id = ur.project_id @@ -169,6 +171,20 @@ func (d *Db) ChangeUserRole(username string, projectname string, role string) er return err3 } +// ChangeUserRole changes the role of a user within a project. +func (d *Db) ChangeUserName(username string, newname string) error { + // Get the user ID + var userid int + userid, err := d.GetUserId(username) + if err != nil { + panic(err) + } + + // Execute the SQL query to change the user's role + _, err2 := d.Exec(changeUserName, username, userid) + return err2 +} + // GetUserRole retrieves the role of a user within a project. func (d *Db) GetUserRole(username string, projectname string) (string, error) { var role string diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index 7a4213b..1934b21 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -38,6 +38,7 @@ type GlobalState interface { ListAllUsers(c *fiber.Ctx) error // To get a list of all users in the application database ListAllUsersProject(c *fiber.Ctx) error // To get a list of all users for a specific project ProjectRoleChange(c *fiber.Ctx) error // To change a users role in a project + // ProjectNameChange(c * fiber.Ctx) error // WIP } // "Constructor" diff --git a/backend/internal/handlers/handlers_project_related.go b/backend/internal/handlers/handlers_project_related.go index 7b95c26..055328b 100644 --- a/backend/internal/handlers/handlers_project_related.go +++ b/backend/internal/handlers/handlers_project_related.go @@ -93,6 +93,40 @@ func (gs *GState) ProjectRoleChange(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) } +// ProjectRoleChange is a handler that changes a user's role within a project +func (gs *GState) ProjectNameChange(c *fiber.Ctx) error { + + //check token and get username of current user + user := c.Locals("user").(*jwt.Token) + claims := user.Claims.(jwt.MapClaims) + projectManagerUsername := claims["name"].(string) + log.Info(projectManagerUsername) + // Extract the necessary parameters from the request + data := new(types.RoleChange) + if err := c.BodyParser(data); err != nil { + log.Info("error parsing username, project or role") + return c.Status(400).SendString(err.Error()) + } + + // dubble diping and checcking if current user is + + if ismanager, err := gs.Db.IsProjectManager(projectManagerUsername, data.Projectname); err != nil { + log.Warn("Error checking if projectmanager:", err) + return c.Status(500).SendString(err.Error()) + } else if !ismanager { + log.Warn("tried chaning name when not projectmanager:", err) + return c.Status(401).SendString("you can not change name when not projectManager") + } + + // Change the user's role within the project in the database + if err := gs.Db.ChangeUserRole(data.Username, data.Projectname, data.Role); err != nil { + return c.Status(500).SendString(err.Error()) + } + + // Return a success message + return c.SendStatus(fiber.StatusOK) +} + // GetProject retrieves a specific project by its ID func (gs *GState) GetProject(c *fiber.Ctx) error { // Extract the project ID from the request parameters or body From 3515a86bbb7dd6062efc9e94dae3c6252a99270b Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 12:50:04 +0100 Subject: [PATCH 08/14] Added ChangeUserName handler, untested and WIP, --- backend/internal/handlers/global_state.go | 2 +- .../handlers/handlers_project_related.go | 34 ----------- .../handlers/handlers_user_related.go | 58 +++++++++++++++---- backend/internal/types/project.go | 5 ++ backend/main.go | 1 + 5 files changed, 53 insertions(+), 47 deletions(-) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index 1934b21..4a4b73f 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -38,7 +38,7 @@ type GlobalState interface { ListAllUsers(c *fiber.Ctx) error // To get a list of all users in the application database ListAllUsersProject(c *fiber.Ctx) error // To get a list of all users for a specific project ProjectRoleChange(c *fiber.Ctx) error // To change a users role in a project - // ProjectNameChange(c * fiber.Ctx) error // WIP + ChangeUserName(c *fiber.Ctx) error // WIP } // "Constructor" diff --git a/backend/internal/handlers/handlers_project_related.go b/backend/internal/handlers/handlers_project_related.go index 055328b..7b95c26 100644 --- a/backend/internal/handlers/handlers_project_related.go +++ b/backend/internal/handlers/handlers_project_related.go @@ -93,40 +93,6 @@ func (gs *GState) ProjectRoleChange(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) } -// ProjectRoleChange is a handler that changes a user's role within a project -func (gs *GState) ProjectNameChange(c *fiber.Ctx) error { - - //check token and get username of current user - user := c.Locals("user").(*jwt.Token) - claims := user.Claims.(jwt.MapClaims) - projectManagerUsername := claims["name"].(string) - log.Info(projectManagerUsername) - // Extract the necessary parameters from the request - data := new(types.RoleChange) - if err := c.BodyParser(data); err != nil { - log.Info("error parsing username, project or role") - return c.Status(400).SendString(err.Error()) - } - - // dubble diping and checcking if current user is - - if ismanager, err := gs.Db.IsProjectManager(projectManagerUsername, data.Projectname); err != nil { - log.Warn("Error checking if projectmanager:", err) - return c.Status(500).SendString(err.Error()) - } else if !ismanager { - log.Warn("tried chaning name when not projectmanager:", err) - return c.Status(401).SendString("you can not change name when not projectManager") - } - - // Change the user's role within the project in the database - if err := gs.Db.ChangeUserRole(data.Username, data.Projectname, data.Role); err != nil { - return c.Status(500).SendString(err.Error()) - } - - // Return a success message - return c.SendStatus(fiber.StatusOK) -} - // GetProject retrieves a specific project by its ID func (gs *GState) GetProject(c *fiber.Ctx) error { // Extract the project ID from the request parameters or body diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index 75b8953..862416c 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -105,7 +105,7 @@ func (gs *GState) Login(c *fiber.Ctx) error { if err != nil { log.Info("Error checking admin status:", err) return c.Status(500).SendString(err.Error()) -} + } // Create the Claims claims := jwt.MapClaims{ "name": u.Username, @@ -187,17 +187,17 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error { return c.JSON(users) } -// @Summary PromoteToAdmin -// @Description promote chosen user to admin -// @Tags User -// @Accept json -// @Produce plain -// @Param NewUser body types.NewUser true "user info" -// @Success 200 {json} json "Successfully prometed user" -// @Failure 400 {string} string "bad request" -// @Failure 401 {string} string "Unauthorized" -// @Failure 500 {string} string "Internal server error" -// @Router /promoteToAdmin [post] +// @Summary PromoteToAdmin +// @Description promote chosen user to admin +// @Tags User +// @Accept json +// @Produce plain +// @Param NewUser body types.NewUser true "user info" +// @Success 200 {json} json "Successfully prometed user" +// @Failure 400 {string} string "bad request" +// @Failure 401 {string} string "Unauthorized" +// @Failure 500 {string} string "Internal server error" +// @Router /promoteToAdmin [post] func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error { // Extract the username from the request body var newUser types.NewUser @@ -219,3 +219,37 @@ func (gs *GState) PromoteToAdmin(c *fiber.Ctx) error { // Return a success message return c.SendStatus(fiber.StatusOK) } + +// Changes a users name in the database +func (gs *GState) ChangeUserName(c *fiber.Ctx) error { + + //check token and get username of current user + user := c.Locals("user").(*jwt.Token) + claims := user.Claims.(jwt.MapClaims) + projectManagerUsername := claims["name"].(string) + log.Info(projectManagerUsername) + // Extract the necessary parameters from the request + data := new(types.NameChange) + if err := c.BodyParser(data); err != nil { + log.Info("error parsing username, project or role") + return c.Status(400).SendString(err.Error()) + } + + // dubble diping and checcking if current user is + + if ismanager, err := gs.Db.IsProjectManager(projectManagerUsername, c.Params(data.Name)); err != nil { + log.Warn("Error checking if projectmanager:", err) + return c.Status(500).SendString(err.Error()) + } else if !ismanager { + log.Warn("tried changing name when not projectmanager:", err) + return c.Status(401).SendString("you can not change name when not projectManager") + } + + // Change the user's name within the project in the database + if err := gs.Db.ChangeUserName(projectManagerUsername, data.Name); err != nil { + return c.Status(500).SendString(err.Error()) + } + + // Return a success message + return c.SendStatus(fiber.StatusOK) +} diff --git a/backend/internal/types/project.go b/backend/internal/types/project.go index c336bcb..6a7c91a 100644 --- a/backend/internal/types/project.go +++ b/backend/internal/types/project.go @@ -19,3 +19,8 @@ type RoleChange struct { Username string `json:"username"` Projectname string `json:"projectname"` } + +type NameChange struct { + ID int `json:"id" db:"id"` + Name string `json:"name" db:"name"` +} diff --git a/backend/main.go b/backend/main.go index d0ccdb1..8a70186 100644 --- a/backend/main.go +++ b/backend/main.go @@ -93,6 +93,7 @@ func main() { server.Get("/api/getWeeklyReport", gs.GetWeeklyReport) server.Post("/api/signReport", gs.SignReport) server.Put("/api/addUserToProject", gs.AddUserToProjectHandler) + server.Put("/api/changeUserName", gs.ChangeUserName) server.Post("/api/promoteToAdmin", gs.PromoteToAdmin) server.Get("/api/users/all", gs.ListAllUsers) server.Get("/api/getWeeklyReportsUser", gs.GetWeeklyReportsUserHandler) From a18ce465de6639b994b22d55d0c28ed5d44da252 Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 13:03:43 +0100 Subject: [PATCH 09/14] Added a handler for getting all users for a specific project, WIP and untested --- backend/internal/handlers/global_state.go | 1 + backend/internal/handlers/handlers_user_related.go | 14 ++++++++++++++ backend/main.go | 3 ++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index 4a4b73f..a3e43ce 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -39,6 +39,7 @@ type GlobalState interface { ListAllUsersProject(c *fiber.Ctx) error // To get a list of all users for a specific project ProjectRoleChange(c *fiber.Ctx) error // To change a users role in a project ChangeUserName(c *fiber.Ctx) error // WIP + GetAllUsersProject(c *fiber.Ctx) error // WIP } // "Constructor" diff --git a/backend/internal/handlers/handlers_user_related.go b/backend/internal/handlers/handlers_user_related.go index 862416c..ea511ac 100644 --- a/backend/internal/handlers/handlers_user_related.go +++ b/backend/internal/handlers/handlers_user_related.go @@ -187,6 +187,20 @@ func (gs *GState) ListAllUsers(c *fiber.Ctx) error { return c.JSON(users) } +func (gs *GState) GetAllUsersProject(c *fiber.Ctx) error { + // Get all users from a project + projectName := c.Params("projectName") + users, err := gs.Db.GetAllUsersProject(projectName) + if err != nil { + log.Info("Error getting users from project:", err) // Debug print + return c.Status(500).SendString(err.Error()) + } + + log.Info("Returning all users") + // Return the list of users as JSON + return c.JSON(users) +} + // @Summary PromoteToAdmin // @Description promote chosen user to admin // @Tags User diff --git a/backend/main.go b/backend/main.go index 8a70186..888cb51 100644 --- a/backend/main.go +++ b/backend/main.go @@ -88,8 +88,9 @@ func main() { server.Post("/api/loginrenew", gs.LoginRenew) server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches server.Delete("api/project", gs.DeleteProject) // WIP - server.Post("/api/project", gs.CreateProject) + server.Post("/api/project", gs.CreateProject) // WIP server.Get("/api/project/:projectId", gs.GetProject) + server.Get("/api/project/getAllUsers", gs.GetAllUsersProject) server.Get("/api/getWeeklyReport", gs.GetWeeklyReport) server.Post("/api/signReport", gs.SignReport) server.Put("/api/addUserToProject", gs.AddUserToProjectHandler) From c6969b9503f489fd051ab15d8ad2a9358e284f38 Mon Sep 17 00:00:00 2001 From: borean Date: Wed, 20 Mar 2024 14:16:47 +0100 Subject: [PATCH 10/14] updated api path for DeleteProject --- backend/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/main.go b/backend/main.go index 888cb51..c5ac0bb 100644 --- a/backend/main.go +++ b/backend/main.go @@ -87,7 +87,7 @@ func main() { server.Get("/api/getUserProjects", gs.GetUserProjects) server.Post("/api/loginrenew", gs.LoginRenew) server.Delete("/api/userdelete/:username", gs.UserDelete) // Perhaps just use POST to avoid headaches - server.Delete("api/project", gs.DeleteProject) // WIP + server.Delete("api/project/:projectID", gs.DeleteProject) // WIP server.Post("/api/project", gs.CreateProject) // WIP server.Get("/api/project/:projectId", gs.GetProject) server.Get("/api/project/getAllUsers", gs.GetAllUsersProject) From a84c2c6129ed6d1daca8369030a3ddd81b101c0f Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 20 Mar 2024 14:54:22 +0100 Subject: [PATCH 11/14] Rename GetWeeklyReportsForProject -> GetWeeklyReportsForUser --- frontend/src/API/API.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index 1e90174..a3c4e92 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -49,7 +49,7 @@ interface API { week: string, token: string, ): Promise>; - getWeeklyReportsForProject( + getWeeklyReportsForUser( username: string, projectName: string, token: string, @@ -274,7 +274,7 @@ export const api: API = { } }, - async getWeeklyReportsForProject( + async getWeeklyReportsForUser( username: string, projectName: string, token: string, From 7799eb09e9c0e857e97531b7aa7057b8ab9331d7 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 20 Mar 2024 15:11:25 +0100 Subject: [PATCH 12/14] Delete unused handler prototype --- backend/internal/handlers/global_state.go | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index a3e43ce..b88bdcd 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -22,19 +22,7 @@ type GlobalState interface { PromoteToAdmin(c *fiber.Ctx) error GetWeeklyReportsUserHandler(c *fiber.Ctx) error IsProjectManagerHandler(c *fiber.Ctx) error - // UpdateProject(c *fiber.Ctx) error // To update a project // WIP - DeleteProject(c *fiber.Ctx) error // To delete a project // WIP - // CreateTask(c *fiber.Ctx) error // To create a new task // WIP - // GetTasks(c *fiber.Ctx) error // To get all tasks // WIP - // GetTask(c *fiber.Ctx) error // To get a specific task // WIP - // UpdateTask(c *fiber.Ctx) error // To update a task // WIP - // DeleteTask(c *fiber.Ctx) error // To delete a task // WIP - // CreateCollection(c *fiber.Ctx) error // To create a new collection // WIP - // GetCollections(c *fiber.Ctx) error // To get all collections // WIP - // GetCollection(c *fiber.Ctx) error // To get a specific collection // WIP - // UpdateCollection(c *fiber.Ctx) error // To update a collection // WIP - // DeleteCollection(c *fiber.Ctx) error // To delete a collection // WIP - // SignCollection(c *fiber.Ctx) error // To sign a collection // WIP + DeleteProject(c *fiber.Ctx) error // To delete a project // WIP ListAllUsers(c *fiber.Ctx) error // To get a list of all users in the application database ListAllUsersProject(c *fiber.Ctx) error // To get a list of all users for a specific project ProjectRoleChange(c *fiber.Ctx) error // To change a users role in a project From 771118f85ead09aac4619f7870593b7456da804c Mon Sep 17 00:00:00 2001 From: al8763be Date: Wed, 20 Mar 2024 15:13:00 +0100 Subject: [PATCH 13/14] Update API.ts --- frontend/src/API/API.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index a3c4e92..d6a4b23 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -286,7 +286,7 @@ export const api: API = { "Content-Type": "application/json", Authorization: "Bearer " + token, }, - body: JSON.stringify({ username, projectName }), + body: JSON.stringify({ username: username, projectName: projectName}), }); if (!response.ok) { From af5813681d11500c0cfe723af1b8d2522bf277d2 Mon Sep 17 00:00:00 2001 From: al8763be Date: Wed, 20 Mar 2024 15:17:00 +0100 Subject: [PATCH 14/14] Jag avskyr denna handlern --- frontend/src/API/API.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index d6a4b23..6a508b5 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -280,13 +280,12 @@ export const api: API = { token: string, ): Promise> { try { - const response = await fetch("/api/getWeeklyReportsUser", { + const response = await fetch(`/api/getWeeklyReportsUser?username=${username}&projectName=${projectName}`, { method: "GET", headers: { "Content-Type": "application/json", Authorization: "Bearer " + token, }, - body: JSON.stringify({ username: username, projectName: projectName}), }); if (!response.ok) {