Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Link Objects and Additional Controller Actions #264

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion lib/jsonapi/view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ defmodule JSONAPI.View do
@type t :: module()
@type data :: any()
@type field :: atom()
@type links :: %{atom() => String.t()}
@type link_object :: %{required(:href) => String.t(), optional(:meta) => meta()}
@type links :: %{atom() => String.t() | link_object()}
@type meta :: %{atom() => String.t()}
@type options :: keyword()
@type resource_id :: String.t()
Expand Down Expand Up @@ -244,6 +245,15 @@ defmodule JSONAPI.View do
def show(model, conn, _params, meta \\ nil, options \\ []),
do: Serializer.serialize(__MODULE__, model, conn, meta, options)

def update(model, conn, _params, meta \\ nil, options \\ []),
do: Serializer.serialize(__MODULE__, model, conn, meta, options)

def delete(model, conn, _params, meta \\ nil, options \\ []),
do: Serializer.serialize(__MODULE__, model, conn, meta, options)

def create(model, conn, _params, meta \\ nil, options \\ []),
do: Serializer.serialize(__MODULE__, model, conn, meta, options)

if Code.ensure_loaded?(Phoenix) do
def render("show.json", %{data: data, conn: conn, meta: meta, options: options}),
do: Serializer.serialize(__MODULE__, data, conn, meta, options)
Expand All @@ -262,6 +272,33 @@ defmodule JSONAPI.View do

def render("index.json", %{data: data, conn: conn}),
do: Serializer.serialize(__MODULE__, data, conn)

def render("create.json", %{data: data, conn: conn, meta: meta, options: options}),
do: Serializer.serialize(__MODULE__, data, conn, meta, options)

def render("create.json", %{data: data, conn: conn, meta: meta}),
do: Serializer.serialize(__MODULE__, data, conn, meta)

def render("create.json", %{data: data, conn: conn}),
do: Serializer.serialize(__MODULE__, data, conn)

def render("update.json", %{data: data, conn: conn, meta: meta, options: options}),
do: Serializer.serialize(__MODULE__, data, conn, meta, options)

def render("update.json", %{data: data, conn: conn, meta: meta}),
do: Serializer.serialize(__MODULE__, data, conn, meta)

def render("update.json", %{data: data, conn: conn}),
do: Serializer.serialize(__MODULE__, data, conn)

def render("delete.json", %{data: data, conn: conn, meta: meta, options: options}),
do: Serializer.serialize(__MODULE__, data, conn, meta, options)

def render("delete.json", %{data: data, conn: conn, meta: meta}),
do: Serializer.serialize(__MODULE__, data, conn, meta)

def render("delete.json", %{data: data, conn: conn}),
do: Serializer.serialize(__MODULE__, data, conn)
else
raise ArgumentError,
"Attempted to call function that depends on Phoenix. " <>
Expand Down
11 changes: 11 additions & 0 deletions test/jsonapi_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ defmodule JSONAPITest do
def fields, do: [:username]
def type, do: "user"

def links(user, _conn) do
%{
profile: %{
href: "#{path()}/#{user.username}",
meta: %{
method: "get"
}
}
}
end

def relationships do
[company: JSONAPITest.CompanyView]
end
Expand Down
Loading