Finished route for getting user points

This commit is contained in:
2023-07-21 22:53:32 +01:00
parent 31913d0b94
commit 0bdaac368c

30
main.go
View File

@@ -53,6 +53,15 @@ type historyArray struct {
History []historyData `json:"history"`
}
type userPointsData struct {
Name string `json:"name"`
Points int `json:"points"`
}
type userPointsArray struct {
UserPoints []userPointsData `json:"userPoints"`
}
// log the user into their account
func login(c *gin.Context) {
// get the username and password from the request
@@ -199,7 +208,28 @@ func addTask(c *gin.Context) {
}
func getUserPoints(c *gin.Context) {
// get the names of all users
var allUsers []userPointsData
rows, err := db.Query("SELECT name, points FROM users WHERE role='user'")
checkErr(err)
// put them in an array of users
for rows.Next() {
var tempUser userPointsData
err = rows.Scan(&tempUser.Name, &tempUser.Points)
if err != nil {
c.IndentedJSON(http.StatusNotFound, allUsers)
return
}
allUsers = append(allUsers, tempUser)
}
rows.Close()
// return to the requester
var jsonUserPoints userPointsArray
jsonUserPoints.UserPoints = allUsers
c.IndentedJSON(http.StatusOK, jsonUserPoints)
}
func main() {