diff --git a/frontend/index.js b/frontend/index.js index 8ffb330..29ad2e5 100644 --- a/frontend/index.js +++ b/frontend/index.js @@ -72,8 +72,8 @@ async function populateScreen() {
-

User: ${history[i].uid}

-

Task: ${history[i].tid}

+

User: ${history[i].user}

+

Task: ${history[i].task}

Time: ${history[i].time}

Points gained: ${history[i].pointsGained}

diff --git a/main b/main index 892a0e9..de35d28 100755 Binary files a/main and b/main differ diff --git a/main.go b/main.go index 9e8238b..b15a29d 100644 --- a/main.go +++ b/main.go @@ -34,15 +34,22 @@ type taskArray struct { Tasks []task `json:"tasks"` } -type historyData struct { +type historyReqData struct { UID int `json:"uid"` TID int `json:"tid"` Time string `json:"time"` PointsGained int `json:"pointsGained"` } +type historyResData struct { + User string `json:"user"` + Task string `json:"task"` + Time string `json:"time"` + PointsGained int `json:"pointsGained"` +} + type historyArray struct { - History []historyData `json:"history"` + History []historyResData `json:"history"` } // log the user into their account @@ -56,7 +63,8 @@ func login(c *gin.Context) { // check for user using given credentials stmt, err := db.Prepare("SELECT * FROM users WHERE name=?") checkErr(err) - defer stmt.Close() + err = stmt.Close() + checkErr(err) var user loginOutput err = stmt.QueryRow(userData.Name).Scan(&user.UID, &user.Name, &user.Password, &user.Role) if err != nil { @@ -92,7 +100,8 @@ func getTasks(c *gin.Context) { } tasks = append(tasks, tempTask) } - rows.Close() + err = rows.Close() + checkErr(err) var jsonTasks taskArray jsonTasks.Tasks = tasks c.IndentedJSON(http.StatusOK, jsonTasks) @@ -100,7 +109,7 @@ func getTasks(c *gin.Context) { func completeTask(c *gin.Context) { // get the task data from the request - var completedTask historyData + var completedTask historyReqData if err := c.BindJSON(&completedTask); err != nil { c.IndentedJSON(http.StatusBadRequest, completedTask) return @@ -121,12 +130,12 @@ func completeTask(c *gin.Context) { func getHistory(c *gin.Context) { // get the log of past points gained - var history []historyData - // get array of all historyData + var history []historyReqData + // get array of all historyReqData rows, err := db.Query("SELECT * FROM history") checkErr(err) for rows.Next() { - var tempHistoryData historyData + var tempHistoryData historyReqData err = rows.Scan(&tempHistoryData.UID, &tempHistoryData.TID, &tempHistoryData.Time, &tempHistoryData.PointsGained) if err != nil { c.IndentedJSON(http.StatusNotFound, history) @@ -134,9 +143,41 @@ func getHistory(c *gin.Context) { } history = append(history, tempHistoryData) } - rows.Close() + err = rows.Close() + checkErr(err) + + var historyRes []historyResData + // make the data human-readable + for i := 0; i < len(history); i++ { + var tempHistory historyResData + // get the username + var tempUser loginOutput + stmt, err := db.Prepare("SELECT * FROM users WHERE uid=?") + checkErr(err) + err = stmt.Close() + checkErr(err) + err = stmt.QueryRow(history[i].UID).Scan(&tempUser.UID, &tempUser.Name, &tempUser.Password, &tempUser.Role) + checkErr(err) + tempHistory.User = tempUser.Name + + // get the task name and points + var tempTask task + stmt, err = db.Prepare("SELECT * FROM activities WHERE taskId=?") + checkErr(err) + err = stmt.Close() + checkErr(err) + err = stmt.QueryRow(history[i].TID).Scan(&tempTask.TID, &tempTask.Name, &tempTask.Points) + checkErr(err) + tempHistory.Task = tempTask.Name + tempHistory.PointsGained = tempTask.Points + + tempHistory.Time = history[i].Time + + historyRes = append(historyRes, tempHistory) + } + var jsonHistory historyArray - jsonHistory.History = history + jsonHistory.History = historyRes c.IndentedJSON(http.StatusOK, jsonHistory) } @@ -180,7 +221,8 @@ func main() { }) router.Static("/frontend", "./frontend") - router.Run("localhost:8080") + err := router.Run("localhost:8080") + checkErr(err) } func checkErr(err error) {