-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
927 additions
and
21 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
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 @@ | ||
flex@onpoits-MacBook-Pro.local.5981 |
62 changes: 62 additions & 0 deletions
62
example/lib/example_web/controllers/category_controller.ex
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 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 |
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,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 |
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 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 |
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 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> |
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,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 %> |
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,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> |
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 Category</h1> | ||
|
||
<%= render "form.html", Map.put(assigns, :action, Routes.category_path(@conn, :create)) %> | ||
|
||
<span><%= link "Back", to: Routes.category_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,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> |
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 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> |
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,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 %> |
Oops, something went wrong.