forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrole_skill_controller.ex
More file actions
50 lines (41 loc) · 1.44 KB
/
role_skill_controller.ex
File metadata and controls
50 lines (41 loc) · 1.44 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
defmodule CodeCorps.RoleSkillController do
use CodeCorps.Web, :controller
alias CodeCorps.RoleSkill
alias JaSerializer.Params
plug :load_and_authorize_resource, model: RoleSkill, only: [:create, :delete]
plug :scrub_params, "data" when action in [:create]
def index(conn, params) do
role_skills =
RoleSkill
|> RoleSkill.index_filters(params)
|> preload([:role, :skill])
|> Repo.all
render(conn, "index.json-api", data: role_skills)
end
def create(conn, %{"data" => data = %{"type" => "role-skill", "attributes" => _role_skill_params}}) do
changeset = RoleSkill.changeset(%RoleSkill{}, Params.to_attributes(data))
case Repo.insert(changeset) do
{:ok, role_skill} ->
conn
|> put_status(:created)
|> put_resp_header("location", role_skill_path(conn, :show, role_skill))
|> render("show.json-api", data: role_skill |> Repo.preload([:role, :skill]))
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
role_skill =
RoleSkill
|> preload([:role, :skill])
|> Repo.get!(id)
render(conn, "show.json-api", data: role_skill)
end
def delete(conn, %{"id" => id}) do
role_skill = Repo.get!(RoleSkill, id)
Repo.delete!(role_skill)
send_resp(conn, :no_content, "")
end
end