Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add GetMultiple function to fetch multiple tasks by IDs #760

Closed
wants to merge 7 commits into from
40 changes: 40 additions & 0 deletions api/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
87 changes: 45 additions & 42 deletions binding/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions binding/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
Loading