From f05457b1c686f987e3da2a5a3b59f9d88f658138 Mon Sep 17 00:00:00 2001 From: MiriSafra Date: Sun, 27 Oct 2024 13:38:11 +0200 Subject: [PATCH 1/5] Modify Post function to include response handling Signed-off-by: MiriSafra --- binding/client.go | 131 +++++++++++++++++++++++++++++++--------------- 1 file changed, 89 insertions(+), 42 deletions(-) diff --git a/binding/client.go b/binding/client.go index a5976eb9..54ae8c36 100644 --- a/binding/client.go +++ b/binding/client.go @@ -143,48 +143,95 @@ func (r *Client) Get(path string, object any, params ...Param) (err error) { } // 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) (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 +// } + +// Post a resource. +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. From d5c58cccdc08259b089e412ffb8e6db63dd8df74 Mon Sep 17 00:00:00 2001 From: shevijacobson Date: Sun, 27 Oct 2024 14:21:52 +0200 Subject: [PATCH 2/5] Add function to retrieve multiple tasks by their IDs Signed-off-by: shevijacobson --- binding/task.go | 6 ++++++ 1 file changed, 6 insertions(+) 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{} From 52b724c17e748f8fc580fc0ae0c68c264afc62e3 Mon Sep 17 00:00:00 2001 From: MiriSafra Date: Sun, 27 Oct 2024 15:15:56 +0200 Subject: [PATCH 3/5] Add multi-task fetch Signed-off-by: MiriSafra --- api/task.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/api/task.go b/api/task.go index 74600e68..6ca63297 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,36 @@ 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 + } + + +} + + // List godoc // @summary List all tasks. // @description List all tasks. From 50c19104558d88b29110362b8c8c271cba97e942 Mon Sep 17 00:00:00 2001 From: shevijacobson Date: Sun, 27 Oct 2024 15:27:31 +0200 Subject: [PATCH 4/5] Add response format conversion for multiple tasks retrieval in GetMultiple function Signed-off-by: shevijacobson --- api/task.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/api/task.go b/api/task.go index 6ca63297..872ce712 100644 --- a/api/task.go +++ b/api/task.go @@ -136,6 +136,14 @@ func (h TaskHandler) GetMultiple(ctx *gin.Context) { 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) } From 1ce42ce57fb7a9e0f250cc3d89aedf891c5d284e Mon Sep 17 00:00:00 2001 From: MiriSafra Date: Sun, 27 Oct 2024 15:55:38 +0200 Subject: [PATCH 5/5] remove unnecessary code Signed-off-by: MiriSafra --- binding/client.go | 44 -------------------------------------------- 1 file changed, 44 deletions(-) diff --git a/binding/client.go b/binding/client.go index 54ae8c36..f6038a45 100644 --- a/binding/client.go +++ b/binding/client.go @@ -142,50 +142,6 @@ 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 -// } // Post a resource. func (r *Client) Post(path string, object any, returnValue ...any) (err error) {