From 9fcc33b0ba373e9938dd767c7546b969c511334e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?BENEDEK=20L=C3=A1szl=C3=B3?= Date: Thu, 10 Oct 2024 23:42:30 +0200 Subject: [PATCH] list task --- api/auth/login.go | 1 - api/endpotins.go | 5 +++++ api/task/task.go | 24 ++++++++++++++++++++++++ database/task/task.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/api/auth/login.go b/api/auth/login.go index a3a5ccc..d82155d 100644 --- a/api/auth/login.go +++ b/api/auth/login.go @@ -39,5 +39,4 @@ func Login(c *gin.Context) { "token": token, }) } - } diff --git a/api/endpotins.go b/api/endpotins.go index b637148..ceff60c 100644 --- a/api/endpotins.go +++ b/api/endpotins.go @@ -33,6 +33,11 @@ func Listen(address string, path string) { 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.GET("logged_in", func(c *gin.Context) { diff --git a/api/task/task.go b/api/task/task.go index fdac55c..5b87091 100644 --- a/api/task/task.go +++ b/api/task/task.go @@ -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, + }) + } +} diff --git a/database/task/task.go b/database/task/task.go index 46cb4a8..7b1f354 100644 --- a/database/task/task.go +++ b/database/task/task.go @@ -17,6 +17,7 @@ func Add(task model.Task) error { return err } + func Remove(id int) error { db := database.GetDB() @@ -27,3 +28,35 @@ func Remove(id int) error { 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 +}