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 {
UID string `json:"uid"`
UID int `json:"uid"`
Name string `json:"name"`
Password string `json:"password"`
Role string `json:"role"`
}
type task struct {
TID string `json:"tid"`
TID int `json:"tid"`
Name string `json:"name"`
Points int `json:"points"`
}
@@ -34,19 +34,19 @@ 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"`
//}
type historyData struct {
UID int `json:"uid"`
TID int `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
var userData loginInput
if err := c.BindJSON(&userData); err != nil {
c.IndentedJSON(http.StatusNotAcceptable, userData)
c.IndentedJSON(http.StatusBadRequest, userData)
return
}
// 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)
if err != nil {
// search failed user not real
c.IndentedJSON(http.StatusNotAcceptable, userData)
c.IndentedJSON(http.StatusNotFound, userData)
return
}
if user.Password != userData.Password {
// user not real
c.IndentedJSON(http.StatusNotAcceptable, userData)
c.IndentedJSON(http.StatusNotFound, userData)
return
} else {
// user is in
@@ -92,10 +92,28 @@ func getTasks(c *gin.Context) {
c.IndentedJSON(http.StatusOK, jsonTasks)
}
//func completeTask(c *gin.Context) {
// // complete a task in the history log
//}
//
func completeTask(c *gin.Context) {
// 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) {
// // get the log of past points gained
//}
@@ -108,7 +126,7 @@ func main() {
router := gin.Default()
router.POST("/login", login)
router.GET("/getTasks", getTasks)
//router.POST("/completeTask", completeTask)
router.POST("/completeTask", completeTask)
//router.POST("/addTask", addTask)
//router.GET("/getHistory", getHistory)