diff --git a/main.go b/main.go index d555977..6aa37d6 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,10 @@ type historyData struct { PointsGained int `json:"pointsGained"` } +type historyArray struct { + History []historyData `json:"history"` +} + // log the user into their account func login(c *gin.Context) { // get the username and password from the request @@ -114,10 +118,27 @@ func completeTask(c *gin.Context) { } -//func getHistory(c *gin.Context) { -// // get the log of past points gained -//} -// +func getHistory(c *gin.Context) { + // 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) { // // add a task to the list of tasks //} @@ -128,7 +149,7 @@ func main() { router.GET("/getTasks", getTasks) router.POST("/completeTask", completeTask) //router.POST("/addTask", addTask) - //router.GET("/getHistory", getHistory) + router.GET("/getHistory", getHistory) router.Run("localhost:8080") }