-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d90cf93
commit 558852b
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
defmodule Yggdrasil.CharacterController do | ||
use Yggdrasil.Web, :controller | ||
|
||
alias Yggdrasil.Character | ||
|
||
def index(conn, %{"filter" => %{"user_id" => user_id}}) do | ||
# this should not happen, but could easily | ||
# probably don't need to crash but rather return an error | ||
^user_id = conn.assigns.user | ||
|
||
chars = Repo.all from c in Character, | ||
where: c.user_id == ^user_id, | ||
select: c | ||
|
||
render conn, :show, data: chars | ||
end | ||
|
||
def show(conn, %{"char_id" => char_id}) do | ||
user_id = conn.assigns.user | ||
|
||
# returns nil if not found | ||
# need to sort out what we want here. | ||
char = Repo.one from c in Character, | ||
where: c.id == ^char_id and c.user_id == ^user_id, | ||
select: c | ||
|
||
render conn, :show, data: char | ||
end | ||
|
||
def create(conn, %{"data" => %{"attributes" => attributes}}) do | ||
char = Character.changeset(%Character{}, attributes) | ||
|
||
case Repo.insert(char) do | ||
{:ok, new_char} -> | ||
render conn, :show, data: new_char | ||
{:error, err_changeset} -> | ||
render conn, :errors, data: err_changeset | ||
end | ||
end | ||
|
||
def delete(conn, %{"char_id" => char_id}) do | ||
user_id = conn.assigns.user | ||
|
||
# returns nil if not found | ||
# need to sort out what we want here. | ||
char = Repo.one from c in Character, | ||
where: c.id == ^char_id and c.user_id == ^user_id, | ||
select: c | ||
|
||
case Repo.delete(char)do | ||
{:ok, _char} -> | ||
# http://jsonapi.org/format/#crud-deleting | ||
conn | ||
|> put_status(204) | ||
|> send_resp | ||
{:error, err_changeset} -> | ||
render conn, :errors, data: err_changeset | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters