task can be added
This commit is contained in:
parent
87e66f74bd
commit
f73f862bcf
4 changed files with 64 additions and 15 deletions
|
@ -11,6 +11,16 @@ func (wp *Webpage) redirectHome(c *gin.Context) {
|
|||
c.Redirect(http.StatusTemporaryRedirect, "/view/tasks")
|
||||
}
|
||||
|
||||
func (wp *Webpage) isLoggedInMiddleware(c *gin.Context) bool {
|
||||
username := c.Request.Header.Get("username")
|
||||
if wp.Database.UserIsLoggedIn(username) {
|
||||
return true
|
||||
} else {
|
||||
c.JSON(401, gin.H{"message": "unauthorized"})
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (wp *Webpage) defineRoutes() {
|
||||
|
||||
view := wp.Router.Group("/view")
|
||||
|
@ -28,12 +38,21 @@ func (wp *Webpage) defineRoutes() {
|
|||
tasks := wp.Router.Group("/tasks")
|
||||
{
|
||||
tasks.GET("", func(c *gin.Context) {
|
||||
username := c.Request.Header.Get("username")
|
||||
if wp.Database.UserIsLoggedIn(username) {
|
||||
if wp.isLoggedInMiddleware(c) {
|
||||
tasks := wp.Database.GetAllTasks()
|
||||
c.JSON(200, gin.H{"tasks": tasks})
|
||||
} else {
|
||||
c.JSON(401, gin.H{"message": "unauthorized"})
|
||||
}
|
||||
})
|
||||
tasks.POST("", func(c *gin.Context) {
|
||||
if wp.isLoggedInMiddleware(c) {
|
||||
username := c.Request.Header.Get("username")
|
||||
description, existing := c.GetPostForm("description")
|
||||
if existing == false || description == "" {
|
||||
c.JSON(400, gin.H{"message": "bad post form"})
|
||||
return
|
||||
}
|
||||
task := wp.Database.CreateTask(username, description)
|
||||
c.JSON(200, gin.H{"task": task})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Reference in a new issue