change paths, add task to db

This commit is contained in:
Florian Hoss 2022-04-04 12:13:55 +02:00
parent 6a56b03bf6
commit 9e058e4f03
7 changed files with 87 additions and 70 deletions

View file

@ -8,71 +8,73 @@ import (
)
func (wp *Webpage) redirectHome(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/tasks")
c.Redirect(http.StatusTemporaryRedirect, "/view/tasks")
}
func (wp *Webpage) defineRoutes() {
wp.Router.GET("/tasks", func(c *gin.Context) {
c.HTML(http.StatusOK, "tasks.tmpl", gin.H{"title": "Tasks"})
})
wp.Router.GET("/login", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.tmpl", gin.H{"title": "Login"})
})
wp.Router.GET("/register", func(c *gin.Context) {
c.HTML(http.StatusOK, "register.tmpl", gin.H{"title": "Register"})
})
wp.Router.GET("/user", func(c *gin.Context) {
username := c.Request.Header.Get("username")
if username != "" {
success := wp.Database.UserIsLoggedIn(username)
c.JSON(200, gin.H{"logged_in": success, "username": username})
return
}
c.JSON(200, gin.H{"logged_in": false, "username": ""})
})
wp.Router.POST("/logout", func(c *gin.Context) {
username, uExisting := c.GetPostForm("username")
if uExisting == false || username == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
success := wp.Database.LogoutUser(username)
c.JSON(200, gin.H{"logged_out": success, "username": username})
})
wp.Router.POST("/login", func(c *gin.Context) {
username, uExisting := c.GetPostForm("username")
password, pExisting := c.GetPostForm("password")
if uExisting == false || pExisting == false || username == "" || password == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
success := wp.Database.LoginUser(username, password)
if success == true {
c.JSON(200, gin.H{"logged_in": success, "username": username})
return
}
c.JSON(401, gin.H{"message": "user or password not found"})
})
wp.Router.POST("/register", func(c *gin.Context) {
username, uExisting := c.GetPostForm("username")
password, pExisting := c.GetPostForm("password")
if uExisting == false || pExisting == false || username == "" || password == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
err := wp.Database.CreateUser(username, password)
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
c.JSON(200, gin.H{"message": "user registered"})
})
view := wp.Router.Group("/view")
{
view.GET("/tasks", func(c *gin.Context) {
c.HTML(http.StatusOK, "tasks.tmpl", gin.H{"title": "Tasks"})
})
view.GET("/login", func(c *gin.Context) {
c.HTML(http.StatusOK, "login.tmpl", gin.H{"title": "Login"})
})
view.GET("/register", func(c *gin.Context) {
c.HTML(http.StatusOK, "register.tmpl", gin.H{"title": "Register"})
})
}
auth := wp.Router.Group("/auth")
{
auth.GET("/user", func(c *gin.Context) {
username := c.Request.Header.Get("username")
if username != "" {
success := wp.Database.UserIsLoggedIn(username)
c.JSON(200, gin.H{"logged_in": success, "username": username})
return
}
c.JSON(200, gin.H{"logged_in": false, "username": ""})
})
auth.POST("/logout", func(c *gin.Context) {
username, uExisting := c.GetPostForm("username")
if uExisting == false || username == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
success := wp.Database.LogoutUser(username)
c.JSON(200, gin.H{"logged_out": success, "username": username})
})
auth.POST("/login", func(c *gin.Context) {
username, uExisting := c.GetPostForm("username")
password, pExisting := c.GetPostForm("password")
if uExisting == false || pExisting == false || username == "" || password == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
success := wp.Database.LoginUser(username, password)
if success == true {
c.JSON(200, gin.H{"logged_in": success, "username": username})
return
}
c.JSON(401, gin.H{"message": "user or password not found"})
})
auth.POST("/register", func(c *gin.Context) {
username, uExisting := c.GetPostForm("username")
password, pExisting := c.GetPostForm("password")
if uExisting == false || pExisting == false || username == "" || password == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
err := wp.Database.CreateUser(username, password)
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
c.JSON(200, gin.H{"message": "user registered"})
})
}
wp.Router.NoRoute(func(c *gin.Context) {
wp.redirectHome(c)
})