2024-04-12 20:36:23 +01:00
|
|
|
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) {
|
2024-04-13 14:21:38 +01:00
|
|
|
|
|
|
|
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 {
|
2024-04-13 14:21:38 +01:00
|
|
|
var err error = nil
|
2024-04-12 20:36:23 +01:00
|
|
|
|
|
|
|
var requestData struct {
|
|
|
|
ModelId string `json:"model_id"`
|
2024-04-13 14:21:38 +01:00
|
|
|
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)
|
|
|
|
if err == ModelNotFoundError {
|
|
|
|
return c.SendJSONStatus(404, "Model not found!")
|
|
|
|
} else if err != nil {
|
|
|
|
return c.Error500(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-13 14:21:38 +01:00
|
|
|
var rows []*Task = nil
|
2024-04-12 20:36:23 +01:00
|
|
|
|
|
|
|
if requestData.ModelId != "" {
|
2024-04-13 14:21:38 +01:00
|
|
|
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 {
|
2024-04-13 14:21:38 +01:00
|
|
|
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))
|
2024-04-13 14:21:38 +01:00
|
|
|
|
|
|
|
c.ShowMessage = false
|
2024-04-12 20:36:23 +01:00
|
|
|
return c.SendJSON(struct {
|
2024-04-13 14:21:38 +01:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|