list task

This commit is contained in:
BENEDEK László 2024-10-10 23:42:30 +02:00
parent ec0075993e
commit 9fcc33b0ba
4 changed files with 62 additions and 1 deletions

View File

@ -39,5 +39,4 @@ func Login(c *gin.Context) {
"token": token, "token": token,
}) })
} }
} }

View File

@ -33,6 +33,11 @@ func Listen(address string, path string) {
apiAdmin.POST("task/remove/:id", task.Remove) apiAdmin.POST("task/remove/:id", task.Remove)
} }
apiUser := api.Group("user").Use(auth.LoggedIn)
{
apiUser.GET("list", task.List)
}
apiTest := api.Group("test").Use(auth.LoggedIn).Use(auth.IsAdmin) apiTest := api.Group("test").Use(auth.LoggedIn).Use(auth.IsAdmin)
{ {
apiTest.GET("logged_in", func(c *gin.Context) { apiTest.GET("logged_in", func(c *gin.Context) {

View File

@ -76,3 +76,27 @@ func Remove(c *gin.Context) {
}) })
} }
} }
func List(c *gin.Context) {
recipient, ok := c.Get("neptun")
if !ok {
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
"error": "not logged in",
})
return
}
tasks, err := task.List(recipient.(string))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"error": err.Error(),
})
} else {
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"tasks": tasks,
})
}
}

View File

@ -17,6 +17,7 @@ func Add(task model.Task) error {
return err return err
} }
func Remove(id int) error { func Remove(id int) error {
db := database.GetDB() db := database.GetDB()
@ -27,3 +28,35 @@ func Remove(id int) error {
return err return err
} }
func List(neptun string) ([]model.Task, error) {
db := database.GetDB()
rows, err := db.NamedQuery(
`select "task"."id" as "id","description", "points", "r"."neptun" as "recipient", "i"."neptun" as "issuer", "created_date"
from "task"
inner join "user" as "r" on "r"."id" = "task"."recipient"
inner join "user" as "i" on "i"."id" = "task"."issuer"
where "r"."neptun" = :neptun`,
map[string]interface{}{
"neptun": neptun,
})
if err != nil {
return nil, err
}
tasks := []model.Task{}
var task model.Task
for rows.Next() {
err := rows.StructScan(&task)
if err != nil {
return nil, err
}
tasks = append(tasks, task)
}
return tasks, err
}