Skip to content

Instantly share code, notes, and snippets.

@MikaAK
Created February 23, 2016 05:21
Show Gist options
  • Save MikaAK/0e9ec9b264abfaf343b5 to your computer and use it in GitHub Desktop.
Save MikaAK/0e9ec9b264abfaf343b5 to your computer and use it in GitHub Desktop.
defmodule TrelloBurndown.SprintController do
use TrelloBurndown.Web, :controller
alias TrelloBurndown.Sprint
def index(conn, _params) do
sprints = Repo.all(from s in Sprint, preload: [:team])
render(conn, "index.json", sprints: sprints)
end
def create(conn, sprint_params) do
changeset = Sprint.changeset(%Sprint{}, sprint_params)
case Repo.insert(changeset) do
{:ok, sprint} ->
conn
|> put_status(:created)
|> put_resp_header("location", sprint_path(conn, :show, sprint))
|> render("show.json", sprint: sprint)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(TrelloBurndown.ChangesetView, "error.json", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
sprint = Repo.get!(Sprint, id)
render(conn, "show.json", sprint: sprint)
end
def update(conn, sprint_params) do
sprint = Repo.get!(Sprint, sprint_params.id)
changeset = Sprint.changeset(sprint, sprint_params)
case Repo.update(changeset) do
{:ok, sprint} ->
render(conn, "show.json", sprint: sprint)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(TrelloBurndown.ChangesetView, "error.json", changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
sprint = Repo.get!(Sprint, id)
# Here we use delete! (with a bang) because we expect
# it to always work (and if it does not, it will raise).
Repo.delete!(sprint)
send_resp(conn, :no_content, "")
end
end
defmodule TrelloBurndown.SprintView do
import IEx
use TrelloBurndown.Web, :view
def render("index.json", %{sprints: sprints}) do
%{data: render_many(sprints, TrelloBurndown.SprintView, "sprint.json")}
end
def render("show.json", %{sprint: sprint}) do
%{data: render_one(sprint, TrelloBurndown.SprintView, "sprint.json")}
end
def render("sprint.json", %{sprint: sprint}) do
params = %{id: sprint.id,
board_id: sprint.board_id,
sprint_name: sprint.sprint_name,
point_total: sprint.point_total,
team_id: sprint.team_id,
holidays: sprint.holidays
}
IO.inspect(params)
if Map.get(sprint, :team) do
params = Map.put(params, :team, Map.get(sprint, :team))
IO.inspect(params)
params
else
params
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment