Packages
literature
0.4.17
0.5.1
0.5.0
0.4.22
0.4.21
0.4.20
0.4.19
0.4.18
0.4.17
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
retired
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.18
0.2.17
0.2.16
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.32
0.1.30
0.1.29
0.1.28
0.1.27
0.1.26
0.1.25
0.1.24
0.1.23
0.1.22
0.1.21
0.1.20
0.1.19
0.1.18
0.1.17
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
A simple CMS / Blog
Current section
Files
Jump to
Current section
Files
lib/literature/controllers/api_controller.ex
defmodule Literature.ApiController do
@moduledoc false
use Literature.Web, :controller
def author(conn, params) do
with {:ok, _} <- Literature.validate_params(params),
params <- Literature.build_params(params),
{:ok, author} <- Literature.create_author(params) do
conn
|> put_resp_content_type("application/json")
|> send_resp(
200,
Jason.encode!(%{
status: "success",
message: "#{author.name} successfully created"
})
)
else
{:error, %Ecto.Changeset{} = changeset} ->
conn
|> put_resp_content_type("application/json")
|> put_status(:unprocessable_entity)
|> json(%{
errors: format_errors(changeset.errors),
message: "Failed to create author"
})
{:error, message} ->
conn
|> put_resp_content_type("application/json")
|> send_resp(
400,
Jason.encode!(%{
status: "error",
message: message
})
)
end
end
def tag(conn, params) do
with {:ok, _} <- Literature.validate_params(params),
params <- Literature.build_params(params),
{:ok, tag} <- Literature.create_tag(params) do
conn
|> put_resp_content_type("application/json")
|> send_resp(
200,
Jason.encode!(%{
status: "success",
message: "#{tag.name} successfully created"
})
)
else
{:error, %Ecto.Changeset{} = changeset} ->
conn
|> put_resp_content_type("application/json")
|> put_status(:unprocessable_entity)
|> json(%{
errors: format_errors(changeset.errors),
message: "Failed to create tag"
})
{:error, message} ->
conn
|> put_resp_content_type("application/json")
|> send_resp(
400,
Jason.encode!(%{
status: "error",
message: message
})
)
end
end
def post(conn, params) do
case oban_post_worker() do
{oban, oban_job, oban_worker} ->
params
|> Map.put("type", "literature_post")
|> oban_job.new(worker: oban_worker, max_attempts: 3)
|> oban.insert()
conn
|> put_resp_content_type("application/json")
|> send_resp(
200,
Jason.encode!(%{
status: "success",
message: "#{params["data"]["title"]} successfully created oban worker"
})
)
_oban ->
with {:ok, _} <- Literature.validate_params(params),
params <- Literature.build_params(params),
{:ok, post} <- Literature.create_post(params) do
conn
|> put_resp_content_type("application/json")
|> send_resp(
200,
Jason.encode!(%{
status: "success",
message: "#{post.title} successfully created"
})
)
else
{:error, %Ecto.Changeset{} = changeset} ->
conn
|> put_resp_content_type("application/json")
|> put_status(:unprocessable_entity)
|> json(%{
errors: format_errors(changeset.errors),
message: "Failed to create post"
})
{:error, message} ->
conn
|> put_resp_content_type("application/json")
|> put_resp_content_type("application/json")
|> send_resp(
400,
Jason.encode!(%{
status: "error",
message: message
})
)
end
end
end
def format_errors(errors) do
errors
|> Enum.map(fn f ->
{field, {message, _} = _opts} = f
field =
field
|> Atom.to_string()
"#{field} #{message}"
end)
end
def oban_post_worker, do: Application.get_env(:literature, :oban_job)
end