-
Notifications
You must be signed in to change notification settings - Fork 145
/
list.go
62 lines (47 loc) · 1.46 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package workflows
import (
"math"
"github.com/labstack/echo/v4"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/transformers"
"github.com/hatchet-dev/hatchet/internal/repository"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
)
func (t *WorkflowService) WorkflowList(ctx echo.Context, request gen.WorkflowListRequestObject) (gen.WorkflowListResponseObject, error) {
tenant := ctx.Get("tenant").(*db.TenantModel)
limit := 50
offset := 0
listOpts := &repository.ListWorkflowsOpts{
Limit: &limit,
Offset: &offset,
}
listResp, err := t.config.APIRepository.Workflow().ListWorkflows(tenant.ID, listOpts)
if err != nil {
return nil, err
}
rows := make([]gen.Workflow, len(listResp.Rows))
for i := range listResp.Rows {
workflow, err := transformers.ToWorkflow(listResp.Rows[i].WorkflowModel, listResp.Rows[i].LatestRun)
if err != nil {
return nil, err
}
rows[i] = *workflow
}
// use the total rows and limit to calculate the total pages
totalPages := int64(math.Ceil(float64(listResp.Count) / float64(limit)))
currPage := 1 + int64(math.Ceil(float64(offset)/float64(limit)))
nextPage := currPage + 1
if currPage == totalPages {
nextPage = currPage
}
return gen.WorkflowList200JSONResponse(
gen.WorkflowList{
Rows: &rows,
Pagination: &gen.PaginationResponse{
NumPages: &totalPages,
CurrentPage: &currPage,
NextPage: &nextPage,
},
},
), nil
}