list task
This commit is contained in:
parent
ec0075993e
commit
9fcc33b0ba
@ -39,5 +39,4 @@ func Login(c *gin.Context) {
|
||||
"token": token,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user