-
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.
gen html to create schema for todo #1
- Loading branch information
1 parent
c6f5fe6
commit 99a62bd
Showing
14 changed files
with
520 additions
and
4 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 App.Ctx do | ||
@moduledoc """ | ||
The Ctx context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias App.Repo | ||
|
||
alias App.Ctx.Todo | ||
|
||
@doc """ | ||
Returns the list of todos. | ||
## Examples | ||
iex> list_todos() | ||
[%Todo{}, ...] | ||
""" | ||
def list_todos do | ||
Repo.all(Todo) | ||
end | ||
|
||
@doc """ | ||
Gets a single todo. | ||
Raises `Ecto.NoResultsError` if the Todo does not exist. | ||
## Examples | ||
iex> get_todo!(123) | ||
%Todo{} | ||
iex> get_todo!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_todo!(id), do: Repo.get!(Todo, id) | ||
|
||
@doc """ | ||
Creates a todo. | ||
## Examples | ||
iex> create_todo(%{field: value}) | ||
{:ok, %Todo{}} | ||
iex> create_todo(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_todo(attrs \\ %{}) do | ||
%Todo{} | ||
|> Todo.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a todo. | ||
## Examples | ||
iex> update_todo(todo, %{field: new_value}) | ||
{:ok, %Todo{}} | ||
iex> update_todo(todo, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_todo(%Todo{} = todo, attrs) do | ||
todo | ||
|> Todo.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a Todo. | ||
## Examples | ||
iex> delete_todo(todo) | ||
{:ok, %Todo{}} | ||
iex> delete_todo(todo) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_todo(%Todo{} = todo) do | ||
Repo.delete(todo) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking todo changes. | ||
## Examples | ||
iex> change_todo(todo) | ||
%Ecto.Changeset{source: %Todo{}} | ||
""" | ||
def change_todo(%Todo{} = todo) do | ||
Todo.changeset(todo, %{}) | ||
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,24 @@ | ||
defmodule App.Ctx.Todo do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "todos" do | ||
field :assignee, :string | ||
field :deadline, :naive_datetime | ||
field :owner, :string | ||
field :priority, :integer | ||
field :schedule, :naive_datetime | ||
field :status, :string | ||
field :time_estimate, :integer | ||
field :title, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(todo, attrs) do | ||
todo | ||
|> cast(attrs, [:title, :status, :priority, :time_estimate, :deadline, :schedule, :assignee, :owner]) | ||
|> validate_required([:title, :status, :priority, :time_estimate, :deadline, :schedule, :assignee, :owner]) | ||
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,62 @@ | ||
defmodule AppWeb.TodoController do | ||
use AppWeb, :controller | ||
|
||
alias App.Ctx | ||
alias App.Ctx.Todo | ||
|
||
def index(conn, _params) do | ||
todos = Ctx.list_todos() | ||
render(conn, "index.html", todos: todos) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = Ctx.change_todo(%Todo{}) | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"todo" => todo_params}) do | ||
case Ctx.create_todo(todo_params) do | ||
{:ok, todo} -> | ||
conn | ||
|> put_flash(:info, "Todo created successfully.") | ||
|> redirect(to: Routes.todo_path(conn, :show, todo)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "new.html", changeset: changeset) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
todo = Ctx.get_todo!(id) | ||
render(conn, "show.html", todo: todo) | ||
end | ||
|
||
def edit(conn, %{"id" => id}) do | ||
todo = Ctx.get_todo!(id) | ||
changeset = Ctx.change_todo(todo) | ||
render(conn, "edit.html", todo: todo, changeset: changeset) | ||
end | ||
|
||
def update(conn, %{"id" => id, "todo" => todo_params}) do | ||
todo = Ctx.get_todo!(id) | ||
|
||
case Ctx.update_todo(todo, todo_params) do | ||
{:ok, todo} -> | ||
conn | ||
|> put_flash(:info, "Todo updated successfully.") | ||
|> redirect(to: Routes.todo_path(conn, :show, todo)) | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, "edit.html", todo: todo, changeset: changeset) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
todo = Ctx.get_todo!(id) | ||
{:ok, _todo} = Ctx.delete_todo(todo) | ||
|
||
conn | ||
|> put_flash(:info, "Todo deleted successfully.") | ||
|> redirect(to: Routes.todo_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
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 Todo</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.todo_path(@conn, :update, @todo)) %> | ||
|
||
<span><%= link "Back", to: Routes.todo_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,43 @@ | ||
<%= 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, :title %> | ||
<%= text_input f, :title %> | ||
<%= error_tag f, :title %> | ||
|
||
<%= label f, :status %> | ||
<%= text_input f, :status %> | ||
<%= error_tag f, :status %> | ||
|
||
<%= label f, :priority %> | ||
<%= number_input f, :priority %> | ||
<%= error_tag f, :priority %> | ||
|
||
<%= label f, :time_estimate %> | ||
<%= number_input f, :time_estimate %> | ||
<%= error_tag f, :time_estimate %> | ||
|
||
<%= label f, :deadline %> | ||
<%= datetime_select f, :deadline %> | ||
<%= error_tag f, :deadline %> | ||
|
||
<%= label f, :schedule %> | ||
<%= datetime_select f, :schedule %> | ||
<%= error_tag f, :schedule %> | ||
|
||
<%= label f, :assignee %> | ||
<%= text_input f, :assignee %> | ||
<%= error_tag f, :assignee %> | ||
|
||
<%= label f, :owner %> | ||
<%= text_input f, :owner %> | ||
<%= error_tag f, :owner %> | ||
|
||
<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,40 @@ | ||
<h1>Listing Todos</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Title</th> | ||
<th>Status</th> | ||
<th>Priority</th> | ||
<th>Time estimate</th> | ||
<th>Deadline</th> | ||
<th>Schedule</th> | ||
<th>Assignee</th> | ||
<th>Owner</th> | ||
|
||
<th></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%= for todo <- @todos do %> | ||
<tr> | ||
<td><%= todo.title %></td> | ||
<td><%= todo.status %></td> | ||
<td><%= todo.priority %></td> | ||
<td><%= todo.time_estimate %></td> | ||
<td><%= todo.deadline %></td> | ||
<td><%= todo.schedule %></td> | ||
<td><%= todo.assignee %></td> | ||
<td><%= todo.owner %></td> | ||
|
||
<td> | ||
<%= link "Show", to: Routes.todo_path(@conn, :show, todo) %> | ||
<%= link "Edit", to: Routes.todo_path(@conn, :edit, todo) %> | ||
<%= link "Delete", to: Routes.todo_path(@conn, :delete, todo), method: :delete, data: [confirm: "Are you sure?"] %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<span><%= link "New Todo", to: Routes.todo_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 Todo</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.todo_path(@conn, :create)) %> | ||
|
||
<span><%= link "Back", to: Routes.todo_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,48 @@ | ||
<h1>Show Todo</h1> | ||
|
||
<ul> | ||
|
||
<li> | ||
<strong>Title:</strong> | ||
<%= @todo.title %> | ||
</li> | ||
|
||
<li> | ||
<strong>Status:</strong> | ||
<%= @todo.status %> | ||
</li> | ||
|
||
<li> | ||
<strong>Priority:</strong> | ||
<%= @todo.priority %> | ||
</li> | ||
|
||
<li> | ||
<strong>Time estimate:</strong> | ||
<%= @todo.time_estimate %> | ||
</li> | ||
|
||
<li> | ||
<strong>Deadline:</strong> | ||
<%= @todo.deadline %> | ||
</li> | ||
|
||
<li> | ||
<strong>Schedule:</strong> | ||
<%= @todo.schedule %> | ||
</li> | ||
|
||
<li> | ||
<strong>Assignee:</strong> | ||
<%= @todo.assignee %> | ||
</li> | ||
|
||
<li> | ||
<strong>Owner:</strong> | ||
<%= @todo.owner %> | ||
</li> | ||
|
||
</ul> | ||
|
||
<span><%= link "Edit", to: Routes.todo_path(@conn, :edit, @todo) %></span> | ||
<span><%= link "Back", to: Routes.todo_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 AppWeb.TodoView do | ||
use AppWeb, :view | ||
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,19 @@ | ||
defmodule App.Repo.Migrations.CreateTodos do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:todos) do | ||
add :title, :string | ||
add :status, :string | ||
add :priority, :integer | ||
add :time_estimate, :integer | ||
add :deadline, :naive_datetime | ||
add :schedule, :naive_datetime | ||
add :assignee, :string | ||
add :owner, :string | ||
|
||
timestamps() | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.