use new query helper
All checks were successful
Build and Publish / build-release (push) Successful in 1m25s
All checks were successful
Build and Publish / build-release (push) Successful in 1m25s
This commit is contained in:
@@ -9,38 +9,17 @@ defmodule PolicyServiceWeb.PolicyController do
|
||||
alias PolicyService.Commands.CarPolicy
|
||||
|
||||
alias PolicyServiceWeb.Schemas.Policy, as: S
|
||||
alias PolicyServiceWeb.QueryHelpers
|
||||
|
||||
tags(["Policies"])
|
||||
security([%{"bearerAuth" => []}])
|
||||
|
||||
operation(:index,
|
||||
summary: "List policies",
|
||||
parameters: [
|
||||
"page[number]": [in: :query, type: :integer, required: false],
|
||||
"page[size]": [in: :query, type: :integer, required: false],
|
||||
filters: [
|
||||
in: :query,
|
||||
required: false,
|
||||
explode: true,
|
||||
description: """
|
||||
Apply filters in Flop format.
|
||||
|
||||
Example: filters[0][field]=status&filters[0][op]==&filters[0][value]=created
|
||||
""",
|
||||
schema: %{
|
||||
type: :array,
|
||||
items: %{
|
||||
type: :object,
|
||||
properties: %{
|
||||
field: %{type: :string, description: "Field to filter on"},
|
||||
op: %{type: :string, description: "Operator (e.g., ==, !=, ~>)"},
|
||||
value: %{type: :string, description: "Filter value"}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"order_by[]": [in: :query, type: :string, required: false]
|
||||
],
|
||||
parameters: QueryHelpers.flop(
|
||||
[:status, :policy_type, :search],
|
||||
[:submitted_at, :policy_type, :status]
|
||||
),
|
||||
responses: [
|
||||
ok: {"Policy list", "application/json", S.PolicyListResponse},
|
||||
bad_request: {"Invalid params", "application/json", S.ErrorResponse}
|
||||
|
||||
33
lib/policy_service_web/query_helpers.ex
Normal file
33
lib/policy_service_web/query_helpers.ex
Normal file
@@ -0,0 +1,33 @@
|
||||
defmodule PolicyServiceWeb.QueryHelpers do
|
||||
@moduledoc false
|
||||
|
||||
alias OpenApiSpex.Schema
|
||||
|
||||
def flop(filter_fields, order_fields, other \\ []) do
|
||||
[
|
||||
filters: [in: :query, schema: filters(filter_fields)],
|
||||
order_by: [in: :query, schema: order_by(order_fields)],
|
||||
page: [in: :query, schema: %Schema{type: :number, default: 1}],
|
||||
page_size: [in: :query, schema: %Schema{type: :number, default: 20}]
|
||||
] ++ other
|
||||
end
|
||||
|
||||
defp filters(fields) do
|
||||
%Schema{
|
||||
type: :array,
|
||||
items: %Schema{
|
||||
type: :object,
|
||||
properties: %{
|
||||
field: %Schema{type: :string, enum: fields},
|
||||
value: %Schema{type: :any},
|
||||
op: %Schema{type: :string, enum: Flop.Filter.allowed_operators(:all)}
|
||||
},
|
||||
required: [:field, :value]
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
defp order_by(fields) do
|
||||
%Schema{type: :array, items: %Schema{type: :string, enum: fields}}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user