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

BIN
main

Binary file not shown.

70
main.go
View File

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