fyp/logic/tasks/list.go

82 lines
2.0 KiB
Go
Raw Normal View History

2024-04-12 20:36:23 +01:00
package tasks
import (
2024-04-14 14:51:16 +01:00
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
2024-04-12 20:36:23 +01:00
dbtypes "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/utils"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
)
func handleList(handler *Handle) {
handler.PostAuth("/tasks/task", 1, func(c *Context) *Error {
var getId JustId
if _err := c.ToJSON(&getId); _err != nil {
return _err
}
var task Task
err := GetDBOnce(c, &task, "tasks where id=$1;", getId.Id)
if err == NotFoundError {
return c.JsonBadRequest("Did not found task id")
} else if err != nil {
return c.E500M("Faied to get task", err)
}
c.ShowMessage = false
return c.SendJSON(task)
})
2024-04-12 20:36:23 +01:00
handler.PostAuth("/tasks/list", 1, func(c *Context) *Error {
var err error = nil
2024-04-12 20:36:23 +01:00
var requestData struct {
ModelId string `json:"model_id"`
Page int `json:"page"`
2024-04-12 20:36:23 +01:00
}
if _err := c.ToJSON(&requestData); _err != nil {
return _err
}
if requestData.ModelId == "" && c.User.UserType < int(dbtypes.User_Admin) {
return c.SendJSONStatus(400, "Please provide a model_id")
}
if requestData.ModelId != "" {
_, err := GetBaseModel(c.Db, requestData.ModelId)
2024-04-19 15:39:51 +01:00
if err == NotFoundError {
2024-04-12 20:36:23 +01:00
return c.SendJSONStatus(404, "Model not found!")
} else if err != nil {
return c.Error500(err)
}
}
var rows []*Task = nil
2024-04-12 20:36:23 +01:00
if requestData.ModelId != "" {
rows, err = GetDbMultitple[Task](c, "tasks where model_id=$1 order by created_on desc limit 11 offset $2", requestData.ModelId, requestData.Page*10)
2024-04-12 20:36:23 +01:00
if err != nil {
return c.Error500(err)
}
} else {
rows, err = GetDbMultitple[Task](c, "tasks order by created_on desc limit 11 offset $1", requestData.Page*10)
2024-04-12 20:36:23 +01:00
if err != nil {
return c.Error500(err)
}
}
max_len := min(11, len(rows))
c.ShowMessage = false
2024-04-12 20:36:23 +01:00
return c.SendJSON(struct {
TaskList []*Task `json:"task_list"`
ShowNext bool `json:"show_next"`
}{
rows[0:max_len],
len(rows) > 10,
})
2024-04-12 20:36:23 +01:00
})
}