Skip to content

Commit

Permalink
Code for step 4
Browse files Browse the repository at this point in the history
  • Loading branch information
akoutmos committed Mar 10, 2020
1 parent 714ab4b commit 9d63bf1
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 11 deletions.
25 changes: 25 additions & 0 deletions lib/auto_finder/used_cars/used_car.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule AutoFinder.UsedCars.UsedCar do
use Ecto.Schema

import Ecto.Changeset

@fields ~w(make model year mileage price)a

@derive {Jason.Encoder, only: @fields}

schema "used_cars" do
field :make, :string
field :model, :string
field :year, :integer
field :mileage, :integer
field :price, :integer

timestamps()
end

def changeset(used_car, attrs \\ %{}) do
used_car
|> cast(attrs, @fields)
|> validate_required(@fields)
end
end
33 changes: 33 additions & 0 deletions lib/auto_finder/used_cars/used_cars.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule AutoFinder.UsedCars do
import Ecto.Query

alias AutoFinder.{Repo, UsedCars.UsedCar}

def get_used_cars(query_params) do
base_query = from(used_car in UsedCar)

query_params
|> Enum.reduce(base_query, &handle_query_param/2)
|> Repo.all()
end

defp handle_query_param({"make", make}, acc_query) do
from used_car in acc_query, where: ilike(used_car.make, ^make)
end

defp handle_query_param({"model", model}, acc_query) do
from used_car in acc_query, where: ilike(used_car.model, ^model)
end

defp handle_query_param({"min_year", min_year}, acc_query) do
from used_car in acc_query, where: used_car.year >= ^min_year
end

defp handle_query_param({"max_price", max_price}, acc_query) do
from used_car in acc_query, where: used_car.price <= ^max_price
end

defp handle_query_param({"max_mileage", max_mileage}, acc_query) do
from used_car in acc_query, where: used_car.mileage <= ^max_mileage
end
end
21 changes: 21 additions & 0 deletions lib/auto_finder_web/controllers/used_car_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule AutoFinderWeb.UsedCarController do
use AutoFinderWeb, :controller

alias AutoFinder.UsedCars

require Logger

def index(conn, params) do
results = UsedCars.get_used_cars(params)

if results == [] do
Logger.info(
"Search #{inspect(params)} yielded no results...perhaps we should suggest something similar"
)
else
Logger.info("Search #{inspect(params)} yielded some results...customer should be happy")
end

json(conn, results)
end
end
2 changes: 2 additions & 0 deletions lib/auto_finder_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ defmodule AutoFinderWeb.Router do

scope "/api", AutoFinderWeb do
pipe_through :api

get "/used_cars", UsedCarController, :index
end
end
15 changes: 15 additions & 0 deletions priv/repo/migrations/20200310044206_used_cars.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule AutoFinder.Repo.Migrations.UsedCars do
use Ecto.Migration

def change do
create table(:used_cars) do
add :make, :string
add :model, :string
add :year, :integer
add :mileage, :integer
add :price, :integer

timestamps()
end
end
end
39 changes: 28 additions & 11 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
# Script for populating the database. You can run it as:
#
# mix run priv/repo/seeds.exs
#
# Inside the script, you can read and write to any of your
# repositories directly:
#
# AutoFinder.Repo.insert!(%AutoFinder.SomeSchema{})
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
alias AutoFinder.{Repo, UsedCars.UsedCar}

car_selection = [
{"Acura", ~w(ILX TLX RLX RDX MDX NSX), 15_000..35_000},
{"Honda", ~w(Accord Civic CR-V Odyssey Passport), 10_000..25_000},
{"Nissan", ~w(GT-R 370Z Titan Leaf Sentra), 25_000..50_000},
{"Mazda", ~w(MX-5 CX-3 CX5 CX-9), 15_000..25_000},
{"Chevrolet", ~w(Camaro Corvette Colorado Silverado), 25_000..50_000},
{"Ford", ~w(Escape Explorer Mustang Focus), 15_000..25_000},
{"Audi", ~w(A4 Q3 A6 Q7 R8 S3 S4 RS5), 20_000..50_000},
{"BMW", ~w(M2 M3 M5 X4 X7), 20_000..50_000},
{"Subaru", ~w(Impreza Legacy Forester BRZ WRX), 15_000..25_000},
{"Porsche", ~w(Taycan Panamera MAcan Cayenne Carrera Cayman), 40_000..70_000},
{"Ferrari", ~w(812 F8 488 GTC4 Portofino), 150_000..250_000}
]

1..1_000
|> Enum.each(fn _ ->
{make, models, price_range} = Enum.random(car_selection)
model = Enum.random(models)
price = Enum.random(price_range)
year = Enum.random(2015..2020)
mileage = Enum.random(10_000..60_000)

%UsedCar{}
|> UsedCar.changeset(%{make: make, model: model, price: price, year: year, mileage: mileage})
|> Repo.insert!()
end)

0 comments on commit 9d63bf1

Please sign in to comment.