From abfb79b9917f8f1f7ef9f7f08d58d6971902f126 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 4 Apr 2024 19:54:31 +0200 Subject: [PATCH] getUserName path implemented --- backend/internal/database/db.go | 7 ++++ .../internal/handlers/users/GetUserName.go | 32 +++++++++++++++++++ backend/main.go | 1 + frontend/src/API/API.ts | 27 ++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 backend/internal/handlers/users/GetUserName.go diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index 0bd67bc..b8e7205 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -44,6 +44,7 @@ type Database interface { GetProjectTimes(projectName string) (map[string]int, error) UpdateWeeklyReport(projectName string, userName string, week int, developmentTime int, meetingTime int, adminTime int, ownWorkTime int, studyTime int, testingTime int) error RemoveProject(projectname string) error + GetUserName(id int) (string, error) } // This struct is a wrapper type that holds the database connection @@ -611,3 +612,9 @@ func (d *Db) RemoveProject(projectname string) error { _, err := d.Exec("DELETE FROM projects WHERE name = ?", projectname) return err } + +func (d *Db) GetUserName(id int) (string, error) { + var username string + err := d.Get(&username, "SELECT username FROM users WHERE id = ?", id) + return username, err +} diff --git a/backend/internal/handlers/users/GetUserName.go b/backend/internal/handlers/users/GetUserName.go new file mode 100644 index 0000000..82b6cc8 --- /dev/null +++ b/backend/internal/handlers/users/GetUserName.go @@ -0,0 +1,32 @@ +package users + +import ( + "strconv" + db "ttime/internal/database" + + "github.com/gofiber/fiber/v2" +) + +// Return the username of a user given their user id +func GetUserName(c *fiber.Ctx) error { + // Check the query params for userId + user_id_string := c.Query("userId") + if user_id_string == "" { + return c.Status(400).SendString("Missing user id") + } + + // Convert to int + user_id, err := strconv.Atoi(user_id_string) + if err != nil { + return c.Status(400).SendString("Invalid user id") + } + + // Get the username from the database + username, err := db.GetDb(c).GetUserName(user_id) + if err != nil { + return c.Status(500).SendString(err.Error()) + } + + // Send the nuclear launch codes to north korea + return c.JSON(fiber.Map{"username": username}) +} diff --git a/backend/main.go b/backend/main.go index 42daa5c..7b19dd9 100644 --- a/backend/main.go +++ b/backend/main.go @@ -103,6 +103,7 @@ func main() { // userGroup := api.Group("/user") // Not currently in use api.Get("/users/all", users.ListAllUsers) api.Get("/project/getAllUsers", users.GetAllUsersProject) + api.Get("/username", users.GetUserName) api.Post("/login", users.Login) api.Post("/register", users.Register) api.Post("/loginrenew", users.LoginRenew) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index cf3d1eb..86ad6dc 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -233,6 +233,12 @@ interface API { projectName: string, token: string, ): Promise>; + /** + * Get the username from the id + * @param {number} id The id of the user + * @param {string} token Your token + */ + getUsername(id: number, token: string): Promise>; } /** An instance of the API */ @@ -870,4 +876,25 @@ export const api: API = { } return { success: true, message: "User promoted to project manager" }; }, + + async getUsername(id: number, token: string): Promise> { + try { + const response = await fetch(`/api/username?userId=${id}`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }, + }); + + if (!response.ok) { + return { success: false, message: "Failed to get username" }; + } else { + const data = (await response.json()) as string; + return { success: true, data }; + } + } catch (e) { + return { success: false, message: "Failed to get username" }; + } + }, };