Skip to content

Commit

Permalink
mix phx.gen.html Ctx Sent sent ... dwyl/aws-ses-lambda#3
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Feb 24, 2020
1 parent 04f3b62 commit b8d4b06
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ we need to create the **`sent`** schema:
mix phx.gen.html Ctx Sent sent message_id:string person_id:references:people request_id:string status_id:references:status template:string
```

When you run this command in your terminal,
you should see the following output:

```
* creating lib/app_web/controllers/sent_controller.ex
* creating lib/app_web/templates/sent/edit.html.eex
* creating lib/app_web/templates/sent/form.html.eex
* creating lib/app_web/templates/sent/index.html.eex
* creating lib/app_web/templates/sent/new.html.eex
* creating lib/app_web/templates/sent/show.html.eex
* creating lib/app_web/views/sent_view.ex
* creating test/app_web/controllers/sent_controller_test.exs
* creating lib/app/ctx/sent.ex
* creating priv/repo/migrations/20200224224024_create_sent.exs
* creating lib/app/ctx.ex
* injecting lib/app/ctx.ex
* creating test/app/ctx_test.exs
* injecting test/app/ctx_test.exs
Add the resource to your browser scope in lib/app_web/router.ex:
resources "/sent", SentController
Remember to update your repository by running migrations:
$ mix ecto.migrate
```

We will follow these instructions in the next step!


In case you are wondering what the **`message_id`** and **`request_id`** fields
Expand Down
104 changes: 104 additions & 0 deletions lib/app/ctx.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
defmodule App.Ctx do
@moduledoc """
The Ctx context.
"""

import Ecto.Query, warn: false
alias App.Repo

alias App.Ctx.Sent

@doc """
Returns the list of sent.
## Examples
iex> list_sent()
[%Sent{}, ...]
"""
def list_sent do
Repo.all(Sent)
end

@doc """
Gets a single sent.
Raises `Ecto.NoResultsError` if the Sent does not exist.
## Examples
iex> get_sent!(123)
%Sent{}
iex> get_sent!(456)
** (Ecto.NoResultsError)
"""
def get_sent!(id), do: Repo.get!(Sent, id)

@doc """
Creates a sent.
## Examples
iex> create_sent(%{field: value})
{:ok, %Sent{}}
iex> create_sent(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_sent(attrs \\ %{}) do
%Sent{}
|> Sent.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a sent.
## Examples
iex> update_sent(sent, %{field: new_value})
{:ok, %Sent{}}
iex> update_sent(sent, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_sent(%Sent{} = sent, attrs) do
sent
|> Sent.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a sent.
## Examples
iex> delete_sent(sent)
{:ok, %Sent{}}
iex> delete_sent(sent)
{:error, %Ecto.Changeset{}}
"""
def delete_sent(%Sent{} = sent) do
Repo.delete(sent)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking sent changes.
## Examples
iex> change_sent(sent)
%Ecto.Changeset{source: %Sent{}}
"""
def change_sent(%Sent{} = sent) do
Sent.changeset(sent, %{})
end
end
21 changes: 21 additions & 0 deletions lib/app/ctx/sent.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule App.Ctx.Sent do
use Ecto.Schema
import Ecto.Changeset

schema "sent" do
field :message_id, :string
field :request_id, :string
field :template, :string
field :person_id, :id
field :status_id, :id

timestamps()
end

@doc false
def changeset(sent, attrs) do
sent
|> cast(attrs, [:message_id, :request_id, :template])
|> validate_required([:message_id, :request_id, :template])
end
end
62 changes: 62 additions & 0 deletions lib/app_web/controllers/sent_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule AppWeb.SentController do
use AppWeb, :controller

alias App.Ctx
alias App.Ctx.Sent

def index(conn, _params) do
sent = Ctx.list_sent()
render(conn, "index.html", sent: sent)
end

def new(conn, _params) do
changeset = Ctx.change_sent(%Sent{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"sent" => sent_params}) do
case Ctx.create_sent(sent_params) do
{:ok, sent} ->
conn
|> put_flash(:info, "Sent created successfully.")
|> redirect(to: Routes.sent_path(conn, :show, sent))

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end

def show(conn, %{"id" => id}) do
sent = Ctx.get_sent!(id)
render(conn, "show.html", sent: sent)
end

def edit(conn, %{"id" => id}) do
sent = Ctx.get_sent!(id)
changeset = Ctx.change_sent(sent)
render(conn, "edit.html", sent: sent, changeset: changeset)
end

def update(conn, %{"id" => id, "sent" => sent_params}) do
sent = Ctx.get_sent!(id)

case Ctx.update_sent(sent, sent_params) do
{:ok, sent} ->
conn
|> put_flash(:info, "Sent updated successfully.")
|> redirect(to: Routes.sent_path(conn, :show, sent))

{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", sent: sent, changeset: changeset)
end
end

def delete(conn, %{"id" => id}) do
sent = Ctx.get_sent!(id)
{:ok, _sent} = Ctx.delete_sent(sent)

conn
|> put_flash(:info, "Sent deleted successfully.")
|> redirect(to: Routes.sent_path(conn, :index))
end
end
5 changes: 5 additions & 0 deletions lib/app_web/templates/sent/edit.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Edit Sent</h1>

<%= render "form.html", Map.put(assigns, :action, Routes.sent_path(@conn, :update, @sent)) %>

<span><%= link "Back", to: Routes.sent_path(@conn, :index) %></span>
23 changes: 23 additions & 0 deletions lib/app_web/templates/sent/form.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%= 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, :message_id %>
<%= text_input f, :message_id %>
<%= error_tag f, :message_id %>

<%= label f, :request_id %>
<%= text_input f, :request_id %>
<%= error_tag f, :request_id %>

<%= label f, :template %>
<%= text_input f, :template %>
<%= error_tag f, :template %>

<div>
<%= submit "Save" %>
</div>
<% end %>
30 changes: 30 additions & 0 deletions lib/app_web/templates/sent/index.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<h1>Listing Sent</h1>

<table>
<thead>
<tr>
<th>Message</th>
<th>Request</th>
<th>Template</th>

<th></th>
</tr>
</thead>
<tbody>
<%= for sent <- @sent do %>
<tr>
<td><%= sent.message_id %></td>
<td><%= sent.request_id %></td>
<td><%= sent.template %></td>

<td>
<span><%= link "Show", to: Routes.sent_path(@conn, :show, sent) %></span>
<span><%= link "Edit", to: Routes.sent_path(@conn, :edit, sent) %></span>
<span><%= link "Delete", to: Routes.sent_path(@conn, :delete, sent), method: :delete, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>

<span><%= link "New Sent", to: Routes.sent_path(@conn, :new) %></span>
5 changes: 5 additions & 0 deletions lib/app_web/templates/sent/new.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Sent</h1>

<%= render "form.html", Map.put(assigns, :action, Routes.sent_path(@conn, :create)) %>

<span><%= link "Back", to: Routes.sent_path(@conn, :index) %></span>
23 changes: 23 additions & 0 deletions lib/app_web/templates/sent/show.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>Show Sent</h1>

<ul>

<li>
<strong>Message:</strong>
<%= @sent.message_id %>
</li>

<li>
<strong>Request:</strong>
<%= @sent.request_id %>
</li>

<li>
<strong>Template:</strong>
<%= @sent.template %>
</li>

</ul>

<span><%= link "Edit", to: Routes.sent_path(@conn, :edit, @sent) %></span>
<span><%= link "Back", to: Routes.sent_path(@conn, :index) %></span>
3 changes: 3 additions & 0 deletions lib/app_web/views/sent_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule AppWeb.SentView do
use AppWeb, :view
end
18 changes: 18 additions & 0 deletions priv/repo/migrations/20200224224024_create_sent.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule App.Repo.Migrations.CreateSent do
use Ecto.Migration

def change do
create table(:sent) do
add :message_id, :string
add :request_id, :string
add :template, :string
add :person_id, references(:people, on_delete: :nothing)
add :status_id, references(:status, on_delete: :nothing)

timestamps()
end

create index(:sent, [:person_id])
create index(:sent, [:status_id])
end
end
Loading

0 comments on commit b8d4b06

Please sign in to comment.