Skip to content
Snippets Groups Projects
Commit 34129b22 authored by Kerdo Kurs's avatar Kerdo Kurs
Browse files

add implementation for similar properties #28

parent c6ae7755
No related branches found
No related tags found
1 merge request!19Resolve "FR-18: Display Similar Properties on Listing View"
Pipeline #45369 passed
......@@ -57,10 +57,13 @@ defmodule PropTrackrWeb.PropertiesController do
)
end
similar_properties = get_similar_properties(property)
render(conn, "show.html",
property: property,
can_edit: can_edit,
favorites: favorites
favorites: favorites,
similar_properties: similar_properties,
)
end
end
......@@ -181,4 +184,20 @@ defmodule PropTrackrWeb.PropertiesController do
end
end
end
defp get_similar_properties(property) do
Repo.all(
from p in Property,
where: p.state == :available
and p.id != ^property.id,
order_by: [
desc: fragment("abs(? - ?)", p.price, ^property.price),
desc: fragment("abs(? - ?)", p.room_count, ^property.room_count),
desc: fragment("abs(? - ?)", p.area, ^property.area),
desc: fragment("abs(? - ?)", p.floor, ^property.floor)
],
limit: 5,
select: p
)
end
end
......@@ -103,4 +103,30 @@
<p class="text-sm text-gray-600">Email: <%= @property.user.email %></p>
</div>
</div>
<div class="bg-white shadow rounded-lg p-6">
<h3 class="text-lg font-medium text-gray-900">Similar properties</h3>
<div class="mt-4">
<%= if @similar_properties == [] do %>
<p class="text-sm text-gray-600">No similar properties found</p>
<% else %>
<%= for property <- @similar_properties do %>
<div class="flex items-center justify-between mt-4">
<div>
<p class="text-sm text-gray-600"><%= property.title %></p>
<p class="text-sm text-gray-600"><%= :erlang.float_to_binary(property.price, decimals: 2) %></p>
<p class="text-sm text-gray-600"><%= property.location %></p>
</div>
<a
href={~p"/properties/#{property.reference}"}
class="text-sm text-blue-600 hover:underline"
id={"view-similar-property-#{property.reference}"}
>
View
</a>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment