addTask api route

This commit is contained in:
2023-07-16 18:12:07 +01:00
parent f0b94fb491
commit a85e63a327
2 changed files with 22 additions and 6 deletions

28
main.go
View File

@@ -115,7 +115,6 @@ func completeTask(c *gin.Context) {
return
}
c.IndentedJSON(http.StatusOK, completedTask)
}
func getHistory(c *gin.Context) {
@@ -139,16 +138,33 @@ func getHistory(c *gin.Context) {
c.IndentedJSON(http.StatusOK, jsonHistory)
}
//func addTask(c *gin.Context) {
// // add a task to the list of tasks
//}
func addTask(c *gin.Context) {
// get the data of the new task
var newTask task
if err := c.BindJSON(&newTask); err != nil {
c.IndentedJSON(http.StatusBadRequest, newTask)
return
}
// insert new task into the task table
stmt, err := db.Prepare("INSERT INTO activities (name, points) VALUES (?,?)")
if err != nil {
c.IndentedJSON(http.StatusNotModified, newTask)
return
}
_, err = stmt.Exec(newTask.Name, newTask.Points)
if err != nil {
c.IndentedJSON(http.StatusNotModified, newTask)
return
}
c.IndentedJSON(http.StatusOK, newTask)
}
func main() {
router := gin.Default()
router.POST("/login", login)
router.GET("/getTasks", getTasks)
router.POST("/completeTask", completeTask)
//router.POST("/addTask", addTask)
router.POST("/addTask", addTask)
router.GET("/getTasks", getTasks)
router.GET("/getHistory", getHistory)
router.Run("localhost:8080")