Skip to content

Commit

Permalink
add example project
Browse files Browse the repository at this point in the history
  • Loading branch information
bluzky committed Oct 25, 2020
1 parent f142ae5 commit ae5a297
Show file tree
Hide file tree
Showing 34 changed files with 927 additions and 21 deletions.
12 changes: 11 additions & 1 deletion example/lib/example/content/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Example.Content.Post do
field(:slug, :string)
field(:state, :string, default: "draft")
field(:title, :string)
field(:view_count, :integer, default: 0)
belongs_to(:category, Example.PostMeta.Category)
belongs_to(:author, Example.Account.User)

Expand All @@ -19,7 +20,16 @@ defmodule Example.Content.Post do
@doc false
def changeset(post, attrs) do
post
|> cast(attrs, [:title, :slug, :cover, :content, :state, :author_id, :category_id])
|> cast(attrs, [
:title,
:slug,
:cover,
:content,
:state,
:author_id,
:category_id,
:view_count
])
|> validate_required([:title, :content, :state])
end
end
1 change: 1 addition & 0 deletions example/lib/example_web/controllers/.#post_controller.ex
62 changes: 62 additions & 0 deletions example/lib/example_web/controllers/category_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule ExampleWeb.CategoryController do
use ExampleWeb, :controller

alias Example.PostMeta
alias Example.PostMeta.Category

def index(conn, _params) do
categories = PostMeta.list_categories()
render(conn, "index.html", categories: categories)
end

def new(conn, _params) do
changeset = PostMeta.change_category(%Category{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"category" => category_params}) do
case PostMeta.create_category(category_params) do
{:ok, category} ->
conn
|> put_flash(:info, "Category created successfully.")
|> redirect(to: Routes.category_path(conn, :show, category))

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

def show(conn, %{"id" => id}) do
category = PostMeta.get_category!(id)
render(conn, "show.html", category: category)
end

def edit(conn, %{"id" => id}) do
category = PostMeta.get_category!(id)
changeset = PostMeta.change_category(category)
render(conn, "edit.html", category: category, changeset: changeset)
end

def update(conn, %{"id" => id, "category" => category_params}) do
category = PostMeta.get_category!(id)

case PostMeta.update_category(category, category_params) do
{:ok, category} ->
conn
|> put_flash(:info, "Category updated successfully.")
|> redirect(to: Routes.category_path(conn, :show, category))

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

def delete(conn, %{"id" => id}) do
category = PostMeta.get_category!(id)
{:ok, _category} = PostMeta.delete_category(category)

conn
|> put_flash(:info, "Category deleted successfully.")
|> redirect(to: Routes.category_path(conn, :index))
end
end
98 changes: 98 additions & 0 deletions example/lib/example_web/controllers/post_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
defmodule ExampleWeb.PostController do
use ExampleWeb, :controller

alias Example.Content
alias Example.Content.Post
alias Example.PostMeta.Category
alias Example.Account.User
alias Example.Repo
require Logger

@post_filter_schema %{
title: :string,
view_count: :integer,
state: :string,
author: [
type: :ref,
model: User,
schema: %{
email: :string,
first_name: :string
}
],
category: [
type: :ref,
model: Category,
schema: %{
name: :string,
is_enabled: :boolean
}
]
}

def index(conn, params) do
with {:ok, filter} <- Querie.parse(@post_filter_schema, params) do
posts =
Querie.filter(Post, filter)
|> Repo.all()
|> Repo.preload([:category, :author])

render(conn, "index.html", posts: posts, errors: nil)
else
{:error, err} ->
Logger.error(inspect(err))
render(conn, "index.html", posts: [], errors: err)
end
end

def new(conn, _params) do
changeset = Content.change_post(%Post{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"post" => post_params}) do
case Content.create_post(post_params) do
{:ok, post} ->
conn
|> put_flash(:info, "Post created successfully.")
|> redirect(to: Routes.post_path(conn, :show, post))

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

def show(conn, %{"id" => id}) do
post = Content.get_post!(id)
render(conn, "show.html", post: post)
end

def edit(conn, %{"id" => id}) do
post = Content.get_post!(id)
changeset = Content.change_post(post)
render(conn, "edit.html", post: post, changeset: changeset)
end

def update(conn, %{"id" => id, "post" => post_params}) do
post = Content.get_post!(id)

case Content.update_post(post, post_params) do
{:ok, post} ->
conn
|> put_flash(:info, "Post updated successfully.")
|> redirect(to: Routes.post_path(conn, :show, post))

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

def delete(conn, %{"id" => id}) do
post = Content.get_post!(id)
{:ok, _post} = Content.delete_post(post)

conn
|> put_flash(:info, "Post deleted successfully.")
|> redirect(to: Routes.post_path(conn, :index))
end
end
62 changes: 62 additions & 0 deletions example/lib/example_web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule ExampleWeb.UserController do
use ExampleWeb, :controller

alias Example.Account
alias Example.Account.User

def index(conn, _params) do
users = Account.list_users()
render(conn, "index.html", users: users)
end

def new(conn, _params) do
changeset = Account.change_user(%User{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, %{"user" => user_params}) do
case Account.create_user(user_params) do
{:ok, user} ->
conn
|> put_flash(:info, "User created successfully.")
|> redirect(to: Routes.user_path(conn, :show, user))

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

def show(conn, %{"id" => id}) do
user = Account.get_user!(id)
render(conn, "show.html", user: user)
end

def edit(conn, %{"id" => id}) do
user = Account.get_user!(id)
changeset = Account.change_user(user)
render(conn, "edit.html", user: user, changeset: changeset)
end

def update(conn, %{"id" => id, "user" => user_params}) do
user = Account.get_user!(id)

case Account.update_user(user, user_params) do
{:ok, user} ->
conn
|> put_flash(:info, "User updated successfully.")
|> redirect(to: Routes.user_path(conn, :show, user))

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

def delete(conn, %{"id" => id}) do
user = Account.get_user!(id)
{:ok, _user} = Account.delete_user(user)

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

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

<span><%= link "Back", to: Routes.category_path(@conn, :index) %></span>
23 changes: 23 additions & 0 deletions example/lib/example_web/templates/category/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, :name %>
<%= text_input f, :name %>
<%= error_tag f, :name %>

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

<%= label f, :is_enabled %>
<%= checkbox f, :is_enabled %>
<%= error_tag f, :is_enabled %>

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

<table>
<thead>
<tr>
<th>Name</th>
<th>Slug</th>
<th>Is enabled</th>

<th></th>
</tr>
</thead>
<tbody>
<%= for category <- @categories do %>
<tr>
<td><%= category.name %></td>
<td><%= category.slug %></td>
<td><%= category.is_enabled %></td>

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

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

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

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

<ul>

<li>
<strong>Name:</strong>
<%= @category.name %>
</li>

<li>
<strong>Slug:</strong>
<%= @category.slug %>
</li>

<li>
<strong>Is enabled:</strong>
<%= @category.is_enabled %>
</li>

</ul>

<span><%= link "Edit", to: Routes.category_path(@conn, :edit, @category) %></span>
<span><%= link "Back", to: Routes.category_path(@conn, :index) %></span>
5 changes: 5 additions & 0 deletions example/lib/example_web/templates/post/edit.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Edit Post</h1>

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

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

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

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

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

<div>
<%= submit "Save" %>
</div>
<% end %>
Loading

0 comments on commit ae5a297

Please sign in to comment.