Skip to content

Commit

Permalink
applying new behaviour to api
Browse files Browse the repository at this point in the history
  • Loading branch information
brianflanagan committed Jan 14, 2025
1 parent 282ebed commit eecef8a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 66 deletions.
30 changes: 27 additions & 3 deletions lib/ppr_api_web/controllers/api/residential_sale_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@ defmodule PprApiWeb.API.ResidentialSaleController do

action_fallback PprApiWeb.FallbackController

def index(conn, _params) do
residential_sales = ResidentialSales.list_residential_sales(limit: 100)
render(conn, :index, residential_sales: residential_sales)
def index(conn, params) do
opts =
params
|> parse_params()

%{entries: entries, metadata: metadata} =
ResidentialSales.list_residential_sales(opts)

render(conn, :index, entries: entries, metadata: metadata)
end

defp parse_params(params) do
defaults = %{
"sort" => "date-desc",
"before" => nil,
"after" => nil,
"limit" => 250
}

defaults
|> Map.merge(Map.take(params, Map.keys(defaults)))
|> Enum.reject(fn
{_key, nil} -> true
{_key, value} when is_binary(value) -> String.trim(value) == ""
{_key, _value} -> false
end)
|> Enum.into(%{})
end
end
17 changes: 6 additions & 11 deletions lib/ppr_api_web/controllers/api/residential_sale_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@ defmodule PprApiWeb.API.ResidentialSaleJSON do
@doc """
Renders a list of residential_sales.
"""
def index(%{residential_sales: residential_sales}) do
%{data: for(residential_sale <- residential_sales, do: data(residential_sale))}
end

@doc """
Renders a single residential_sale.
"""
def show(%{residential_sale: residential_sale}) do
%{data: data(residential_sale)}
def index(%{entries: residential_sales, metadata: metadata}) do
%{
data: for(residential_sale <- residential_sales, do: data(residential_sale)),
metadata: metadata
}
end

defp data(%ResidentialSale{} = residential_sale) do
%{
id: residential_sale.id,
date_of_sale: residential_sale.date_of_sale,
address: residential_sale.address,
county: residential_sale.county,
eircode: residential_sale.eircode,
price_in_euros: residential_sale.price_in_euros,
price_in_euros: residential_sale.price_in_euros |> Decimal.to_string(),
not_full_market_price: residential_sale.not_full_market_price,
vat_exclusive: residential_sale.vat_exclusive,
description_of_property: residential_sale.description_of_property,
Expand Down
46 changes: 0 additions & 46 deletions lib/ppr_api_web/controllers/residential_sale_controller.ex

This file was deleted.

5 changes: 0 additions & 5 deletions lib/ppr_api_web/controllers/residential_sale_html.ex

This file was deleted.

1 change: 0 additions & 1 deletion lib/ppr_api_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ defmodule PprApiWeb.Router do

scope "/api/v1", PprApiWeb do
pipe_through :api

resources "/residential/sales", API.ResidentialSaleController
end

Expand Down

0 comments on commit eecef8a

Please sign in to comment.