Add the API to show the Links model or models.

This commit is contained in:
2023-07-28 20:04:45 +03:00
parent 09e36646bf
commit 669d8fcefa
6 changed files with 90 additions and 3 deletions
@@ -0,0 +1,17 @@
defmodule LinkShortenerWeb.Api.V1.LinksController do
use LinkShortenerWeb, :controller
alias LinkShortener.Links
action_fallback LinkShortenerWeb.FallbackController
def index(conn, _params) do
links = Links.get_all({})
render(conn, "index.json", links: links)
end
def show(conn, %{"id" => id}) do
link = Links.get_one(id)
render(conn, "show.json", links: link)
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(LinkShortenerWeb.ChangesetView)
|> render("error.json", 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(LinkShortenerWeb.ErrorView)
|> render(:"404")
end
end