diff --git a/api/task.go b/api/task.go index 74600e68..872ce712 100644 --- a/api/task.go +++ b/api/task.go @@ -24,6 +24,7 @@ const ( TasksRoot = "/tasks" TasksReportRoot = TasksRoot + "/report" TasksReportQueueRoot = TasksReportRoot + "/queue" + TasksReportQueueRootByIds=TasksRoot+"/multiple" TasksReportDashboardRoot = TasksReportRoot + "/dashboard" TaskRoot = TasksRoot + "/:" + ID TaskReportRoot = TaskRoot + "/report" @@ -51,6 +52,7 @@ func (h TaskHandler) AddRoutes(e *gin.Engine) { routeGroup.GET(TasksRoot+"/", h.List) routeGroup.POST(TasksRoot, h.Create) routeGroup.GET(TaskRoot, h.Get) + routeGroup.POST(TasksReportQueueRootByIds, h.GetMultiple) routeGroup.PUT(TaskRoot, h.Update) routeGroup.PATCH(TaskRoot, Transaction, h.Update) routeGroup.DELETE(TaskRoot, h.Delete) @@ -108,6 +110,44 @@ func (h TaskHandler) Get(ctx *gin.Context) { h.Respond(ctx, http.StatusOK, r) } +// GetMultiple godoc +// @summary Get tasks by a list of IDs. +// @description Get multiple tasks by their IDs. +// @tags tasks +// @produce json +// @success 200 {array} api.Task +// @router /tasks/multiple [post] +// @param ids body []int true "List of Task IDs" +func (h TaskHandler) GetMultiple(ctx *gin.Context) { + var ids []int + var tasks []model.Task + + // Parse the body to get the list of IDs + if err := ctx.ShouldBindJSON(&ids); err != nil { + h.Respond(ctx, http.StatusBadRequest, gin.H{"error": "Invalid input"}) + return + } + + // Query the database to find all tasks with the given IDs + db := h.DB(ctx).Preload(clause.Associations) + result := db.Find(&tasks, ids) + if result.Error != nil { + _ = ctx.Error(result.Error) + return + } + + // Convert the retrieved tasks to the response format + var response []Task + for _, task := range tasks { + r := Task{} + r.With(&task) + response = append(response, r) + } // Respond with the list of tasks + h.Respond(ctx, http.StatusOK, response) + +} + + // List godoc // @summary List all tasks. // @description List all tasks. diff --git a/binding/client.go b/binding/client.go index a5976eb9..f6038a45 100644 --- a/binding/client.go +++ b/binding/client.go @@ -142,49 +142,52 @@ func (r *Client) Get(path string, object any, params ...Param) (err error) { return } + // Post a resource. -func (r *Client) Post(path string, object any) (err error) { - request := func() (request *http.Request, err error) { - bfr, err := json.Marshal(object) - if err != nil { - err = liberr.Wrap(err) - return - } - reader := bytes.NewReader(bfr) - request = &http.Request{ - Header: http.Header{}, - Method: http.MethodPost, - Body: io.NopCloser(reader), - URL: r.join(path), - } - request.Header.Set(api.Accept, binding.MIMEJSON) - return - } - response, err := r.send(request) - if err != nil { - return - } - status := response.StatusCode - switch status { - case http.StatusAccepted: - case http.StatusNoContent: - case http.StatusOK, - http.StatusCreated: - var body []byte - body, err = io.ReadAll(response.Body) - if err != nil { - err = liberr.Wrap(err) - return - } - err = json.Unmarshal(body, object) - if err != nil { - err = liberr.Wrap(err) - return - } - default: - err = r.restError(response) - } - return +func (r *Client) Post(path string, object any, returnValue ...any) (err error) { + request := func() (request *http.Request, err error) { + bfr, err := json.Marshal(object) + if err != nil { + err = liberr.Wrap(err) + return + } + reader := bytes.NewReader(bfr) + request = &http.Request{ + Header: http.Header{}, + Method: http.MethodPost, + Body: io.NopCloser(reader), + URL: r.join(path), + } + request.Header.Set(api.Accept, binding.MIMEJSON) + return + } + response, err := r.send(request) + if err != nil { + return + } + status := response.StatusCode + switch status { + case http.StatusAccepted: + case http.StatusNoContent: + case http.StatusOK, + http.StatusCreated: + var body []byte + body, err = io.ReadAll(response.Body) + if err != nil { + err = liberr.Wrap(err) + return + } + if returnValue != nil { + err = json.Unmarshal(body, returnValue) + if err != nil { + err = liberr.Wrap(err) + return + } + } + default: + err = r.restError(response) + } + return } // Put a resource. diff --git a/binding/task.go b/binding/task.go index 623bd2bc..8ce8206d 100644 --- a/binding/task.go +++ b/binding/task.go @@ -15,6 +15,12 @@ func (h *Task) Create(r *api.Task) (err error) { return } +// Get multiple tasks by their IDs. +func (h *Task) GetTasksByIds(ids []uint) (tasks []api.Task, err error) { + err = h.client.Post(api.TasksReportQueueRootByIds, ids, &tasks) + return +} + // Get a Task by ID. func (h *Task) Get(id uint) (r *api.Task, err error) { r = &api.Task{}