Updated history view api to return more human-readable data
This commit is contained in:
@@ -72,8 +72,8 @@ async function populateScreen() {
|
|||||||
<div class="full">
|
<div class="full">
|
||||||
<article class="card">
|
<article class="card">
|
||||||
<header>
|
<header>
|
||||||
<p>User: ${history[i].uid}</p>
|
<p>User: ${history[i].user}</p>
|
||||||
<p>Task: ${history[i].tid}</p>
|
<p>Task: ${history[i].task}</p>
|
||||||
<p>Time: ${history[i].time}</p>
|
<p>Time: ${history[i].time}</p>
|
||||||
<p>Points gained: ${history[i].pointsGained}</p>
|
<p>Points gained: ${history[i].pointsGained}</p>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
64
main.go
64
main.go
@@ -34,15 +34,22 @@ type taskArray struct {
|
|||||||
Tasks []task `json:"tasks"`
|
Tasks []task `json:"tasks"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type historyData struct {
|
type historyReqData struct {
|
||||||
UID int `json:"uid"`
|
UID int `json:"uid"`
|
||||||
TID int `json:"tid"`
|
TID int `json:"tid"`
|
||||||
Time string `json:"time"`
|
Time string `json:"time"`
|
||||||
PointsGained int `json:"pointsGained"`
|
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 {
|
type historyArray struct {
|
||||||
History []historyData `json:"history"`
|
History []historyResData `json:"history"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// log the user into their account
|
// log the user into their account
|
||||||
@@ -56,7 +63,8 @@ func login(c *gin.Context) {
|
|||||||
// check for user using given credentials
|
// check for user using given credentials
|
||||||
stmt, err := db.Prepare("SELECT * FROM users WHERE name=?")
|
stmt, err := db.Prepare("SELECT * FROM users WHERE name=?")
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
defer stmt.Close()
|
err = stmt.Close()
|
||||||
|
checkErr(err)
|
||||||
var user loginOutput
|
var user loginOutput
|
||||||
err = stmt.QueryRow(userData.Name).Scan(&user.UID, &user.Name, &user.Password, &user.Role)
|
err = stmt.QueryRow(userData.Name).Scan(&user.UID, &user.Name, &user.Password, &user.Role)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -92,7 +100,8 @@ func getTasks(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
tasks = append(tasks, tempTask)
|
tasks = append(tasks, tempTask)
|
||||||
}
|
}
|
||||||
rows.Close()
|
err = rows.Close()
|
||||||
|
checkErr(err)
|
||||||
var jsonTasks taskArray
|
var jsonTasks taskArray
|
||||||
jsonTasks.Tasks = tasks
|
jsonTasks.Tasks = tasks
|
||||||
c.IndentedJSON(http.StatusOK, jsonTasks)
|
c.IndentedJSON(http.StatusOK, jsonTasks)
|
||||||
@@ -100,7 +109,7 @@ func getTasks(c *gin.Context) {
|
|||||||
|
|
||||||
func completeTask(c *gin.Context) {
|
func completeTask(c *gin.Context) {
|
||||||
// get the task data from the request
|
// get the task data from the request
|
||||||
var completedTask historyData
|
var completedTask historyReqData
|
||||||
if err := c.BindJSON(&completedTask); err != nil {
|
if err := c.BindJSON(&completedTask); err != nil {
|
||||||
c.IndentedJSON(http.StatusBadRequest, completedTask)
|
c.IndentedJSON(http.StatusBadRequest, completedTask)
|
||||||
return
|
return
|
||||||
@@ -121,12 +130,12 @@ 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
|
var history []historyReqData
|
||||||
// get array of all historyData
|
// get array of all historyReqData
|
||||||
rows, err := db.Query("SELECT * FROM history")
|
rows, err := db.Query("SELECT * FROM history")
|
||||||
checkErr(err)
|
checkErr(err)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var tempHistoryData historyData
|
var tempHistoryData historyReqData
|
||||||
err = rows.Scan(&tempHistoryData.UID, &tempHistoryData.TID, &tempHistoryData.Time, &tempHistoryData.PointsGained)
|
err = rows.Scan(&tempHistoryData.UID, &tempHistoryData.TID, &tempHistoryData.Time, &tempHistoryData.PointsGained)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.IndentedJSON(http.StatusNotFound, history)
|
c.IndentedJSON(http.StatusNotFound, history)
|
||||||
@@ -134,9 +143,41 @@ func getHistory(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
history = append(history, tempHistoryData)
|
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
|
var jsonHistory historyArray
|
||||||
jsonHistory.History = history
|
jsonHistory.History = historyRes
|
||||||
c.IndentedJSON(http.StatusOK, jsonHistory)
|
c.IndentedJSON(http.StatusOK, jsonHistory)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,7 +221,8 @@ func main() {
|
|||||||
})
|
})
|
||||||
router.Static("/frontend", "./frontend")
|
router.Static("/frontend", "./frontend")
|
||||||
|
|
||||||
router.Run("localhost:8080")
|
err := router.Run("localhost:8080")
|
||||||
|
checkErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkErr(err error) {
|
func checkErr(err error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user