Skip to content

Commit 1946372

Browse files
committed
Allow property lookup by slug
1 parent 58b5389 commit 1946372

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

app/controllers/api_public/v1/properties_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ def show
77
locale = params[:locale] || I18n.default_locale
88
I18n.locale = locale
99
# Use listed_properties (materialized view) instead of deprecated props
10-
property = Pwb::Current.website.listed_properties.find(params[:id])
10+
scope = Pwb::Current.website.listed_properties
11+
property = scope.find_by(slug: params[:id]) || scope.find_by(id: params[:id])
12+
raise ActiveRecord::RecordNotFound unless property
1113
render json: property.as_json
1214
rescue ActiveRecord::RecordNotFound
1315
render json: { error: "Property not found" }, status: :not_found

spec/requests/api_public/v1/properties_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
expect(json["id"]).to eq(realty_asset.id)
2222
end
2323

24+
it "returns the property by slug" do
25+
host! 'properties-test.example.com'
26+
get "/api_public/v1/properties/#{realty_asset.slug}"
27+
expect(response).to have_http_status(200)
28+
json = response.parsed_body
29+
expect(json["id"]).to eq(realty_asset.id)
30+
end
31+
2432
it "returns 404 for non-existent property" do
2533
host! 'properties-test.example.com'
2634
get "/api_public/v1/properties/00000000-0000-0000-0000-000000000000"

0 commit comments

Comments
 (0)