Created
August 13, 2015 14:43
-
-
Save nickgartmann/04886d379d1a58847ccf to your computer and use it in GitHub Desktop.
Example output from a JSONAPI generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mix jsonapi.gen.templates | |
mix phoenix.gen.json Test testing name:string email:string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Testing.Testing.TestController do | |
use Testing.Web, :controller | |
alias Testing.ChangesetView | |
import Ecto.Query | |
plug :scrub_params, "data" when action in [:create, :update] | |
# GET /tests/:id | |
def show(conn, %{"id" => id}) do | |
test = Repo.get!(Testing.Test, id) | |
render conn, "show.json", data: test | |
end | |
# GET /tests | |
def index(conn, params) do | |
filter = conn.assigns[:jsonapi][:filter] | |
tests = from(t in Testing.Test) | |
|> JSONAPI.Query.add_query_paging(conn.assigns[:jsonapi]) | |
|> Repo.all | |
render conn, "index.json", data: tests, params: params | |
end | |
# POST /tests | |
def create(conn, json=%{"format" => "json"}) do | |
changeset = Test.changeset(%Test{}, Dict.get(json, "data", %{})) | |
case Repo.insert(changeset) do | |
{:ok, test} -> | |
conn | |
|> put_resp_header("location", test_path(Endpoint, :show, test.id)) | |
|> put_status(201) | |
|> render "show.json", data: test | |
{:error, errors} -> | |
put_status(conn, 400) | |
|> render ChangesetView, "error.json", changeset: changeset | |
end | |
end | |
# PUT /tests/:id | |
def update(conn, json=%{"format" => "json", "id" => id}) do | |
test = Repo.get!(Test, id) | |
changeset = Test.changeset(test, Dict.get(json, "data", %{})) | |
case Repo.update(changeset) do | |
{:ok, test} -> | |
render conn, "show.json", data: test | |
{:error, changeset} -> | |
put_status(conn, 400) | |
|> render ChangesetView, "error.json", changeset: changeset | |
end | |
end | |
# DELETE /tests/:id | |
def delete(conn, %{"format" => "json", "id" => id}) do | |
test = Repo.get!(Test, id) | |
|> Repo.delete! | |
send_resp(conn, 204, "") |> halt | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Testing.TestView do | |
use Testing.Web, :view | |
def type(), do: "test" | |
def attributes(test) do | |
Map.take(test, [:email, :name]) | |
end | |
def relationships() do | |
%{} | |
end | |
def url_func(), do: &test_url/3 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment