Compare commits
4
Commits
0.4.0
..
fb4f0025d1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb4f0025d1 | ||
|
|
6799e5b00b | ||
|
|
b2fd493d07 | ||
|
|
4e43da527f |
@@ -19,4 +19,11 @@
|
||||
Added the simple dev configuration.
|
||||
** 0.4.0 <2023-07-28 Fri>
|
||||
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
|
||||
@@ -26,7 +26,7 @@ defmodule LinkShortenerWeb.Router do
|
||||
pipe_through :api
|
||||
|
||||
scope "/v1", Api.V1, as: :v1 do
|
||||
resources "/links", LinksController, except: [:new, :edit]
|
||||
resources "/links", LinkController
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+4
-4
@@ -1,13 +1,13 @@
|
||||
defmodule LinkShortenerWeb.Api.V1.LinksView do
|
||||
defmodule LinkShortenerWeb.Api.V1.LinkView do
|
||||
use LinkShortenerWeb, :view
|
||||
alias LinkShortenerWeb.Api.V1.LinksView
|
||||
alias LinkShortenerWeb.Api.V1.LinkView
|
||||
|
||||
def render("index.json", %{links: links}) do
|
||||
%{data: render_many(links, LinksView, "link.json")}
|
||||
%{data: render_many(links, LinkView, "link.json")}
|
||||
end
|
||||
|
||||
def render("show.json", %{links: link}) do
|
||||
%{data: render_one(link, LinksView, "link.json")}
|
||||
%{data: render_one(link, LinkView, "link.json")}
|
||||
end
|
||||
|
||||
def render("link.json", %{links: link}) do
|
||||
Reference in New Issue
Block a user