forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview_controller.ex
More file actions
33 lines (25 loc) · 859 Bytes
/
preview_controller.ex
File metadata and controls
33 lines (25 loc) · 859 Bytes
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
defmodule CodeCorps.PreviewController do
use CodeCorps.Web, :controller
alias CodeCorps.Preview
alias JaSerializer.Params
plug :load_and_authorize_resource, model: Preview, only: [:create]
def create(conn, %{"data" => data = %{"type" => "preview", "attributes" => _project_params}}) do
user =
conn
|> Guardian.Plug.current_resource
changeset = Preview.changeset(%Preview{}, Params.to_attributes(data), user)
case Repo.insert(changeset) do
{:ok, preview} ->
preview =
preview
|> Repo.preload([:user])
conn
|> put_status(:created)
|> render("show.json-api", data: preview)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset)
end
end
end