forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrole_controller.ex
More file actions
35 lines (29 loc) · 1 KB
/
role_controller.ex
File metadata and controls
35 lines (29 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
defmodule CodeCorps.RoleController do
use CodeCorps.Web, :controller
alias CodeCorps.Role
alias JaSerializer.Params
plug :load_and_authorize_resource, model: Role, only: [:create]
plug :scrub_params, "data" when action in [:create]
def index(conn, _params) do
roles =
Role
|> Repo.all
|> Repo.preload([:role_skills])
render(conn, "index.json-api", data: roles)
end
def create(conn, %{"data" => data = %{"type" => "role", "attributes" => _role_params}}) do
changeset = Role.changeset(%Role{}, Params.to_attributes(data))
case Repo.insert(changeset) do
{:ok, role} ->
role = Repo.preload(role, [:role_skills])
conn
|> put_status(:created)
|> put_resp_header("location", role_path(conn, :show, role))
|> render("show.json-api", data: role)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset)
end
end
end