Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.1.7 create update phoenix api #4

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions lib/rent_cars_web/controllers/api/category_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/rent_cars_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/rent_cars/categories.test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions test/rent_cars_web/controllers/api/category_controller_test.exs
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -29,4 +30,47 @@

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

Check failure on line 60 in test/rent_cars_web/controllers/api/category_controller_test.exs

View workflow job for this annotation

GitHub Actions / check_security (1.14, 26)

test delete category delete category (RentCarsWeb.Api.CategoryControllerTest)
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
12 changes: 12 additions & 0 deletions test/support/fixtures/categories_fixtures.ex
Original file line number Diff line number Diff line change
@@ -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
Loading