Added getTask api call

This commit is contained in:
2023-07-16 17:18:38 +01:00
parent 561b81f2cf
commit 3666c4efbc
2 changed files with 48 additions and 22 deletions

70
main.go
View File

@@ -2,6 +2,7 @@ package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"net/http"
@@ -30,13 +31,17 @@ type task struct {
Points int `json:"points"`
}
type historyData struct {
UID string `json:"uid"`
TID string `json:"tid"`
Time string `json:"time"`
PointsGained int `json:"pointsGained"`
type taskArray struct {
Tasks []task `json:"tasks"`
}
//type historyData struct {
// UID string `json:"uid"`
// TID string `json:"tid"`
// Time string `json:"time"`
// PointsGained int `json:"pointsGained"`
//}
// log the user into their account
func login(c *gin.Context) {
// get the username and password from the request
@@ -49,14 +54,18 @@ func login(c *gin.Context) {
checkErr(err)
defer stmt.Close()
var user loginOutput
err = stmt.QueryRow(userData.Name).Scan(&user)
fmt.Printf("%s", userData.Name)
err = stmt.QueryRow(userData.Name).Scan(&user.Name, &user.Password, &user.UID, &user.Role)
if err != nil {
// search failed user not real
c.IndentedJSON(http.StatusNotAcceptable, userData)
fmt.Print(err)
return
}
if user.Name != userData.Name || user.Password != userData.Password {
if user.Password != userData.Password {
// user not real
c.IndentedJSON(http.StatusNotAcceptable, userData)
c.IndentedJSON(http.StatusMethodNotAllowed, userData)
return
} else {
// user is in
c.IndentedJSON(http.StatusOK, user)
@@ -66,27 +75,44 @@ func login(c *gin.Context) {
func getTasks(c *gin.Context) {
// return a list of all the tasks
var tasks []task
// get array of all tasks
rows, err := db.Query("SELECT * FROM activities")
checkErr(err)
for rows.Next() {
var tempTask task
err = rows.Scan(&tempTask.TID, &tempTask.Name, &tempTask.Points)
if err != nil {
c.IndentedJSON(http.StatusNotFound, tasks)
return
}
tasks = append(tasks, tempTask)
}
rows.Close()
var jsonTasks taskArray
jsonTasks.Tasks = tasks
c.IndentedJSON(http.StatusOK, jsonTasks)
}
func completeTask(c *gin.Context) {
// complete a task in the history log
}
func getHistory(c *gin.Context) {
// get the log of past points gained
}
func addTask(c *gin.Context) {
// add a task to the list of tasks
}
//func completeTask(c *gin.Context) {
// // complete a task in the history log
//}
//
//func getHistory(c *gin.Context) {
// // get the log of past points gained
//}
//
//func addTask(c *gin.Context) {
// // add a task to the list of tasks
//}
func main() {
router := gin.Default()
router.POST("/login", login)
router.GET("/getTasks", getTasks)
router.POST("/completeTask", completeTask)
router.POST("/addTask", addTask)
router.GET("/getHistory", getHistory)
//router.POST("/completeTask", completeTask)
//router.POST("/addTask", addTask)
//router.GET("/getHistory", getHistory)
router.Run("localhost:8080")
}