Skip to content

Commit 9d63bf1

Browse files
committed
Code for step 4
1 parent 714ab4b commit 9d63bf1

File tree

6 files changed

+124
-11
lines changed

6 files changed

+124
-11
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule AutoFinder.UsedCars.UsedCar do
2+
use Ecto.Schema
3+
4+
import Ecto.Changeset
5+
6+
@fields ~w(make model year mileage price)a
7+
8+
@derive {Jason.Encoder, only: @fields}
9+
10+
schema "used_cars" do
11+
field :make, :string
12+
field :model, :string
13+
field :year, :integer
14+
field :mileage, :integer
15+
field :price, :integer
16+
17+
timestamps()
18+
end
19+
20+
def changeset(used_car, attrs \\ %{}) do
21+
used_car
22+
|> cast(attrs, @fields)
23+
|> validate_required(@fields)
24+
end
25+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
defmodule AutoFinder.UsedCars do
2+
import Ecto.Query
3+
4+
alias AutoFinder.{Repo, UsedCars.UsedCar}
5+
6+
def get_used_cars(query_params) do
7+
base_query = from(used_car in UsedCar)
8+
9+
query_params
10+
|> Enum.reduce(base_query, &handle_query_param/2)
11+
|> Repo.all()
12+
end
13+
14+
defp handle_query_param({"make", make}, acc_query) do
15+
from used_car in acc_query, where: ilike(used_car.make, ^make)
16+
end
17+
18+
defp handle_query_param({"model", model}, acc_query) do
19+
from used_car in acc_query, where: ilike(used_car.model, ^model)
20+
end
21+
22+
defp handle_query_param({"min_year", min_year}, acc_query) do
23+
from used_car in acc_query, where: used_car.year >= ^min_year
24+
end
25+
26+
defp handle_query_param({"max_price", max_price}, acc_query) do
27+
from used_car in acc_query, where: used_car.price <= ^max_price
28+
end
29+
30+
defp handle_query_param({"max_mileage", max_mileage}, acc_query) do
31+
from used_car in acc_query, where: used_car.mileage <= ^max_mileage
32+
end
33+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule AutoFinderWeb.UsedCarController do
2+
use AutoFinderWeb, :controller
3+
4+
alias AutoFinder.UsedCars
5+
6+
require Logger
7+
8+
def index(conn, params) do
9+
results = UsedCars.get_used_cars(params)
10+
11+
if results == [] do
12+
Logger.info(
13+
"Search #{inspect(params)} yielded no results...perhaps we should suggest something similar"
14+
)
15+
else
16+
Logger.info("Search #{inspect(params)} yielded some results...customer should be happy")
17+
end
18+
19+
json(conn, results)
20+
end
21+
end

lib/auto_finder_web/router.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ defmodule AutoFinderWeb.Router do
77

88
scope "/api", AutoFinderWeb do
99
pipe_through :api
10+
11+
get "/used_cars", UsedCarController, :index
1012
end
1113
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule AutoFinder.Repo.Migrations.UsedCars do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:used_cars) do
6+
add :make, :string
7+
add :model, :string
8+
add :year, :integer
9+
add :mileage, :integer
10+
add :price, :integer
11+
12+
timestamps()
13+
end
14+
end
15+
end

priv/repo/seeds.exs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
# Script for populating the database. You can run it as:
2-
#
3-
# mix run priv/repo/seeds.exs
4-
#
5-
# Inside the script, you can read and write to any of your
6-
# repositories directly:
7-
#
8-
# AutoFinder.Repo.insert!(%AutoFinder.SomeSchema{})
9-
#
10-
# We recommend using the bang functions (`insert!`, `update!`
11-
# and so on) as they will fail if something goes wrong.
1+
alias AutoFinder.{Repo, UsedCars.UsedCar}
2+
3+
car_selection = [
4+
{"Acura", ~w(ILX TLX RLX RDX MDX NSX), 15_000..35_000},
5+
{"Honda", ~w(Accord Civic CR-V Odyssey Passport), 10_000..25_000},
6+
{"Nissan", ~w(GT-R 370Z Titan Leaf Sentra), 25_000..50_000},
7+
{"Mazda", ~w(MX-5 CX-3 CX5 CX-9), 15_000..25_000},
8+
{"Chevrolet", ~w(Camaro Corvette Colorado Silverado), 25_000..50_000},
9+
{"Ford", ~w(Escape Explorer Mustang Focus), 15_000..25_000},
10+
{"Audi", ~w(A4 Q3 A6 Q7 R8 S3 S4 RS5), 20_000..50_000},
11+
{"BMW", ~w(M2 M3 M5 X4 X7), 20_000..50_000},
12+
{"Subaru", ~w(Impreza Legacy Forester BRZ WRX), 15_000..25_000},
13+
{"Porsche", ~w(Taycan Panamera MAcan Cayenne Carrera Cayman), 40_000..70_000},
14+
{"Ferrari", ~w(812 F8 488 GTC4 Portofino), 150_000..250_000}
15+
]
16+
17+
1..1_000
18+
|> Enum.each(fn _ ->
19+
{make, models, price_range} = Enum.random(car_selection)
20+
model = Enum.random(models)
21+
price = Enum.random(price_range)
22+
year = Enum.random(2015..2020)
23+
mileage = Enum.random(10_000..60_000)
24+
25+
%UsedCar{}
26+
|> UsedCar.changeset(%{make: make, model: model, price: price, year: year, mileage: mileage})
27+
|> Repo.insert!()
28+
end)

0 commit comments

Comments
 (0)