Update the link api.
continuous-integration/drone/push Build is passing

This commit is contained in:
2024-08-20 21:46:00 +03:00
parent 18bb0b13b6
commit 59de90601a
6 changed files with 219 additions and 4 deletions
@@ -0,0 +1,43 @@
defmodule LinkShortenerWeb.Api.V1.LinkController do
use LinkShortenerWeb, :controller
alias LinkShortener.Links
alias LinkShortener.Links.Link
action_fallback LinkShortenerWeb.FallbackController
def index(conn, _params) do
links = Links.get_all()
render(conn, :index, links: links)
end
def create(conn, %{"link" => link_params}) do
with {:ok, %Link{} = link} <- Links.insert_one(link_params) do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/v1/items/")
|> render(:show, link: link)
end
end
def show(conn, %{"id" => id}) do
link = Links.get_one!(id)
render(conn, :show, link: link)
end
def update(conn, %{"id" => id, "link" => link_params}) do
link = Links.get_one!(id)
with {:ok, %Link{} = link} <- Links.update_one(link, link_params) do
render(conn, :show, link: link)
end
end
def delete(conn, %{"id" => id}) do
link = Links.get_one!(id)
with {:ok, %Link{}} <- Links.delete_one(link) do
send_resp(conn, :no_content, "")
end
end
end
@@ -0,0 +1,20 @@
defmodule LinkShortenerWeb.Api.V1.LinkJSON do
alias LinkShortener.Links.Link
def index(%{links: links}) do
%{data: for(link <- links, do: link(link))}
end
def show(%{link: link}) do
%{data: link(link)}
end
def link(%Link{} = link) do
%{
id: link.id,
name: link.name,
url: link.url,
shorten: link.shorten
}
end
end
@@ -0,0 +1,25 @@
defmodule LinkShortenerWeb.ChangesetJSON do
@doc """
Renders changeset errors.
"""
def error(%{changeset: changeset}) do
# When encoded, the changeset returns its errors
# as a JSON object. So we just pass it forward.
%{errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)}
end
defp translate_error({msg, opts}) do
# You can make use of gettext to translate error messages by
# uncommenting and adjusting the following code:
# if count = opts[:count] do
# Gettext.dngettext(LinkShortenerWeb.Gettext, "errors", msg, msg, count, opts)
# else
# Gettext.dgettext(LinkShortenerWeb.Gettext, "errors", msg, opts)
# end
Enum.reduce(opts, msg, fn {key, value}, acc ->
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
end)
end
end
@@ -0,0 +1,24 @@
defmodule LinkShortenerWeb.FallbackController do
@moduledoc """
Translates controller action results into valid `Plug.Conn` responses.
See `Phoenix.Controller.action_fallback/1` for more details.
"""
use LinkShortenerWeb, :controller
# This clause handles errors returned by Ecto's insert/update/delete.
def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
conn
|> put_status(:unprocessable_entity)
|> put_view(json: LinkShortenerWeb.ChangesetJSON)
|> render(:error, changeset: changeset)
end
# This clause is an example of how to handle resources that cannot be found.
def call(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)
|> put_view(html: LinkShortenerWeb.ErrorHTML, json: LinkShortenerWeb.ErrorJSON)
|> render(:"404")
end
end
+7 -4
View File
@@ -20,10 +20,13 @@ defmodule LinkShortenerWeb.Router do
get "/", PageController, :home
end
# Other scopes may use custom stacks.
# scope "/api", LinkShortenerWeb do
# pipe_through :api
# end
scope "/api", LinkShortenerWeb do
pipe_through :api
scope "/v1", Api.V1, as: :v1 do
resources "/links", LinkController
end
end
# Enable LiveDashboard and Swoosh mailbox preview in development
if Application.compile_env(:link_shortener, :dev_routes) do