getUserName path implemented
This commit is contained in:
		
							parent
							
								
									db6fdf3c29
								
							
						
					
					
						commit
						abfb79b991
					
				
					 4 changed files with 67 additions and 0 deletions
				
			
		|  | @ -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 | ||||
| } | ||||
|  |  | |||
							
								
								
									
										32
									
								
								backend/internal/handlers/users/GetUserName.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								backend/internal/handlers/users/GetUserName.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -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}) | ||||
| } | ||||
|  | @ -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) | ||||
|  |  | |||
|  | @ -233,6 +233,12 @@ interface API { | |||
|     projectName: string, | ||||
|     token: string, | ||||
|   ): Promise<APIResponse<string>>; | ||||
|   /** | ||||
|    * 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<APIResponse<string>>; | ||||
| } | ||||
| 
 | ||||
| /** 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<APIResponse<string>> { | ||||
|     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" }; | ||||
|     } | ||||
|   }, | ||||
| }; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus