Update the controller and the view for updated Link model.
This commit is contained in:
@@ -27,4 +27,5 @@
|
|||||||
Rename the controller and the view names to "Link".
|
Rename the controller and the view names to "Link".
|
||||||
Move Links to the Links context.
|
Move Links to the Links context.
|
||||||
Refactor to split the Links to a Link model and a Links logic.
|
Refactor to split the Links to a Link model and a Links logic.
|
||||||
|
Update the controller and the view for updated Link model.
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
defmodule LinkShortenerWeb.Api.V1.LinkController do
|
defmodule LinkShortenerWeb.Api.V1.LinkController do
|
||||||
use LinkShortenerWeb, :controller
|
use LinkShortenerWeb, :controller
|
||||||
|
|
||||||
alias LinkShortener.Links
|
alias LinkShortener.Links.Links
|
||||||
|
alias LinkShortener.Links.Link
|
||||||
|
|
||||||
action_fallback LinkShortenerWeb.FallbackController
|
action_fallback LinkShortenerWeb.FallbackController
|
||||||
|
|
||||||
@@ -11,17 +12,18 @@ defmodule LinkShortenerWeb.Api.V1.LinkController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create(conn, %{"link" => link_params}) do
|
def create(conn, %{"link" => link_params}) do
|
||||||
with {:ok, %Links{} = link} <- Links.insert_one(link_params) do
|
with {:ok, %Link{} = link} <- Links.insert_one(link_params) do
|
||||||
conn
|
conn
|
||||||
|> put_status(:created)
|
|> put_status(:created)
|
||||||
|> put_resp_header("location", Routes.links_path(conn, :show, link))
|
|> put_resp_header("location", Routes.link_path(conn, :show, link))
|
||||||
|> render("show.json", links: link)
|
|> render("show.json", link: link)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def show(conn, %{"id" => id}) do
|
def show(conn, %{"id" => id}) do
|
||||||
link = Links.get_one(id)
|
link = Links.get_one(id)
|
||||||
render(conn, "show.json", links: link)
|
render(conn, "show.json", link: link)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(conn, %{"id" => id}) do
|
def delete(conn, %{"id" => id}) do
|
||||||
|
|||||||
@@ -6,11 +6,11 @@ defmodule LinkShortenerWeb.Api.V1.LinkView do
|
|||||||
%{data: render_many(links, LinkView, "link.json")}
|
%{data: render_many(links, LinkView, "link.json")}
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("show.json", %{links: link}) do
|
def render("show.json", %{link: link}) do
|
||||||
%{data: render_one(link, LinkView, "link.json")}
|
%{data: render_one(link, LinkView, "link.json")}
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("link.json", %{links: link}) do
|
def render("link.json", %{link: link}) do
|
||||||
%{
|
%{
|
||||||
id: link.id,
|
id: link.id,
|
||||||
name: link.name,
|
name: link.name,
|
||||||
|
|||||||
Reference in New Issue
Block a user