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.4.1 upload user photo #35

Merged
merged 9 commits into from
Jan 12, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ npm-debug.log
mix.lock

.elixir_ls/*
uploads
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ config :rent_cars, RentCars.Mailer, adapter: Swoosh.Adapters.Local
config :money,
default_currency: :USD

config :waffle,
storage: Waffle.Storage.Local

# Configure esbuild (the version is required)
config :esbuild,
version: "0.17.11",
Expand Down
9 changes: 8 additions & 1 deletion lib/rent_cars/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ defmodule RentCars.Accounts do
|> Repo.update()
end

def get_user(id) do
def get_user!(id) do
Repo.get(User, id)
end

def upload_photo(user_id, photo) do
user_id
|> get_user!()
|> User.update_photo(%{avatar: photo})
|> Repo.update()
end
end
19 changes: 19 additions & 0 deletions lib/rent_cars/accounts/avatar.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule RentCars.Accounts.Avatar do
use Waffle.Definition
use Waffle.Ecto.Definition

@extension_whitelist ~w(.jpg .jpeg .png)

def validate({file, _}) do
file_extension = file.file_name |> Path.extname() |> String.downcase()

case Enum.member?(@extension_whitelist, file_extension) do
true -> :ok
false -> {:error, "file type is invalid"}
end
end

def storage_dir(_, {_file, user}) do
"uploads/accounts/users/#{user.id}"
end
end
8 changes: 8 additions & 0 deletions lib/rent_cars/accounts/user.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule RentCars.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
alias RentCars.Accounts.Avatar
alias RentCars.Rentals.Rental
import Waffle.Ecto.Schema

@role_values ~w/USER ADMIN/a
@fields ~w/role/a
Expand All @@ -18,6 +20,8 @@
field :email, :string
field :drive_license, :string
field :role, Ecto.Enum, values: @role_values, default: :USER
field :avatar, Avatar.Type

has_many :rentals, Rental

timestamps()
Expand Down Expand Up @@ -46,7 +50,7 @@
end

defp hash_password(%{valid?: true, changes: %{password: password}} = changeset) do
change(changeset, Argon2.add_hash(password))

Check warning on line 53 in lib/rent_cars/accounts/user.ex

View workflow job for this annotation

GitHub Actions / check_security (1.14, 26)

Argon2.add_hash/1 is deprecated. This function will be removed in the next major version.

Check warning on line 53 in lib/rent_cars/accounts/user.ex

View workflow job for this annotation

GitHub Actions / check_security (1.14, 26)

Argon2.add_hash/1 is deprecated. This function will be removed in the next major version.

Check warning on line 53 in lib/rent_cars/accounts/user.ex

View workflow job for this annotation

GitHub Actions / check_security (1.14, 26)

Argon2.add_hash/1 is deprecated. This function will be removed in the next major version.
end

defp hash_password(changeset), do: changeset
Expand All @@ -54,4 +58,8 @@
def update_user(user, params) do
changeset(user, params)
end

def update_photo(user, params) do
cast_attachments(user, params, [:avatar])
end
end
12 changes: 11 additions & 1 deletion lib/rent_cars_web/controllers/api/user_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ defmodule RentCarsWeb.Api.UserController do
end

def show(conn, %{"id" => id}) do
user = Accounts.get_user(id)
user = Accounts.get_user!(id)
render(conn, :show, user: user)
end

def upload_photo(conn, %{"avatar" => params}) do
[user_id] = get_req_header(conn, "user_id")

with {:ok, user} <- Accounts.upload_photo(user_id, params) do
conn
|> put_status(:created)
|> render(:show, user: user)
end
end

# def update(conn, %{"id" => id, "user" => user_params}) do
# user = Accounts.get_user(id)

Expand Down
3 changes: 3 additions & 0 deletions lib/rent_cars_web/controllers/api/user_json.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule RentCarsWeb.Api.UserJSON do
alias RentCars.Accounts.Avatar
alias RentCars.Accounts.User
# If you want to customize a particular status code,
# you may add your own clauses, such as:
Expand All @@ -23,6 +24,8 @@ defmodule RentCarsWeb.Api.UserJSON do
user_name: user.user_name,
email: user.email,
drive_license: user.drive_license,
role: user.role,
avatar: Avatar.url({user.avatar, user}),
inserted_at: user.inserted_at,
updated_at: user.updated_at
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rent_cars_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule RentCarsWeb.Router do
pipe_through :authenticated
post "/session/me", SessionController, :me
get "/users/:id", UserController, :show

patch "/users/photo", UserController, :upload_photo
post "/rentals", RentalController, :create
post "/rentals/return/:id", RentalController, :return
get "/rentals", RentalController, :index
Expand Down
4 changes: 3 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ defmodule RentCars.MixProject do
{:argon2_elixir, "~> 4.0"},
{:phoenix_swoosh, "~> 1.0"},
{:money, "~> 1.12"},
{:timex, "~> 3.0"}
{:timex, "~> 3.0"},
{:waffle, "~> 1.1"},
{:waffle_ecto, "~> 0.0.11"}
]
end

Expand Down
9 changes: 9 additions & 0 deletions priv/repo/migrations/20240112052738_add_user_photo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule RentCars.Repo.Migrations.AddUserPhoto do
use Ecto.Migration

def change do
alter table(:users) do
add :avatar, :string
end
end
end
17 changes: 16 additions & 1 deletion test/rent_cars/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@ defmodule RentCars.AccountsTest do
alias RentCars.Accounts
import RentCars.UserFixtures

describe "upload avatar" do
test "upload avatar image" do
user = user_fixture()

photo = %Plug.Upload{
content_type: "image/jpg",
filename: "1.jpg",
path: "test/support/fixtures/1.jpg"
}

assert {:ok, user_updated} = Accounts.upload_photo(user.id, photo)
assert user_updated.avatar.file_name == "1.jpg"
end
end

describe "get user" do
test "get_user/1" do
user = user_fixture()

assert Accounts.get_user(user.id).email == user.email
assert Accounts.get_user!(user.id).email == user.email
end
end

Expand Down
12 changes: 12 additions & 0 deletions test/rent_cars_web/controllers/api/user_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ defmodule RentCarsWeb.Api.AccountControllerTest do
"email" => ^email
} = json_response(conn, 200)["data"]
end

test "upload user image", %{conn: conn, user: _user} do
photo = %Plug.Upload{
content_type: "image/jpg",
filename: "1.jpg",
path: "test/support/fixtures/1.jpg"
}

conn = patch(conn, Routes.api_user_path(conn, :upload_photo), avatar: photo)

assert json_response(conn, 201)["data"]["avatar"] |> String.contains?("1.jpg")
end
end
Binary file added test/support/fixtures/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading