write first test and let changeset errors return error json in controller

This commit is contained in:
2021-12-02 22:45:39 -05:00
parent 89c2a99283
commit d32dc8c2cb
7 changed files with 148 additions and 13 deletions

View File

@@ -0,0 +1,10 @@
defmodule EntenduWeb.FallbackController do
use EntenduWeb, :controller
def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
conn
|> put_status(:unprocessable_entity)
|> put_view(EntenduWeb.ChangesetView)
|> render("error.json", changeset: changeset)
end
end

View File

@@ -9,27 +9,33 @@ defmodule EntenduWeb.LinkController do
alias Entendu.Links
alias Links.Link
alias Ecto.Changeset
alias EntenduWeb.FallbackController
action_fallback(FallbackController)
def just_page(conn, _params) do
render(conn, "just.html")
end
defparams first_step %{
burn_after_reading: [field: :boolean, default: false],
expires: :utc_datetime,
filename: :string,
filetype: :string,
text_content: :string,
file_content: :string
}
defparams(
first_step(%{
burn_after_reading: [field: :boolean, default: false],
expires: :utc_datetime,
filename: :string,
filetype: :string,
text_content: :string,
file_content: :string
})
)
def just(conn, params) do
with %Changeset{valid?: true} = changeset <- first_step(params),
link_params <- Params.to_map(changeset),
{:ok, %Link{} = link} <- Links.create_link(link_params) do
link_params <- Params.to_map(changeset),
{:ok, %Link{} = link} <- Links.create_link(link_params) do
conn
|> put_status(:created)
|> assign(:link, link)
|> render("show_authorized", link: link)
|> render("show_authorized.json", %{link: link})
end
end