diff --git a/Lab01/app/database/database.go b/Lab01/app/database/database.go index d1a18a0..eac4ab8 100644 --- a/Lab01/app/database/database.go +++ b/Lab01/app/database/database.go @@ -13,6 +13,10 @@ func migrateInitial(orm *gorm.DB) { if err != nil { fmt.Println(fmt.Errorf("failed to migrate User")) } + err = orm.AutoMigrate(&Task{}) + if err != nil { + fmt.Println(fmt.Errorf("failed to migrate Task")) + } } func (db *Database) Initialize() { diff --git a/Lab01/app/database/types.go b/Lab01/app/database/types.go index e1b2d2e..081dbda 100644 --- a/Lab01/app/database/types.go +++ b/Lab01/app/database/types.go @@ -14,3 +14,9 @@ type User struct { Password string LoggedIn bool } + +type Task struct { + ID int `gorm:"primaryKey"` + Description string + Done bool +} diff --git a/Lab01/app/templates/_base.tmpl b/Lab01/app/templates/_base.tmpl index 6702099..4fdaf5f 100644 --- a/Lab01/app/templates/_base.tmpl +++ b/Lab01/app/templates/_base.tmpl @@ -22,13 +22,13 @@ @@ -96,8 +96,13 @@ } async function userLoggedIn() { - const response = await fetch("/user", {method: 'GET', headers: myHeaders}); + const response = await fetch("/auth/user", {method: 'GET', headers: myHeaders}); const json = await response.json(); + if (json.logged_in === true) { + document.getElementById("button-login").hidden = true; + button_logout.hidden = false; + document.getElementById("button-tasks").hidden = false; + } return json.logged_in } @@ -105,7 +110,7 @@ button_logout.disabled = true; let data = new FormData(); data.append("username", username); - const response = await fetch("/logout", {method: 'POST', body: data}); + const response = await fetch("/auth/logout", {method: 'POST', body: data}); deleteCookie("username"); response.ok && redirect("/"); } diff --git a/Lab01/app/templates/login.tmpl b/Lab01/app/templates/login.tmpl index a212568..339755b 100644 --- a/Lab01/app/templates/login.tmpl +++ b/Lab01/app/templates/login.tmpl @@ -21,13 +21,13 @@ userLoggedIn().then((loggedIn) => loggedIn && redirect("/")); async function submitForm(formData) { - const response = await fetch("/login", {method: 'POST', body: formData, redirect: 'follow'}); + const response = await fetch("/auth/login", {method: 'POST', body: formData, redirect: 'follow'}); if (response.ok) { const json = await response.json(); setCookie("username", json.username, 1); redirect("/"); } else { - redirect("/login"); + redirect("/view/login"); } } diff --git a/Lab01/app/templates/register.tmpl b/Lab01/app/templates/register.tmpl index c88532c..d5b887a 100644 --- a/Lab01/app/templates/register.tmpl +++ b/Lab01/app/templates/register.tmpl @@ -19,11 +19,11 @@ {{template "formScripts" .}} diff --git a/Lab01/app/templates/tasks.tmpl b/Lab01/app/templates/tasks.tmpl index 91bfa0e..ed9629a 100644 --- a/Lab01/app/templates/tasks.tmpl +++ b/Lab01/app/templates/tasks.tmpl @@ -15,7 +15,7 @@ {{template "scripts" .}} diff --git a/Lab01/app/webpage/webpage.go b/Lab01/app/webpage/webpage.go index 34ee336..2e6de1f 100644 --- a/Lab01/app/webpage/webpage.go +++ b/Lab01/app/webpage/webpage.go @@ -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) })