Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (34 sloc) 1.04 KB
defmodule BlogWeb.PostController do
use BlogWeb, :controller
alias Blog.Posts
alias Blog.Posts.Post
action_fallback BlogWeb.FallbackController
def index(conn, _params) do
posts = Posts.list_posts()
render(conn, "index.json", posts: posts)
end
def create(conn, %{"post" => post_params}) do
with {:ok, %Post{} = post} <- Posts.create_post(post_params) do
conn
|> put_status(:created)
|> put_resp_header("location", post_path(conn, :show, post))
|> render("show.json", post: post)
end
end
def show(conn, %{"id" => id}) do
post = Posts.get_post!(id)
render(conn, "show.json", post: post)
end
def edit(conn, %{"id" => id, "post" => post_params}) do
post = Posts.get_post!(id)
with {:ok, %Post{} = post} <- Posts.update_post(post, post_params) do
render(conn, "show.json", post: post)
end
end
def delete(conn, %{"id" => id}) do
post = Posts.get_post!(id)
with {:ok, %Post{}} <- Posts.delete_post(post) do
send_resp(conn, :no_content, "")
end
end
end
You can’t perform that action at this time.