diff --git a/Lab01/app/database/database.go b/Lab01/app/database/database.go index eac4ab8..6eb5fa1 100644 --- a/Lab01/app/database/database.go +++ b/Lab01/app/database/database.go @@ -28,6 +28,12 @@ func (db *Database) Initialize() { db.ORM = orm } +func (db *Database) GetAllTasks() []Task { + var tasks []Task + db.ORM.Find(&tasks) + return tasks +} + func (db *Database) CreateUser(username string, password string) error { user := User{Username: username, Password: password} result := db.ORM.Create(&user) diff --git a/Lab01/app/templates/login.tmpl b/Lab01/app/templates/login.tmpl index 339755b..9b2691f 100644 --- a/Lab01/app/templates/login.tmpl +++ b/Lab01/app/templates/login.tmpl @@ -12,7 +12,7 @@
{{ .title }}

{{template "userForm" .}} - Register instead + Register instead {{template "scripts" .}} diff --git a/Lab01/app/templates/register.tmpl b/Lab01/app/templates/register.tmpl index d5b887a..1c95139 100644 --- a/Lab01/app/templates/register.tmpl +++ b/Lab01/app/templates/register.tmpl @@ -12,7 +12,7 @@
{{ .title }}

{{template "userForm" .}} - Login instead + Login instead {{template "scripts" .}} diff --git a/Lab01/app/templates/tasks.tmpl b/Lab01/app/templates/tasks.tmpl index ed9629a..c500c92 100644 --- a/Lab01/app/templates/tasks.tmpl +++ b/Lab01/app/templates/tasks.tmpl @@ -15,7 +15,14 @@ {{template "scripts" .}} diff --git a/Lab01/app/webpage/webpage.go b/Lab01/app/webpage/webpage.go index 2e6de1f..56e3be6 100644 --- a/Lab01/app/webpage/webpage.go +++ b/Lab01/app/webpage/webpage.go @@ -25,6 +25,18 @@ func (wp *Webpage) defineRoutes() { c.HTML(http.StatusOK, "register.tmpl", gin.H{"title": "Register"}) }) } + tasks := wp.Router.Group("/tasks") + { + tasks.GET("", func(c *gin.Context) { + username := c.Request.Header.Get("username") + if wp.Database.UserIsLoggedIn(username) { + tasks := wp.Database.GetAllTasks() + c.JSON(200, gin.H{"tasks": tasks}) + } else { + c.JSON(401, gin.H{"message": "unauthorized"}) + } + }) + } auth := wp.Router.Group("/auth") {