diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..32ab990 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,18 @@ +{ + // Based on Elixir formatter's style + "editor.insertSpaces": true, + // Note: While it is possible to override this in your VSCode configuration, the Elixir Formatter + // does not support a configurable tab size, so if you override this then you should not use the + // formatter. + "editor.tabSize": 2, + "files.trimTrailingWhitespace": true, + "files.insertFinalNewline": true, + "files.trimFinalNewlines": true, + + // Provides smart completion for "do" and "fn ->" blocks. Does not run the Elixir formatter. + "editor.formatOnType": true, + + // Misc + "editor.wordBasedSuggestions": "off", + "editor.trimAutoWhitespace": false +} diff --git a/lib/rent_cars_web/controllers/api/category_controller.ex b/lib/rent_cars_web/controllers/api/category_controller.ex index b716928..43fb439 100644 --- a/lib/rent_cars_web/controllers/api/category_controller.ex +++ b/lib/rent_cars_web/controllers/api/category_controller.ex @@ -22,4 +22,20 @@ defmodule RentCarsWeb.Api.CategoryController do category = Categories.get_category(id) render(conn, :show, category: category) end + + def update(conn, %{"id" => id, "category" => category_params}) do + category = Categories.get_category(id) + + with {:ok, category} <- Categories.update_category(category, category_params) do + render(conn, :show, category: category) + end + end + + def delete(conn, %{"id" => id}) do + category = Categories.get_category(id) + + with {:ok, _category} <- Categories.delete_category(category) do + send_resp(conn, :no_content, "") + end + end end diff --git a/lib/rent_cars_web/router.ex b/lib/rent_cars_web/router.ex index 7f4fa5d..444e9cc 100644 --- a/lib/rent_cars_web/router.ex +++ b/lib/rent_cars_web/router.ex @@ -27,6 +27,8 @@ defmodule RentCarsWeb.Router do get "/categories", CategoryController, :index post "/categories", CategoryController, :create get "/categories/:id", CategoryController, :show + put "/categories/:id", CategoryController, :update + delete "/categories/:id", CategoryController, :delete end # Enable LiveDashboard and Swoosh mailbox preview in development diff --git a/test/rent_cars/categories.test.exs b/test/rent_cars/categories.test.exs index d9273fa..f769f6a 100644 --- a/test/rent_cars/categories.test.exs +++ b/test/rent_cars/categories.test.exs @@ -32,25 +32,25 @@ defmodule RentCars.CategoriesTest do test "get_category/1 with valid data without description" do attrs = %{description: "Car with high ground clearence", name: "SUV"} - %{:ok, category} = Categories.create_category(attrs) + {:ok, category} = Categories.create_category(attrs) assert Categories.get_category(category.id) == category end test "update_category/2" do attrs = %{description: "Car with high ground clearence", name: "SUV"} - %{:ok, category} = Categories.create_category(attrs) + {:ok, category} = Categories.create_category(attrs) - %{:ok, updated_category} = Categories.update_category(category, %{name: "truck"}) + {:ok, updated_category} = Categories.update_category(category, %{name: "truck"}) assert updated_category.name == "truck" end test "delete_category/1" do attrs = %{description: "Car with high ground clearence", name: "SUV"} - %{:ok, category} = Categories.create_category(attrs) + {:ok, category} = Categories.create_category(attrs) - %{:ok, %Category{}} = Categories.delete_category(category) + {:ok, %Category{}} = Categories.delete_category(category) assert updated_category.name == "truck" end diff --git a/test/rent_cars_web/controllers/api/category_controller_test.exs b/test/rent_cars_web/controllers/api/category_controller_test.exs index 08e4c59..e01505f 100644 --- a/test/rent_cars_web/controllers/api/category_controller_test.exs +++ b/test/rent_cars_web/controllers/api/category_controller_test.exs @@ -1,5 +1,6 @@ defmodule RentCarsWeb.Api.CategoryControllerTest do use RentCarsWeb.ConnCase + import RentCars.CategoriesFixtures test "list all categories", %{conn: conn} do conn = get(conn, Routes.api_category_path(conn, :index)) @@ -29,4 +30,47 @@ defmodule RentCarsWeb.Api.CategoryControllerTest do assert json_response(conn, 422)["errors"] == %{"name" => ["can't be blank"]} end + + describe "update category" do + setup [:create_category] + + test "update category with valid data", %{conn: conn, category: category} do + conn = + put( + conn, + Routes.api_category_path(conn, :update, category), + category: %{name: "update category name"} + ) + + assert %{"id" => id} = json_response(conn, 200)["data"] + + conn = get(conn, Routes.api_category_path(conn, :show, id)) + name = String.upcase("update category name") + + assert %{ + "id" => ^id, + "name" => ^name + } = json_response(conn, 200)["data"] + end + end + + describe "delete category" do + setup [:create_category] + + test "delete category", %{conn: conn, category: category} do + id = category.id + conn = delete(conn, Routes.api_category_path(conn, :delete, category)) + + assert response(conn, 204) + + assert_error_sent 404, fn -> + get(conn, Routes.api_category_path(conn, :show, id)) + end + end + end + + defp create_category(_) do + category = category_fixture() + %{category: category} + end end diff --git a/test/support/fixtures/categories_fixtures.ex b/test/support/fixtures/categories_fixtures.ex new file mode 100644 index 0000000..2e50e88 --- /dev/null +++ b/test/support/fixtures/categories_fixtures.ex @@ -0,0 +1,12 @@ +defmodule RentCars.CategoriesFixtures do + alias RentCars.Categories + + def category_fixture(attrs \\ %{}) do + {:ok, category} = + attrs + |> Enum.into(%{description: "some category description", name: "some category name"}) + |> Categories.create_category() + + category + end +end