fyp/logic/tasks/list.go

82 lines
2.0 KiB
Go

package tasks
import (
dbtypes "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
. "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)
})
handler.PostAuth("/tasks/list", 1, func(c *Context) *Error {
var err error = nil
var requestData struct {
ModelId string `json:"model_id"`
Page int `json:"page"`
}
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)
if err == ModelNotFoundError {
return c.SendJSONStatus(404, "Model not found!")
} else if err != nil {
return c.Error500(err)
}
}
var rows []*Task = nil
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)
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)
if err != nil {
return c.Error500(err)
}
}
max_len := min(11, len(rows))
c.ShowMessage = false
return c.SendJSON(struct {
TaskList []*Task `json:"task_list"`
ShowNext bool `json:"show_next"`
}{
rows[0:max_len],
len(rows) > 10,
})
})
}