szoe-pontok/database/task/task.go

64 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-10-10 21:06:50 +00:00
package task
import (
"git.tek.govt.hu/dowerx/szoe-pontok/database"
"git.tek.govt.hu/dowerx/szoe-pontok/model"
)
func Add(task model.Task) error {
db := database.GetDB()
_, err := db.NamedExec(
`insert into "task" ("description", "points", "recipient", "issuer") values
(:description, :points,
(select "id" from "user" where "neptun" = :recipient),
(select "id" from "user" where "neptun" = :issuer)
)`, task)
return err
}
2024-10-10 21:42:30 +00:00
2024-10-10 21:15:49 +00:00
func Remove(id int) error {
db := database.GetDB()
_, err := db.NamedExec(
2024-10-10 21:45:29 +00:00
`delete from "task" where "id" = :id`,
map[string]interface{}{
2024-10-10 21:15:49 +00:00
"id": id,
})
return err
}
2024-10-10 21:42:30 +00:00
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
}