-
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…
- Loading branch information
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
Oops, something went wrong.