-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
132 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule AutoFinderLivedashboard.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule AutoFinderLivedashboard.UsedCars do | ||
import Ecto.Query | ||
|
||
alias AutoFinderLivedashboard.{Repo, UsedCars.UsedCar} | ||
|
||
@event_name [:auto_finder_livedashboard, :query] | ||
|
||
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 | ||
:telemetry.execute(@event_name, %{count: 1}, %{filter: "make"}) | ||
from used_car in acc_query, where: ilike(used_car.make, ^make) | ||
end | ||
|
||
defp handle_query_param({"model", model}, acc_query) do | ||
:telemetry.execute(@event_name, %{count: 1}, %{filter: "model"}) | ||
from used_car in acc_query, where: ilike(used_car.model, ^model) | ||
end | ||
|
||
defp handle_query_param({"min_year", min_year}, acc_query) do | ||
:telemetry.execute(@event_name, %{count: 1}, %{filter: "min_year"}) | ||
from used_car in acc_query, where: used_car.year >= ^min_year | ||
end | ||
|
||
defp handle_query_param({"max_price", max_price}, acc_query) do | ||
:telemetry.execute(@event_name, %{count: 1}, %{filter: "max_price"}) | ||
from used_car in acc_query, where: used_car.price <= ^max_price | ||
end | ||
|
||
defp handle_query_param({"max_mileage", max_mileage}, acc_query) do | ||
:telemetry.execute(@event_name, %{count: 1}, %{filter: "max_mileage"}) | ||
from used_car in acc_query, where: used_car.mileage <= ^max_mileage | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
lib/auto_finder_livedashboard_web/controllers/used_car_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule AutoFinderLivedashboardWeb.UsedCarController do | ||
use AutoFinderLivedashboardWeb, :controller | ||
|
||
alias AutoFinderLivedashboard.UsedCars | ||
|
||
require Logger | ||
|
||
def index(conn, params) do | ||
results = UsedCars.get_used_cars(params) | ||
|
||
json(conn, results) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule AutoFinderLivedashboard.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
# | ||
# AutoFinderLivedashboard.Repo.insert!(%AutoFinderLivedashboard.SomeSchema{}) | ||
# | ||
# We recommend using the bang functions (`insert!`, `update!` | ||
# and so on) as they will fail if something goes wrong. | ||
alias AutoFinderLivedashboard.{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) |