Updated history view api to return more human-readable data

This commit is contained in:
2023-07-21 18:25:57 +01:00
parent 8b1d66617f
commit 02b69b8cdf
3 changed files with 55 additions and 13 deletions

View File

@@ -72,8 +72,8 @@ async function populateScreen() {
<div class="full">
<article class="card">
<header>
<p>User: ${history[i].uid}</p>
<p>Task: ${history[i].tid}</p>
<p>User: ${history[i].user}</p>
<p>Task: ${history[i].task}</p>
<p>Time: ${history[i].time}</p>
<p>Points gained: ${history[i].pointsGained}</p>
</header>

BIN
main

Binary file not shown.

64
main.go
View File

@@ -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) {