Skip to content
Snippets Groups Projects
Commit c205500b authored by snaqvi's avatar snaqvi
Browse files

fixes

parent 8e288c00
No related branches found
No related tags found
1 merge request!115fixes
Pipeline #47304 passed
......@@ -32,50 +32,70 @@ defmodule ProptrackerWeb.AdvertisementController do
# Retrieve the current user if logged in
current_user = if user_id, do: Repo.get(Proptracker.Accounts.User, user_id), else: nil
# Extract filter parameters
type = Map.get(params, "type", "")
query = Map.get(params, "query", "")
location = Map.get(params, "location", "")
state = Map.get(params, "state", "")
min_price = Map.get(params, "min_price") |> parse_optional_integer()
max_price = Map.get(params, "max_price") |> parse_optional_integer()
min_rooms = Map.get(params, "min_rooms") |> parse_optional_integer()
max_rooms = Map.get(params, "max_rooms") |> parse_optional_integer()
# Query with required fields and filters
advertisements =
Advertisement
|> where([a], a.user_id == ^current_user.id) # Fetch only the current user's advertisements
|> (fn q -> if type != "", do: from(a in q, where: a.type == ^type), else: q end).()
|> (fn q -> if query != "", do: from(a in q, where: ilike(a.title, ^"%#{query}%")), else: q end).()
|> (fn q -> if location != "", do: from(a in q, where: ilike(a.location, ^"%#{location}%")), else: q end).()
|> (fn q -> if state != "", do: from(a in q, where: a.state == ^state), else: q end).()
|> (fn q -> if min_price, do: from(a in q, where: a.price >= ^min_price), else: q end).()
|> (fn q -> if max_price, do: from(a in q, where: a.price <= ^max_price), else: q end).()
|> (fn q -> if min_rooms, do: from(a in q, where: a.rooms >= ^min_rooms), else: q end).()
|> (fn q -> if max_rooms, do: from(a in q, where: a.rooms <= ^max_rooms), else: q end).()
|> where([a], a.state != "sold/rented") # Exclude 'sold/rented'
|> order_by([a], asc: a.state) # Order by state ('available' first)
|> select([a], %{
id: a.id,
title: a.title,
price: a.price,
location: a.location,
type: a.type,
rooms: a.rooms,
state: a.state
}) # Select only the required fields
|> Repo.all()
# Ensure a user is logged in
if current_user do
type = Map.get(params, "type", "")
query = Map.get(params, "query", "")
location = Map.get(params, "location", "")
state = Map.get(params, "state", "")
min_price = Map.get(params, "min_price") |> parse_optional_integer()
max_price = Map.get(params, "max_price") |> parse_optional_integer()
min_rooms = Map.get(params, "min_rooms") |> parse_optional_integer()
max_rooms = Map.get(params, "max_rooms") |> parse_optional_integer()
# Start query with current user's advertisements
advertisements_query =
Advertisement
|> where([a], a.user_id == ^current_user.id)
# Apply filters dynamically if they are not empty
advertisements_query =
advertisements_query
|> (fn q -> if type != "", do: from(a in q, where: a.type == ^type), else: q end).()
|> (fn q -> if query != "", do: from(a in q, where: ilike(a.title, ^"%#{query}%")), else: q end).()
|> (fn q -> if location != "", do: from(a in q, where: ilike(a.location, ^"%#{location}%")), else: q end).()
|> (fn q -> if state != "", do: from(a in q, where: a.state == ^state), else: q end).()
|> (fn q -> if min_price, do: from(a in q, where: a.price >= ^min_price), else: q end).()
|> (fn q -> if max_price, do: from(a in q, where: a.price <= ^max_price), else: q end).()
|> (fn q -> if min_rooms, do: from(a in q, where: a.rooms >= ^min_rooms), else: q end).()
|> (fn q -> if max_rooms, do: from(a in q, where: a.rooms <= ^max_rooms), else: q end).()
# Exclude 'sold/rented' advertisements and order by state
advertisements_query =
advertisements_query
|> where([a], a.state != "sold/rented")
|> order_by([a], asc: a.state)
# Select only the required fields, including pictures
advertisements =
advertisements_query
|> select([a], %{
id: a.id,
title: a.title,
price: a.price,
location: a.location,
type: a.type,
rooms: a.rooms,
state: a.state,
pictures: a.pictures
})
|> Repo.all()
render(conn, "index.html",
advertisements: advertisements,
current_user: current_user,
location: location,
query: query,
type: type
)
else
conn
|> put_flash(:error, "You must be logged in to view your advertisements.")
|> redirect(to: ~p"/login")
render(conn, "index.html",
advertisements: advertisements,
current_user: current_user,
location: location,
query: query,
type: type
)
end
end
# Helper function to parse optional integers
defp parse_optional_integer(value) do
case Integer.parse(value || "") do
......
......@@ -100,7 +100,7 @@
<div class="advertisement-title">Title:<%= ad.title %></div>
<div class="advertisement-type" style="color:red"><%= ad.type %></div>
<div class="advertisement-price"><%= ad.price %></div>
<div class="advertisement-location"><%= ad.location %></div>
<div class="advertisement-location">Location:<%= ad.location %></div>
<div class="advertisement-state">
<!-- Status change button -->
<span id={"ad-status-#{ad.id}"}><%= ad.state %></span>
......
......@@ -16,9 +16,9 @@
<div class="advertisement-box">
<.link navigate={~p"/advertisements/#{advertisement.id}"} class="advertisement-link">
<%= if advertisement.pictures != nil and Enum.any?(advertisement.pictures) do %>
<img src="https://media.istockphoto.com/id/1319269543/photo/new-homes-on-a-quiet-street-in-raleigh-nc.jpg?s=612x612&w=0&k=20&c=qaRMP-xgYqFAXR9aTpiG0dtkyqPhJiTAovvxyG1AxvM=" alt="Advertisement Image" class="advertisement-image-img" />
<img src={List.first(advertisement.pictures)} alt="Advertisement Image" class="advertisement-image-img" />
<% else %>
<div class="placeholder">No Image</div>
<img src="https://media.istockphoto.com/id/1319269543/photo/new-homes-on-a-quiet-street-in-raleigh-nc.jpg?s=612x612&w=0&k=20&c=qaRMP-xgYqFAXR9aTpiG0dtkyqPhJiTAovvxyG1AxvM=" alt="Advertisement Image" class="advertisement-image-img" />
<% end %>
<h3 class="advertisement-title"><%= advertisement.title %></h3>
<p class="advertisement-type"><%= advertisement.type %></p>
......
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