forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_controller.ex
More file actions
62 lines (52 loc) · 1.67 KB
/
task_controller.ex
File metadata and controls
62 lines (52 loc) · 1.67 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
51
52
53
54
55
56
57
58
59
60
61
62
defmodule CodeCorps.TaskController do
use CodeCorps.Web, :controller
use JaResource
import CodeCorps.Helpers.Query, only: [
project_filter: 2, project_id_with_number_filter: 2, sort_by_newest_first: 1,
task_type_filter: 2, task_status_filter: 2
]
alias CodeCorps.Task
plug :load_and_authorize_changeset, model: Task, only: [:create]
plug :load_and_authorize_resource, model: Task, only: [:update]
plug JaResource
def handle_index(conn, params) do
page = Task
|> project_filter(params)
|> task_type_filter(params)
|> task_status_filter(params)
|> sort_by_newest_first
|> Repo.paginate(params["page"] || %{})
# TODO: Once we are able to more easily add top-level meta
# from within ja_resource or ja_serializer
# we can split up all of this into
# handle_index
# handle_index_query
# serialization_opts
meta = %{
current_page: page.page_number,
page_size: page.page_size,
total_pages: page.total_pages,
total_records: page.total_entries
}
conn
|> render("index.json-api", data: page, opts: [meta: meta])
end
def record(%Plug.Conn{params: %{"project_id" => _project_id} = params}, _number_as_id) do
Task
|> project_id_with_number_filter(params)
|> Repo.one
end
def record(_conn, id), do: Task |> Repo.get(id)
def handle_create(conn, attributes) do
%Task{}
|> Task.create_changeset(attributes)
|> Repo.insert
|> CodeCorps.Analytics.Segment.track(:created, conn)
end
def handle_update(conn, task, attributes) do
task
|> Task.update_changeset(attributes)
|> Repo.update
|> CodeCorps.Analytics.Segment.track(:edited, conn)
end
end