completeTask api route

This commit is contained in:
2023-07-16 17:45:54 +01:00
parent a1fd8ea825
commit 48ec7f91bc
2 changed files with 34 additions and 16 deletions

BIN
main

Binary file not shown.

50
main.go
View File

@@ -18,14 +18,14 @@ type loginInput struct {
} }
type loginOutput struct { type loginOutput struct {
UID string `json:"uid"` UID int `json:"uid"`
Name string `json:"name"` Name string `json:"name"`
Password string `json:"password"` Password string `json:"password"`
Role string `json:"role"` Role string `json:"role"`
} }
type task struct { type task struct {
TID string `json:"tid"` TID int `json:"tid"`
Name string `json:"name"` Name string `json:"name"`
Points int `json:"points"` Points int `json:"points"`
} }
@@ -34,19 +34,19 @@ type taskArray struct {
Tasks []task `json:"tasks"` Tasks []task `json:"tasks"`
} }
//type historyData struct { type historyData struct {
// UID string `json:"uid"` UID int `json:"uid"`
// TID string `json:"tid"` TID int `json:"tid"`
// Time string `json:"time"` Time string `json:"time"`
// PointsGained int `json:"pointsGained"` 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
var userData loginInput var userData loginInput
if err := c.BindJSON(&userData); err != nil { if err := c.BindJSON(&userData); err != nil {
c.IndentedJSON(http.StatusNotAcceptable, userData) c.IndentedJSON(http.StatusBadRequest, userData)
return return
} }
// check for user using given credentials // check for user using given credentials
@@ -57,12 +57,12 @@ func login(c *gin.Context) {
err = stmt.QueryRow(userData.Name).Scan(&user.Name, &user.Password, &user.UID, &user.Role) 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.StatusNotFound, userData)
return return
} }
if user.Password != userData.Password { if user.Password != userData.Password {
// user not real // user not real
c.IndentedJSON(http.StatusNotAcceptable, userData) c.IndentedJSON(http.StatusNotFound, userData)
return return
} else { } else {
// user is in // user is in
@@ -92,10 +92,28 @@ func getTasks(c *gin.Context) {
c.IndentedJSON(http.StatusOK, jsonTasks) c.IndentedJSON(http.StatusOK, jsonTasks)
} }
//func completeTask(c *gin.Context) { func completeTask(c *gin.Context) {
// // complete a task in the history log // get the task data from the request
//} var completedTask historyData
// if err := c.BindJSON(&completedTask); err != nil {
c.IndentedJSON(http.StatusBadRequest, completedTask)
return
}
// insert data to history table
stmt, err := db.Prepare("INSERT INTO history (uid, taskid, time, pointsGained) VALUES (?, ?, ?, ?)")
if err != nil {
c.IndentedJSON(http.StatusNotModified, completedTask)
return
}
_, err = stmt.Exec(completedTask.UID, completedTask.TID, completedTask.Time, completedTask.PointsGained)
if err != nil {
c.IndentedJSON(http.StatusNotModified, completedTask)
return
}
c.IndentedJSON(http.StatusOK, completedTask)
}
//func getHistory(c *gin.Context) { //func getHistory(c *gin.Context) {
// // get the log of past points gained // // get the log of past points gained
//} //}
@@ -108,7 +126,7 @@ 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)