Skip to content

Commit

Permalink
- Added car list api (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
rushikeshpandit authored Jan 11, 2024
1 parent b70159a commit dd75853
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/rent_cars/rentals.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
defmodule RentCars.Rentals do
import Ecto.Query
alias __MODULE__.CreateRental
alias RentCars.Rentals.Rental
alias RentCars.Repo

def create(payload) do
%{
Expand All @@ -10,4 +13,11 @@ defmodule RentCars.Rentals do

CreateRental.execute(car_id, expected_return_date, user_id)
end

def list_rentals(user_id) do
Rental
|> where([r], r.user_id == ^user_id)
|> preload(:car)
|> Repo.all()
end
end
8 changes: 8 additions & 0 deletions lib/rent_cars_web/controllers/api/rental_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ defmodule RentCarsWeb.Api.RentalController do
|> render(:show, rental: rental)
end
end

def index(conn, _params) do
[user_id] = get_req_header(conn, "user_id")
rentals = Rentals.list_rentals(user_id)

conn
|> render(:index, rentals: rentals)
end
end
6 changes: 6 additions & 0 deletions lib/rent_cars_web/controllers/api/rental_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ defmodule RentCarsWeb.Api.RentalJSON do
%{data: data(rental)}
end

def index(%{rentals: rentals}) do
%{
data: Enum.map(rentals, &data/1)
}
end

def data(%Rental{} = rental) do
%{
id: rental.id,
Expand Down
2 changes: 2 additions & 0 deletions lib/rent_cars_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ defmodule RentCarsWeb.Router do
pipe_through :authenticated
post "/session/me", SessionController, :me
get "/users/:id", UserController, :show

post "/rentals", RentalController, :create
get "/rentals", RentalController, :index
end

post "/users", UserController, :create
Expand Down
18 changes: 16 additions & 2 deletions test/rent_cars_web/controllers/api/rental_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
defmodule RentCarsWeb.Api.RentalControllerTest do
use RentCarsWeb.ConnCase
import RentCars.CarsFixtures
import RentCars.RentalsFixtures

describe "handle with sessions" do
describe "rentals test" do
setup :include_normal_user_token

test "create session", %{conn: conn} do
test "rent a car", %{conn: conn} do
car = car_fixture()

payload = %{
Expand All @@ -17,6 +18,19 @@ defmodule RentCarsWeb.Api.RentalControllerTest do

assert json_response(conn, 201)["data"]["car"]["id"] == car.id
end

test "list all cars booked", %{conn: conn, user: user} do
car = car_fixture()
rental_fixture(%{user_id: user.id, car_id: car.id})

conn = get(conn, Routes.api_rental_path(conn, :index))

car_id = car.id

assert %{
"car" => %{"data" => %{"id" => ^car_id}}
} = json_response(conn, 200)["data"] |> hd()
end
end

defp create_expected_return_date do
Expand Down

0 comments on commit dd75853

Please sign in to comment.