getHistory api route

This commit is contained in:
2023-07-16 17:54:02 +01:00
parent 48ec7f91bc
commit f0b94fb491

31
main.go
View File

@@ -41,6 +41,10 @@ type historyData struct {
PointsGained int `json:"pointsGained"` PointsGained int `json:"pointsGained"`
} }
type historyArray struct {
History []historyData `json:"history"`
}
// log the user into their account // log the user into their account
func login(c *gin.Context) { func login(c *gin.Context) {
// get the username and password from the request // get the username and password from the request
@@ -114,10 +118,27 @@ func completeTask(c *gin.Context) {
} }
//func getHistory(c *gin.Context) { func getHistory(c *gin.Context) {
// // get the log of past points gained // get the log of past points gained
//} var history []historyData
// // get array of all historyData
rows, err := db.Query("SELECT * FROM history")
checkErr(err)
for rows.Next() {
var tempHistoryData historyData
err = rows.Scan(&tempHistoryData.UID, &tempHistoryData.TID, &tempHistoryData.Time, &tempHistoryData.PointsGained)
if err != nil {
c.IndentedJSON(http.StatusNotFound, history)
return
}
history = append(history, tempHistoryData)
}
rows.Close()
var jsonHistory historyArray
jsonHistory.History = history
c.IndentedJSON(http.StatusOK, jsonHistory)
}
//func addTask(c *gin.Context) { //func addTask(c *gin.Context) {
// // add a task to the list of tasks // // add a task to the list of tasks
//} //}
@@ -128,7 +149,7 @@ func main() {
router.GET("/getTasks", getTasks) router.GET("/getTasks", getTasks)
router.POST("/completeTask", completeTask) router.POST("/completeTask", completeTask)
//router.POST("/addTask", addTask) //router.POST("/addTask", addTask)
//router.GET("/getHistory", getHistory) router.GET("/getHistory", getHistory)
router.Run("localhost:8080") router.Run("localhost:8080")
} }