Create user

Create profile
added some validation
This commit is contained in:
2023-02-17 08:29:33 +00:00
parent 57dae2bee3
commit bb1f5d74d8
19 changed files with 797 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
defmodule PhoenixApiTemplate.Accounts do
@moduledoc """
The Accounts context.
"""
import Ecto.Query, warn: false
alias PhoenixApiTemplate.Repo
alias PhoenixApiTemplate.Accounts.User
@doc """
Returns the list of users.
## Examples
iex> list_users()
[%User{}, ...]
"""
def list_users do
Repo.all(User)
end
@doc """
Gets a single user.
Raises `Ecto.NoResultsError` if the User does not exist.
## Examples
iex> get_user!(123)
%User{}
iex> get_user!(456)
** (Ecto.NoResultsError)
"""
def get_user!(id), do: Repo.get!(User, id)
@doc """
Creates a user.
## Examples
iex> create_user(%{field: value})
{:ok, %User{}}
iex> create_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_user(attrs \\ %{}) do
%User{}
|> User.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a user.
## Examples
iex> update_user(user, %{field: new_value})
{:ok, %User{}}
iex> update_user(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_user(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a user.
## Examples
iex> delete_user(user)
{:ok, %User{}}
iex> delete_user(user)
{:error, %Ecto.Changeset{}}
"""
def delete_user(%User{} = user) do
Repo.delete(user)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking user changes.
## Examples
iex> change_user(user)
%Ecto.Changeset{data: %User{}}
"""
def change_user(%User{} = user, attrs \\ %{}) do
User.changeset(user, attrs)
end
end

View File

@@ -0,0 +1,24 @@
defmodule PhoenixApiTemplate.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "users" do
field(:email, :string)
field(:hashed_password, :string)
has_one(:profile, PhoenixApiTemplate.Profiles.Profile)
timestamps()
end
@doc false
def changeset(user, attrs) do
user
|> cast(attrs, [:email, :hashed_password])
|> validate_required([:email, :hashed_password])
|> unique_constraint(:email)
|> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must have the @ sign and no spaces")
|> validate_length(:email, max: 250)
end
end

View File

@@ -0,0 +1,104 @@
defmodule PhoenixApiTemplate.Profiles do
@moduledoc """
The Profiles context.
"""
import Ecto.Query, warn: false
alias PhoenixApiTemplate.Repo
alias PhoenixApiTemplate.Profiles.Profile
@doc """
Returns the list of profiles.
## Examples
iex> list_profiles()
[%Profile{}, ...]
"""
def list_profiles do
Repo.all(Profile)
end
@doc """
Gets a single profile.
Raises `Ecto.NoResultsError` if the Profile does not exist.
## Examples
iex> get_profile!(123)
%Profile{}
iex> get_profile!(456)
** (Ecto.NoResultsError)
"""
def get_profile!(id), do: Repo.get!(Profile, id)
@doc """
Creates a profile.
## Examples
iex> create_profile(%{field: value})
{:ok, %Profile{}}
iex> create_profile(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_profile(attrs \\ %{}) do
%Profile{}
|> Profile.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a profile.
## Examples
iex> update_profile(profile, %{field: new_value})
{:ok, %Profile{}}
iex> update_profile(profile, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_profile(%Profile{} = profile, attrs) do
profile
|> Profile.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a profile.
## Examples
iex> delete_profile(profile)
{:ok, %Profile{}}
iex> delete_profile(profile)
{:error, %Ecto.Changeset{}}
"""
def delete_profile(%Profile{} = profile) do
Repo.delete(profile)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking profile changes.
## Examples
iex> change_profile(profile)
%Ecto.Changeset{data: %Profile{}}
"""
def change_profile(%Profile{} = profile, attrs \\ %{}) do
Profile.changeset(profile, attrs)
end
end

View File

@@ -0,0 +1,20 @@
defmodule PhoenixApiTemplate.Profiles.Profile do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "profiles" do
field(:display_name, :string)
belongs_to(:user, PhoenixApiTemplate.Accounts.User)
timestamps()
end
@doc false
def changeset(profile, attrs) do
profile
|> cast(attrs, [:display_name, :user_id])
|> validate_required([:display_name, :user_id])
end
end

View File

@@ -0,0 +1,24 @@
defmodule PhoenixApiTemplateWeb.FallbackController do
@moduledoc """
Translates controller action results into valid `Plug.Conn` responses.
See `Phoenix.Controller.action_fallback/1` for more details.
"""
use PhoenixApiTemplateWeb, :controller
# This clause handles errors returned by Ecto's insert/update/delete.
def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
conn
|> put_status(:unprocessable_entity)
|> put_view(PhoenixApiTemplateWeb.ChangesetView)
|> render("error.json", changeset: changeset)
end
# This clause is an example of how to handle resources that cannot be found.
def call(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)
|> put_view(PhoenixApiTemplateWeb.ErrorView)
|> render(:"404")
end
end

View File

@@ -0,0 +1,43 @@
defmodule PhoenixApiTemplateWeb.ProfileController do
use PhoenixApiTemplateWeb, :controller
alias PhoenixApiTemplate.Profiles
alias PhoenixApiTemplate.Profiles.Profile
action_fallback PhoenixApiTemplateWeb.FallbackController
def index(conn, _params) do
profiles = Profiles.list_profiles()
render(conn, "index.json", profiles: profiles)
end
def create(conn, %{"profile" => profile_params}) do
with {:ok, %Profile{} = profile} <- Profiles.create_profile(profile_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.profile_path(conn, :show, profile))
|> render("show.json", profile: profile)
end
end
def show(conn, %{"id" => id}) do
profile = Profiles.get_profile!(id)
render(conn, "show.json", profile: profile)
end
def update(conn, %{"id" => id, "profile" => profile_params}) do
profile = Profiles.get_profile!(id)
with {:ok, %Profile{} = profile} <- Profiles.update_profile(profile, profile_params) do
render(conn, "show.json", profile: profile)
end
end
def delete(conn, %{"id" => id}) do
profile = Profiles.get_profile!(id)
with {:ok, %Profile{}} <- Profiles.delete_profile(profile) do
send_resp(conn, :no_content, "")
end
end
end

View File

@@ -0,0 +1,42 @@
defmodule PhoenixApiTemplateWeb.UserController do
use PhoenixApiTemplateWeb, :controller
alias PhoenixApiTemplate.Accounts
alias PhoenixApiTemplate.Accounts.User
action_fallback(PhoenixApiTemplateWeb.FallbackController)
def index(conn, _params) do
users = Accounts.list_users()
render(conn, "index.json", users: users)
end
def create(conn, %{"user" => user_params}) do
with {:ok, %User{} = user} <- Accounts.create_user(user_params) do
conn
|> put_status(:created)
|> render("show.json", user: user)
end
end
def show(conn, %{"id" => id}) do
user = Accounts.get_user!(id)
render(conn, "show.json", user: user)
end
def update(conn, %{"id" => id, "user" => user_params}) do
user = Accounts.get_user!(id)
with {:ok, %User{} = user} <- Accounts.update_user(user, user_params) do
render(conn, "show.json", user: user)
end
end
def delete(conn, %{"id" => id}) do
user = Accounts.get_user!(id)
with {:ok, %User{}} <- Accounts.delete_user(user) do
send_resp(conn, :no_content, "")
end
end
end

View File

@@ -0,0 +1,19 @@
defmodule PhoenixApiTemplateWeb.ChangesetView do
use PhoenixApiTemplateWeb, :view
@doc """
Traverses and translates changeset errors.
See `Ecto.Changeset.traverse_errors/2` and
`PhoenixApiTemplateWeb.ErrorHelpers.translate_error/1` for more details.
"""
def translate_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
end
def render("error.json", %{changeset: changeset}) do
# When encoded, the changeset returns its errors
# as a JSON object. So we just pass it forward.
%{errors: translate_errors(changeset)}
end
end

View File

@@ -0,0 +1,19 @@
defmodule PhoenixApiTemplateWeb.ProfileView do
use PhoenixApiTemplateWeb, :view
alias PhoenixApiTemplateWeb.ProfileView
def render("index.json", %{profiles: profiles}) do
%{data: render_many(profiles, ProfileView, "profile.json")}
end
def render("show.json", %{profile: profile}) do
%{data: render_one(profile, ProfileView, "profile.json")}
end
def render("profile.json", %{profile: profile}) do
%{
id: profile.id,
display_name: profile.display_name
}
end
end

View File

@@ -0,0 +1,20 @@
defmodule PhoenixApiTemplateWeb.UserView do
use PhoenixApiTemplateWeb, :view
alias PhoenixApiTemplateWeb.UserView
def render("index.json", %{users: users}) do
%{data: render_many(users, UserView, "user.json")}
end
def render("show.json", %{user: user}) do
%{data: render_one(user, UserView, "user.json")}
end
def render("user.json", %{user: user}) do
%{
id: user.id,
email: user.email,
hashed_password: user.hashed_password
}
end
end

View File

@@ -0,0 +1,15 @@
defmodule PhoenixApiTemplate.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users, primary_key: false) do
add :id, :binary_id, primary_key: true
add :email, :string
add :hashed_password, :string
timestamps()
end
create unique_index(:users, [:email])
end
end

View File

@@ -0,0 +1,15 @@
defmodule PhoenixApiTemplate.Repo.Migrations.CreateProfiles do
use Ecto.Migration
def change do
create table(:profiles, primary_key: false) do
add(:id, :binary_id, primary_key: true)
add(:display_name, :string)
add(:user_id, references(:users, on_delete: :delete_all, type: :binary_id))
timestamps()
end
create(index(:profiles, [:user_id, :display_name]))
end
end

View File

@@ -0,0 +1,61 @@
defmodule PhoenixApiTemplate.AccountsTest do
use PhoenixApiTemplate.DataCase
alias PhoenixApiTemplate.Accounts
describe "users" do
alias PhoenixApiTemplate.Accounts.User
import PhoenixApiTemplate.AccountsFixtures
@invalid_attrs %{email: nil, hashed_password: nil}
test "list_users/0 returns all users" do
user = user_fixture()
assert Accounts.list_users() == [user]
end
test "get_user!/1 returns the user with given id" do
user = user_fixture()
assert Accounts.get_user!(user.id) == user
end
test "create_user/1 with valid data creates a user" do
valid_attrs = %{email: "some email", hashed_password: "some hashed_password"}
assert {:ok, %User{} = user} = Accounts.create_user(valid_attrs)
assert user.email == "some email"
assert user.hashed_password == "some hashed_password"
end
test "create_user/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Accounts.create_user(@invalid_attrs)
end
test "update_user/2 with valid data updates the user" do
user = user_fixture()
update_attrs = %{email: "some updated email", hashed_password: "some updated hashed_password"}
assert {:ok, %User{} = user} = Accounts.update_user(user, update_attrs)
assert user.email == "some updated email"
assert user.hashed_password == "some updated hashed_password"
end
test "update_user/2 with invalid data returns error changeset" do
user = user_fixture()
assert {:error, %Ecto.Changeset{}} = Accounts.update_user(user, @invalid_attrs)
assert user == Accounts.get_user!(user.id)
end
test "delete_user/1 deletes the user" do
user = user_fixture()
assert {:ok, %User{}} = Accounts.delete_user(user)
assert_raise Ecto.NoResultsError, fn -> Accounts.get_user!(user.id) end
end
test "change_user/1 returns a user changeset" do
user = user_fixture()
assert %Ecto.Changeset{} = Accounts.change_user(user)
end
end
end

View File

@@ -0,0 +1,59 @@
defmodule PhoenixApiTemplate.ProfilesTest do
use PhoenixApiTemplate.DataCase
alias PhoenixApiTemplate.Profiles
describe "profiles" do
alias PhoenixApiTemplate.Profiles.Profile
import PhoenixApiTemplate.ProfilesFixtures
@invalid_attrs %{display_name: nil}
test "list_profiles/0 returns all profiles" do
profile = profile_fixture()
assert Profiles.list_profiles() == [profile]
end
test "get_profile!/1 returns the profile with given id" do
profile = profile_fixture()
assert Profiles.get_profile!(profile.id) == profile
end
test "create_profile/1 with valid data creates a profile" do
valid_attrs = %{display_name: "some display_name"}
assert {:ok, %Profile{} = profile} = Profiles.create_profile(valid_attrs)
assert profile.display_name == "some display_name"
end
test "create_profile/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Profiles.create_profile(@invalid_attrs)
end
test "update_profile/2 with valid data updates the profile" do
profile = profile_fixture()
update_attrs = %{display_name: "some updated display_name"}
assert {:ok, %Profile{} = profile} = Profiles.update_profile(profile, update_attrs)
assert profile.display_name == "some updated display_name"
end
test "update_profile/2 with invalid data returns error changeset" do
profile = profile_fixture()
assert {:error, %Ecto.Changeset{}} = Profiles.update_profile(profile, @invalid_attrs)
assert profile == Profiles.get_profile!(profile.id)
end
test "delete_profile/1 deletes the profile" do
profile = profile_fixture()
assert {:ok, %Profile{}} = Profiles.delete_profile(profile)
assert_raise Ecto.NoResultsError, fn -> Profiles.get_profile!(profile.id) end
end
test "change_profile/1 returns a profile changeset" do
profile = profile_fixture()
assert %Ecto.Changeset{} = Profiles.change_profile(profile)
end
end
end

View File

@@ -0,0 +1,84 @@
defmodule PhoenixApiTemplateWeb.ProfileControllerTest do
use PhoenixApiTemplateWeb.ConnCase
import PhoenixApiTemplate.ProfilesFixtures
alias PhoenixApiTemplate.Profiles.Profile
@create_attrs %{
display_name: "some display_name"
}
@update_attrs %{
display_name: "some updated display_name"
}
@invalid_attrs %{display_name: nil}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all profiles", %{conn: conn} do
conn = get(conn, Routes.profile_path(conn, :index))
assert json_response(conn, 200)["data"] == []
end
end
describe "create profile" do
test "renders profile when data is valid", %{conn: conn} do
conn = post(conn, Routes.profile_path(conn, :create), profile: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, Routes.profile_path(conn, :show, id))
assert %{
"id" => ^id,
"display_name" => "some display_name"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.profile_path(conn, :create), profile: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update profile" do
setup [:create_profile]
test "renders profile when data is valid", %{conn: conn, profile: %Profile{id: id} = profile} do
conn = put(conn, Routes.profile_path(conn, :update, profile), profile: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, Routes.profile_path(conn, :show, id))
assert %{
"id" => ^id,
"display_name" => "some updated display_name"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, profile: profile} do
conn = put(conn, Routes.profile_path(conn, :update, profile), profile: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete profile" do
setup [:create_profile]
test "deletes chosen profile", %{conn: conn, profile: profile} do
conn = delete(conn, Routes.profile_path(conn, :delete, profile))
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, Routes.profile_path(conn, :show, profile))
end
end
end
defp create_profile(_) do
profile = profile_fixture()
%{profile: profile}
end
end

View File

@@ -0,0 +1,88 @@
defmodule PhoenixApiTemplateWeb.UserControllerTest do
use PhoenixApiTemplateWeb.ConnCase
import PhoenixApiTemplate.AccountsFixtures
alias PhoenixApiTemplate.Accounts.User
@create_attrs %{
email: "some email",
hashed_password: "some hashed_password"
}
@update_attrs %{
email: "some updated email",
hashed_password: "some updated hashed_password"
}
@invalid_attrs %{email: nil, hashed_password: nil}
setup %{conn: conn} do
{:ok, conn: put_req_header(conn, "accept", "application/json")}
end
describe "index" do
test "lists all users", %{conn: conn} do
conn = get(conn, Routes.user_path(conn, :index))
assert json_response(conn, 200)["data"] == []
end
end
describe "create user" do
test "renders user when data is valid", %{conn: conn} do
conn = post(conn, Routes.user_path(conn, :create), user: @create_attrs)
assert %{"id" => id} = json_response(conn, 201)["data"]
conn = get(conn, Routes.user_path(conn, :show, id))
assert %{
"id" => ^id,
"email" => "some email",
"hashed_password" => "some hashed_password"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.user_path(conn, :create), user: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "update user" do
setup [:create_user]
test "renders user when data is valid", %{conn: conn, user: %User{id: id} = user} do
conn = put(conn, Routes.user_path(conn, :update, user), user: @update_attrs)
assert %{"id" => ^id} = json_response(conn, 200)["data"]
conn = get(conn, Routes.user_path(conn, :show, id))
assert %{
"id" => ^id,
"email" => "some updated email",
"hashed_password" => "some updated hashed_password"
} = json_response(conn, 200)["data"]
end
test "renders errors when data is invalid", %{conn: conn, user: user} do
conn = put(conn, Routes.user_path(conn, :update, user), user: @invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
end
describe "delete user" do
setup [:create_user]
test "deletes chosen user", %{conn: conn, user: user} do
conn = delete(conn, Routes.user_path(conn, :delete, user))
assert response(conn, 204)
assert_error_sent 404, fn ->
get(conn, Routes.user_path(conn, :show, user))
end
end
end
defp create_user(_) do
user = user_fixture()
%{user: user}
end
end

View File

@@ -0,0 +1,26 @@
defmodule PhoenixApiTemplate.AccountsFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `PhoenixApiTemplate.Accounts` context.
"""
@doc """
Generate a unique user email.
"""
def unique_user_email, do: "some email#{System.unique_integer([:positive])}"
@doc """
Generate a user.
"""
def user_fixture(attrs \\ %{}) do
{:ok, user} =
attrs
|> Enum.into(%{
email: unique_user_email(),
hashed_password: "some hashed_password"
})
|> PhoenixApiTemplate.Accounts.create_user()
user
end
end

View File

@@ -0,0 +1,20 @@
defmodule PhoenixApiTemplate.ProfilesFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `PhoenixApiTemplate.Profiles` context.
"""
@doc """
Generate a profile.
"""
def profile_fixture(attrs \\ %{}) do
{:ok, profile} =
attrs
|> Enum.into(%{
display_name: "some display_name"
})
|> PhoenixApiTemplate.Profiles.create_profile()
profile
end
end

View File

@@ -0,0 +1,10 @@
POST http://localhost:4000/api/users HTTP/1.1
content-type: application/json
{
"quotation": {
"author": "Dennis Ritchie",
"text": "The only way to learn a new programming language is by writing programs in it"
}
}