endpoint to get tasks
This commit is contained in:
parent
9e058e4f03
commit
712af41c28
5 changed files with 28 additions and 3 deletions
|
@ -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)
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<div class="fs-3">{{ .title }}</div>
|
||||
<hr>
|
||||
{{template "userForm" .}}
|
||||
<a class="link-secondary" href="/register">Register instead</a>
|
||||
<a class="link-secondary" href="/view/register">Register instead</a>
|
||||
</div>
|
||||
|
||||
{{template "scripts" .}}
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<div class="fs-3">{{ .title }}</div>
|
||||
<hr>
|
||||
{{template "userForm" .}}
|
||||
<a class="link-secondary" href="/login">Login instead</a>
|
||||
<a class="link-secondary" href="/view/login">Login instead</a>
|
||||
</div>
|
||||
|
||||
{{template "scripts" .}}
|
||||
|
|
|
@ -15,7 +15,14 @@
|
|||
|
||||
{{template "scripts" .}}
|
||||
<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>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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")
|
||||
{
|
||||
|
|
Reference in a new issue