-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add address files afer running mix phx.gen.html command see: dwyl/pho…
Showing
15 changed files
with
472 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
defmodule Append.Accounts do | ||
@moduledoc """ | ||
The Accounts context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias Append.Repo | ||
|
||
alias Append.Accounts.Address | ||
|
||
@doc """ | ||
Returns the list of addresses. | ||
## Examples | ||
iex> list_addresses() | ||
[%Address{}, ...] | ||
""" | ||
def list_addresses do | ||
Repo.all(Address) | ||
end | ||
|
||
@doc """ | ||
Gets a single address. | ||
Raises `Ecto.NoResultsError` if the Address does not exist. | ||
## Examples | ||
iex> get_address!(123) | ||
%Address{} | ||
iex> get_address!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_address!(id), do: Repo.get!(Address, id) | ||
|
||
@doc """ | ||
Creates a address. | ||
## Examples | ||
iex> create_address(%{field: value}) | ||
{:ok, %Address{}} | ||
iex> create_address(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_address(attrs \\ %{}) do | ||
%Address{} | ||
|> Address.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a address. | ||
## Examples | ||
iex> update_address(address, %{field: new_value}) | ||
{:ok, %Address{}} | ||
iex> update_address(address, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_address(%Address{} = address, attrs) do | ||
address | ||
|> Address.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a Address. | ||
## Examples | ||
iex> delete_address(address) | ||
{:ok, %Address{}} | ||
iex> delete_address(address) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_address(%Address{} = address) do | ||
Repo.delete(address) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking address changes. | ||
## Examples | ||
iex> change_address(address) | ||
%Ecto.Changeset{source: %Address{}} | ||
""" | ||
def change_address(%Address{} = address) do | ||
Address.changeset(address, %{}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Append.Accounts.Address do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "addresses" do | ||
field :address_line_1, :string | ||
field :address_line_2, :string | ||
field :city, :string | ||
field :name, :string | ||
field :postcode, :string | ||
field :tel, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(address, attrs) do | ||
address | ||
|> cast(attrs, [:name, :address_line_1, :address_line_2, :city, :postcode, :tel]) | ||
|> validate_required([:name, :address_line_1, :address_line_2, :city, :postcode, :tel]) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
defmodule AppendWeb.AddressController do | ||
use AppendWeb, :controller | ||
|
||
alias Append.Accounts | ||
alias Append.Accounts.Address | ||
|
||
def index(conn, _params) do | ||
addresses = Accounts.list_addresses() | ||
render(conn, "index.html", addresses: addresses) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = Accounts.change_address(%Address{}) | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"address" => address_params}) do | ||
case Accounts.create_address(address_params) do | ||
{:ok, address} -> | ||
conn | ||
|> put_flash(:info, "Address created successfully.") | ||
|> redirect(to: Routes.address_path(conn, :show, address)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
address = Accounts.get_address!(id) | ||
render(conn, "show.html", address: address) | ||
end | ||
|
||
def edit(conn, %{"id" => id}) do | ||
address = Accounts.get_address!(id) | ||
changeset = Accounts.change_address(address) | ||
render(conn, "edit.html", address: address, changeset: changeset) | ||
end | ||
|
||
def update(conn, %{"id" => id, "address" => address_params}) do | ||
address = Accounts.get_address!(id) | ||
|
||
case Accounts.update_address(address, address_params) do | ||
{:ok, address} -> | ||
conn | ||
|> put_flash(:info, "Address updated successfully.") | ||
|> redirect(to: Routes.address_path(conn, :show, address)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "edit.html", address: address, changeset: changeset) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
address = Accounts.get_address!(id) | ||
{:ok, _address} = Accounts.delete_address(address) | ||
|
||
conn | ||
|> put_flash(:info, "Address deleted successfully.") | ||
|> redirect(to: Routes.address_path(conn, :index)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>Edit Address</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.address_path(@conn, :update, @address)) %> | ||
|
||
<span><%= link "Back", to: Routes.address_path(@conn, :index) %></span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<%= form_for @changeset, @action, fn f -> %> | ||
<%= if @changeset.action do %> | ||
<div class="alert alert-danger"> | ||
<p>Oops, something went wrong! Please check the errors below.</p> | ||
</div> | ||
<% end %> | ||
|
||
<%= label f, :name %> | ||
<%= text_input f, :name %> | ||
<%= error_tag f, :name %> | ||
|
||
<%= label f, :address_line_1 %> | ||
<%= text_input f, :address_line_1 %> | ||
<%= error_tag f, :address_line_1 %> | ||
|
||
<%= label f, :address_line_2 %> | ||
<%= text_input f, :address_line_2 %> | ||
<%= error_tag f, :address_line_2 %> | ||
|
||
<%= label f, :city %> | ||
<%= text_input f, :city %> | ||
<%= error_tag f, :city %> | ||
|
||
<%= label f, :postcode %> | ||
<%= text_input f, :postcode %> | ||
<%= error_tag f, :postcode %> | ||
|
||
<%= label f, :tel %> | ||
<%= text_input f, :tel %> | ||
<%= error_tag f, :tel %> | ||
|
||
<div> | ||
<%= submit "Save" %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<h1>Listing Addresses</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Address line 1</th> | ||
<th>Address line 2</th> | ||
<th>City</th> | ||
<th>Postcode</th> | ||
<th>Tel</th> | ||
|
||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= for address <- @addresses do %> | ||
<tr> | ||
<td><%= address.name %></td> | ||
<td><%= address.address_line_1 %></td> | ||
<td><%= address.address_line_2 %></td> | ||
<td><%= address.city %></td> | ||
<td><%= address.postcode %></td> | ||
<td><%= address.tel %></td> | ||
|
||
<td> | ||
<%= link "Show", to: Routes.address_path(@conn, :show, address) %> | ||
<%= link "Edit", to: Routes.address_path(@conn, :edit, address) %> | ||
<%= link "Delete", to: Routes.address_path(@conn, :delete, address), method: :delete, data: [confirm: "Are you sure?"] %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<span><%= link "New Address", to: Routes.address_path(@conn, :new) %></span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>New Address</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.address_path(@conn, :create)) %> | ||
|
||
<span><%= link "Back", to: Routes.address_path(@conn, :index) %></span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<h1>Show Address</h1> | ||
|
||
<ul> | ||
|
||
<li> | ||
<strong>Name:</strong> | ||
<%= @address.name %> | ||
</li> | ||
|
||
<li> | ||
<strong>Address line 1:</strong> | ||
<%= @address.address_line_1 %> | ||
</li> | ||
|
||
<li> | ||
<strong>Address line 2:</strong> | ||
<%= @address.address_line_2 %> | ||
</li> | ||
|
||
<li> | ||
<strong>City:</strong> | ||
<%= @address.city %> | ||
</li> | ||
|
||
<li> | ||
<strong>Postcode:</strong> | ||
<%= @address.postcode %> | ||
</li> | ||
|
||
<li> | ||
<strong>Tel:</strong> | ||
<%= @address.tel %> | ||
</li> | ||
|
||
</ul> | ||
|
||
<span><%= link "Edit", to: Routes.address_path(@conn, :edit, @address) %></span> | ||
<span><%= link "Back", to: Routes.address_path(@conn, :index) %></span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule AppendWeb.AddressView do | ||
use AppendWeb, :view | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
defmodule Append.AccountsTest do | ||
use Append.DataCase | ||
|
||
alias Append.Accounts | ||
|
||
describe "addresses" do | ||
alias Append.Accounts.Address | ||
|
||
@valid_attrs %{address_line_1: "some address_line_1", address_line_2: "some address_line_2", city: "some city", name: "some name", postcode: "some postcode", tel: "some tel"} | ||
@update_attrs %{address_line_1: "some updated address_line_1", address_line_2: "some updated address_line_2", city: "some updated city", name: "some updated name", postcode: "some updated postcode", tel: "some updated tel"} | ||
@invalid_attrs %{address_line_1: nil, address_line_2: nil, city: nil, name: nil, postcode: nil, tel: nil} | ||
|
||
def address_fixture(attrs \\ %{}) do | ||
{:ok, address} = | ||
attrs | ||
|> Enum.into(@valid_attrs) | ||
|> Accounts.create_address() | ||
|
||
address | ||
end | ||
|
||
test "list_addresses/0 returns all addresses" do | ||
address = address_fixture() | ||
assert Accounts.list_addresses() == [address] | ||
end | ||
|
||
test "get_address!/1 returns the address with given id" do | ||
address = address_fixture() | ||
assert Accounts.get_address!(address.id) == address | ||
end | ||
|
||
test "create_address/1 with valid data creates a address" do | ||
assert {:ok, %Address{} = address} = Accounts.create_address(@valid_attrs) | ||
assert address.address_line_1 == "some address_line_1" | ||
assert address.address_line_2 == "some address_line_2" | ||
assert address.city == "some city" | ||
assert address.name == "some name" | ||
assert address.postcode == "some postcode" | ||
assert address.tel == "some tel" | ||
end | ||
|
||
test "create_address/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = Accounts.create_address(@invalid_attrs) | ||
end | ||
|
||
test "update_address/2 with valid data updates the address" do | ||
address = address_fixture() | ||
assert {:ok, %Address{} = address} = Accounts.update_address(address, @update_attrs) | ||
assert address.address_line_1 == "some updated address_line_1" | ||
assert address.address_line_2 == "some updated address_line_2" | ||
assert address.city == "some updated city" | ||
assert address.name == "some updated name" | ||
assert address.postcode == "some updated postcode" | ||
assert address.tel == "some updated tel" | ||
end | ||
|
||
test "update_address/2 with invalid data returns error changeset" do | ||
address = address_fixture() | ||
assert {:error, %Ecto.Changeset{}} = Accounts.update_address(address, @invalid_attrs) | ||
assert address == Accounts.get_address!(address.id) | ||
end | ||
|
||
test "delete_address/1 deletes the address" do | ||
address = address_fixture() | ||
assert {:ok, %Address{}} = Accounts.delete_address(address) | ||
assert_raise Ecto.NoResultsError, fn -> Accounts.get_address!(address.id) end | ||
end | ||
|
||
test "change_address/1 returns a address changeset" do | ||
address = address_fixture() | ||
assert %Ecto.Changeset{} = Accounts.change_address(address) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
defmodule AppendWeb.AddressControllerTest do | ||
use AppendWeb.ConnCase | ||
|
||
alias Append.Accounts | ||
|
||
@create_attrs %{address_line_1: "some address_line_1", address_line_2: "some address_line_2", city: "some city", name: "some name", postcode: "some postcode", tel: "some tel"} | ||
@update_attrs %{address_line_1: "some updated address_line_1", address_line_2: "some updated address_line_2", city: "some updated city", name: "some updated name", postcode: "some updated postcode", tel: "some updated tel"} | ||
@invalid_attrs %{address_line_1: nil, address_line_2: nil, city: nil, name: nil, postcode: nil, tel: nil} | ||
|
||
def fixture(:address) do | ||
{:ok, address} = Accounts.create_address(@create_attrs) | ||
address | ||
end | ||
|
||
describe "index" do | ||
test "lists all addresses", %{conn: conn} do | ||
conn = get(conn, Routes.address_path(conn, :index)) | ||
assert html_response(conn, 200) =~ "Listing Addresses" | ||
end | ||
end | ||
|
||
describe "new address" do | ||
test "renders form", %{conn: conn} do | ||
conn = get(conn, Routes.address_path(conn, :new)) | ||
assert html_response(conn, 200) =~ "New Address" | ||
end | ||
end | ||
|
||
describe "create address" do | ||
test "redirects to show when data is valid", %{conn: conn} do | ||
conn = post(conn, Routes.address_path(conn, :create), address: @create_attrs) | ||
|
||
assert %{id: id} = redirected_params(conn) | ||
assert redirected_to(conn) == Routes.address_path(conn, :show, id) | ||
|
||
conn = get(conn, Routes.address_path(conn, :show, id)) | ||
assert html_response(conn, 200) =~ "Show Address" | ||
end | ||
|
||
test "renders errors when data is invalid", %{conn: conn} do | ||
conn = post(conn, Routes.address_path(conn, :create), address: @invalid_attrs) | ||
assert html_response(conn, 200) =~ "New Address" | ||
end | ||
end | ||
|
||
describe "edit address" do | ||
setup [:create_address] | ||
|
||
test "renders form for editing chosen address", %{conn: conn, address: address} do | ||
conn = get(conn, Routes.address_path(conn, :edit, address)) | ||
assert html_response(conn, 200) =~ "Edit Address" | ||
end | ||
end | ||
|
||
describe "update address" do | ||
setup [:create_address] | ||
|
||
test "redirects when data is valid", %{conn: conn, address: address} do | ||
conn = put(conn, Routes.address_path(conn, :update, address), address: @update_attrs) | ||
assert redirected_to(conn) == Routes.address_path(conn, :show, address) | ||
|
||
conn = get(conn, Routes.address_path(conn, :show, address)) | ||
assert html_response(conn, 200) =~ "some updated address_line_1" | ||
end | ||
|
||
test "renders errors when data is invalid", %{conn: conn, address: address} do | ||
conn = put(conn, Routes.address_path(conn, :update, address), address: @invalid_attrs) | ||
assert html_response(conn, 200) =~ "Edit Address" | ||
end | ||
end | ||
|
||
describe "delete address" do | ||
setup [:create_address] | ||
|
||
test "deletes chosen address", %{conn: conn, address: address} do | ||
conn = delete(conn, Routes.address_path(conn, :delete, address)) | ||
assert redirected_to(conn) == Routes.address_path(conn, :index) | ||
assert_error_sent 404, fn -> | ||
get(conn, Routes.address_path(conn, :show, address)) | ||
end | ||
end | ||
end | ||
|
||
defp create_address(_) do | ||
address = fixture(:address) | ||
{:ok, address: address} | ||
end | ||
end |