Skip to content

Commit

Permalink
gen html to create schema for todo #1
Browse files Browse the repository at this point in the history
  • Loading branch information
RobStallion committed Jun 5, 2019
1 parent c6f5fe6 commit 99a62bd
Show file tree
Hide file tree
Showing 14 changed files with 520 additions and 4 deletions.
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.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
24 changes: 24 additions & 0 deletions lib/app/ctx/todo.ex
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
62 changes: 62 additions & 0 deletions lib/app_web/controllers/todo_controller.ex
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
1 change: 1 addition & 0 deletions lib/app_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule AppWeb.Router do
pipe_through :browser

get "/", PageController, :index
resources "/todos", TodoController
end

# Other scopes may use custom stacks.
Expand Down
5 changes: 5 additions & 0 deletions lib/app_web/templates/todo/edit.html.eex
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>
43 changes: 43 additions & 0 deletions lib/app_web/templates/todo/form.html.eex
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 %>
40 changes: 40 additions & 0 deletions lib/app_web/templates/todo/index.html.eex
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>
5 changes: 5 additions & 0 deletions lib/app_web/templates/todo/new.html.eex
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>
48 changes: 48 additions & 0 deletions lib/app_web/templates/todo/show.html.eex
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>
3 changes: 3 additions & 0 deletions lib/app_web/views/todo_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule AppWeb.TodoView do
use AppWeb, :view
end
4 changes: 0 additions & 4 deletions priv/repo/migrations/.formatter.exs

This file was deleted.

19 changes: 19 additions & 0 deletions priv/repo/migrations/20190605093633_create_todos.exs
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
Loading

0 comments on commit 99a62bd

Please sign in to comment.