endpoint to get tasks

This commit is contained in:
Florian Hoss 2022-04-04 12:26:36 +02:00
parent 9e058e4f03
commit 712af41c28
5 changed files with 28 additions and 3 deletions

View file

@ -28,6 +28,12 @@ func (db *Database) Initialize() {
db.ORM = orm 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 { func (db *Database) CreateUser(username string, password string) error {
user := User{Username: username, Password: password} user := User{Username: username, Password: password}
result := db.ORM.Create(&user) result := db.ORM.Create(&user)

View file

@ -12,7 +12,7 @@
<div class="fs-3">{{ .title }}</div> <div class="fs-3">{{ .title }}</div>
<hr> <hr>
{{template "userForm" .}} {{template "userForm" .}}
<a class="link-secondary" href="/register">Register instead</a> <a class="link-secondary" href="/view/register">Register instead</a>
</div> </div>
{{template "scripts" .}} {{template "scripts" .}}

View file

@ -12,7 +12,7 @@
<div class="fs-3">{{ .title }}</div> <div class="fs-3">{{ .title }}</div>
<hr> <hr>
{{template "userForm" .}} {{template "userForm" .}}
<a class="link-secondary" href="/login">Login instead</a> <a class="link-secondary" href="/view/login">Login instead</a>
</div> </div>
{{template "scripts" .}} {{template "scripts" .}}

View file

@ -15,7 +15,14 @@
{{template "scripts" .}} {{template "scripts" .}}
<script> <script>
userLoggedIn().then((loggedIn) => !loggedIn && redirect("/view/login")); userLoggedIn().then((loggedIn) => !loggedIn ? redirect("/view/login") : getAllTasks());
async function getAllTasks() {
const response = await fetch("/tasks", {method: 'GET', headers: myHeaders});
const json = await response.json();
console.log(json);
}
</script> </script>
</body> </body>
</html> </html>

View file

@ -25,6 +25,18 @@ func (wp *Webpage) defineRoutes() {
c.HTML(http.StatusOK, "register.tmpl", gin.H{"title": "Register"}) 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") auth := wp.Router.Group("/auth")
{ {