|
| 1 | +package plans |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/go-chi/chi/v5" |
| 8 | + "github.com/samber/lo" |
| 9 | + |
| 10 | + api "github.com/openmeterio/openmeter/api/v3" |
| 11 | + "github.com/openmeterio/openmeter/api/v3/apierrors" |
| 12 | + "github.com/openmeterio/openmeter/api/v3/response" |
| 13 | + "github.com/openmeterio/openmeter/openmeter/productcatalog/planaddon" |
| 14 | + "github.com/openmeterio/openmeter/pkg/framework/commonhttp" |
| 15 | + "github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" |
| 16 | + "github.com/openmeterio/openmeter/pkg/pagination" |
| 17 | +) |
| 18 | + |
| 19 | +type ( |
| 20 | + ListPlanAddonsRequest = planaddon.ListPlanAddonsInput |
| 21 | + ListPlanAddonsResponse = response.PagePaginationResponse[api.PlanAddon] |
| 22 | + ListPlanAddonsParams = api.ListPlanAddonsParams |
| 23 | + ListPlanAddonsHandler httptransport.HandlerWithArgs[ListPlanAddonsRequest, ListPlanAddonsResponse, ListPlanAddonsParams] |
| 24 | +) |
| 25 | + |
| 26 | +func (h *handler) ListPlanAddons() ListPlanAddonsHandler { |
| 27 | + return httptransport.NewHandlerWithArgs( |
| 28 | + func(ctx context.Context, r *http.Request, params ListPlanAddonsParams) (ListPlanAddonsRequest, error) { |
| 29 | + ns, err := h.resolveNamespace(ctx) |
| 30 | + if err != nil { |
| 31 | + return ListPlanAddonsRequest{}, err |
| 32 | + } |
| 33 | + |
| 34 | + planID := chi.URLParam(r, "planId") |
| 35 | + |
| 36 | + page := pagination.NewPage(1, 20) |
| 37 | + if params.Page != nil { |
| 38 | + page = pagination.NewPage( |
| 39 | + lo.FromPtrOr(params.Page.Number, 1), |
| 40 | + lo.FromPtrOr(params.Page.Size, 20), |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + if err := page.Validate(); err != nil { |
| 45 | + return ListPlanAddonsRequest{}, apierrors.NewBadRequestError(ctx, err, apierrors.InvalidParameters{ |
| 46 | + apierrors.InvalidParameter{ |
| 47 | + Field: "page", |
| 48 | + Reason: err.Error(), |
| 49 | + Source: apierrors.InvalidParamSourceQuery, |
| 50 | + }, |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + return ListPlanAddonsRequest{ |
| 55 | + Namespaces: []string{ns}, |
| 56 | + PlanIDs: []string{planID}, |
| 57 | + Page: page, |
| 58 | + }, nil |
| 59 | + }, |
| 60 | + func(ctx context.Context, req ListPlanAddonsRequest) (ListPlanAddonsResponse, error) { |
| 61 | + result, err := h.addonService.ListPlanAddons(ctx, req) |
| 62 | + if err != nil { |
| 63 | + return ListPlanAddonsResponse{}, err |
| 64 | + } |
| 65 | + |
| 66 | + items := make([]api.PlanAddon, 0, len(result.Items)) |
| 67 | + for _, a := range result.Items { |
| 68 | + planAddon, err := fromPlanAddon(a) |
| 69 | + if err != nil { |
| 70 | + return ListPlanAddonsResponse{}, err |
| 71 | + } |
| 72 | + |
| 73 | + items = append(items, planAddon) |
| 74 | + } |
| 75 | + |
| 76 | + return response.NewPagePaginationResponse(items, response.PageMetaPage{ |
| 77 | + Size: req.Page.PageSize, |
| 78 | + Number: req.Page.PageNumber, |
| 79 | + Total: lo.ToPtr(result.TotalCount), |
| 80 | + }), nil |
| 81 | + }, |
| 82 | + commonhttp.JSONResponseEncoderWithStatus[ListPlanAddonsResponse](http.StatusOK), |
| 83 | + httptransport.AppendOptions( |
| 84 | + h.options, |
| 85 | + httptransport.WithOperationName("list-plan-addons"), |
| 86 | + httptransport.WithErrorEncoder(apierrors.GenericErrorEncoder()), |
| 87 | + )..., |
| 88 | + ) |
| 89 | +} |
0 commit comments