Compare commits

...
4 Commits
6 changed files with 46 additions and 22 deletions
+7
View File
@@ -19,4 +19,11 @@
Added the simple dev configuration. Added the simple dev configuration.
** 0.4.0 <2023-07-28 Fri> ** 0.4.0 <2023-07-28 Fri>
Added the API to show the Links model or models. Added the API to show the Links model or models.
** 0.4.1 <2023-07-29 Sat>
Add the API to create the Links model.
** 0.4.2 <2023-07-30 Sun>
Add the API to delete the Links model.
** 0.4.3 <2023-07-31 Mon>
Rename the controller and the view names to "Link".
Move Links to the Links context.
@@ -0,0 +1,34 @@
defmodule LinkShortenerWeb.Api.V1.LinkController 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 create(conn, %{"link" => link_params}) do
with {:ok, %Links{} = link} <- Links.insert_one(link_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.links_path(conn, :show, link))
|> render("show.json", links: link)
end
end
def show(conn, %{"id" => id}) do
link = Links.get_one(id)
render(conn, "show.json", links: link)
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
@@ -1,17 +0,0 @@
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
+1 -1
View File
@@ -26,7 +26,7 @@ defmodule LinkShortenerWeb.Router do
pipe_through :api pipe_through :api
scope "/v1", Api.V1, as: :v1 do scope "/v1", Api.V1, as: :v1 do
resources "/links", LinksController, except: [:new, :edit] resources "/links", LinkController
end end
end end
@@ -1,13 +1,13 @@
defmodule LinkShortenerWeb.Api.V1.LinksView do defmodule LinkShortenerWeb.Api.V1.LinkView do
use LinkShortenerWeb, :view use LinkShortenerWeb, :view
alias LinkShortenerWeb.Api.V1.LinksView alias LinkShortenerWeb.Api.V1.LinkView
def render("index.json", %{links: links}) do def render("index.json", %{links: links}) do
%{data: render_many(links, LinksView, "link.json")} %{data: render_many(links, LinkView, "link.json")}
end end
def render("show.json", %{links: link}) do def render("show.json", %{links: link}) do
%{data: render_one(link, LinksView, "link.json")} %{data: render_one(link, LinkView, "link.json")}
end end
def render("link.json", %{links: link}) do def render("link.json", %{links: link}) do