-
Notifications
You must be signed in to change notification settings - Fork 2
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
c93b28c
commit 14c5ff4
Showing
11 changed files
with
381 additions
and
5 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,104 @@ | ||
defmodule SpaceMonopoly.GameObjects do | ||
@moduledoc """ | ||
The GameObjects context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias SpaceMonopoly.Repo | ||
|
||
alias SpaceMonopoly.GameObjects.Piece | ||
|
||
@doc """ | ||
Returns the list of pieces. | ||
## Examples | ||
iex> list_pieces() | ||
[%Piece{}, ...] | ||
""" | ||
def list_pieces do | ||
Repo.all(Piece) | ||
end | ||
|
||
@doc """ | ||
Gets a single piece. | ||
Raises `Ecto.NoResultsError` if the Piece does not exist. | ||
## Examples | ||
iex> get_piece!(123) | ||
%Piece{} | ||
iex> get_piece!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_piece!(id), do: Repo.get!(Piece, id) | ||
|
||
@doc """ | ||
Creates a piece. | ||
## Examples | ||
iex> create_piece(%{field: value}) | ||
{:ok, %Piece{}} | ||
iex> create_piece(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_piece(attrs \\ %{}) do | ||
%Piece{} | ||
|> Piece.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a piece. | ||
## Examples | ||
iex> update_piece(piece, %{field: new_value}) | ||
{:ok, %Piece{}} | ||
iex> update_piece(piece, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_piece(%Piece{} = piece, attrs) do | ||
piece | ||
|> Piece.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a piece. | ||
## Examples | ||
iex> delete_piece(piece) | ||
{:ok, %Piece{}} | ||
iex> delete_piece(piece) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_piece(%Piece{} = piece) do | ||
Repo.delete(piece) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking piece changes. | ||
## Examples | ||
iex> change_piece(piece) | ||
%Ecto.Changeset{data: %Piece{}} | ||
""" | ||
def change_piece(%Piece{} = piece, attrs \\ %{}) do | ||
Piece.changeset(piece, attrs) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule SpaceMonopoly.GameObjects.Piece do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "pieces" do | ||
field :lat, :decimal | ||
field :lon, :decimal | ||
field :name, :string | ||
field :url, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(piece, attrs) do | ||
piece | ||
|> cast(attrs, [:url, :name, :lat, :lon]) | ||
|> validate_required([:url, :name, :lat, :lon]) | ||
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
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,43 @@ | ||
defmodule SpaceMonopolyWeb.PieceController do | ||
use SpaceMonopolyWeb, :controller | ||
|
||
alias SpaceMonopoly.GameObjects | ||
alias SpaceMonopoly.GameObjects.Piece | ||
|
||
action_fallback SpaceMonopolyWeb.FallbackController | ||
|
||
def index(conn, _params) do | ||
pieces = GameObjects.list_pieces() | ||
render(conn, "index.json", pieces: pieces) | ||
end | ||
|
||
def create(conn, %{"piece" => piece_params}) do | ||
with {:ok, %Piece{} = piece} <- GameObjects.create_piece(piece_params) do | ||
conn | ||
|> put_status(:created) | ||
|> put_resp_header("location", Routes.piece_path(conn, :show, piece)) | ||
|> render("show.json", piece: piece) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
piece = GameObjects.get_piece!(id) | ||
render(conn, "show.json", piece: piece) | ||
end | ||
|
||
def update(conn, %{"id" => id, "piece" => piece_params}) do | ||
piece = GameObjects.get_piece!(id) | ||
|
||
with {:ok, %Piece{} = piece} <- GameObjects.update_piece(piece, piece_params) do | ||
render(conn, "show.json", piece: piece) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
piece = GameObjects.get_piece!(id) | ||
|
||
with {:ok, %Piece{}} <- GameObjects.delete_piece(piece) do | ||
send_resp(conn, :no_content, "") | ||
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
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,3 @@ | ||
defmodule SpaceMonopolyWeb.PageView do | ||
use SpaceMonopolyWeb, :view | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule SpaceMonopolyWeb.PieceView do | ||
use SpaceMonopolyWeb, :view | ||
alias SpaceMonopolyWeb.PieceView | ||
|
||
def render("index.json", %{pieces: pieces}) do | ||
%{data: render_many(pieces, PieceView, "piece.json")} | ||
end | ||
|
||
def render("show.json", %{piece: piece}) do | ||
%{data: render_one(piece, PieceView, "piece.json")} | ||
end | ||
|
||
def render("piece.json", %{piece: piece}) do | ||
%{id: piece.id, | ||
url: piece.url, | ||
name: piece.name, | ||
lat: piece.lat, | ||
lon: piece.lon} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule SpaceMonopoly.Repo.Migrations.CreatePieces do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:pieces) do | ||
add :url, :string | ||
add :name, :string | ||
add :lat, :decimal | ||
add :lon, :decimal | ||
|
||
timestamps() | ||
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
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,70 @@ | ||
defmodule SpaceMonopoly.GameObjectsTest do | ||
use SpaceMonopoly.DataCase | ||
|
||
alias SpaceMonopoly.GameObjects | ||
|
||
describe "pieces" do | ||
alias SpaceMonopoly.GameObjects.Piece | ||
|
||
@valid_attrs %{lat: "120.5", lon: "120.5", name: "some name", url: "some url"} | ||
@update_attrs %{lat: "456.7", lon: "456.7", name: "some updated name", url: "some updated url"} | ||
@invalid_attrs %{lat: nil, lon: nil, name: nil, url: nil} | ||
|
||
def piece_fixture(attrs \\ %{}) do | ||
{:ok, piece} = | ||
attrs | ||
|> Enum.into(@valid_attrs) | ||
|> GameObjects.create_piece() | ||
|
||
piece | ||
end | ||
|
||
test "list_pieces/0 returns all pieces" do | ||
piece = piece_fixture() | ||
assert GameObjects.list_pieces() == [piece] | ||
end | ||
|
||
test "get_piece!/1 returns the piece with given id" do | ||
piece = piece_fixture() | ||
assert GameObjects.get_piece!(piece.id) == piece | ||
end | ||
|
||
test "create_piece/1 with valid data creates a piece" do | ||
assert {:ok, %Piece{} = piece} = GameObjects.create_piece(@valid_attrs) | ||
assert piece.lat == Decimal.new("120.5") | ||
assert piece.lon == Decimal.new("120.5") | ||
assert piece.name == "some name" | ||
assert piece.url == "some url" | ||
end | ||
|
||
test "create_piece/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = GameObjects.create_piece(@invalid_attrs) | ||
end | ||
|
||
test "update_piece/2 with valid data updates the piece" do | ||
piece = piece_fixture() | ||
assert {:ok, %Piece{} = piece} = GameObjects.update_piece(piece, @update_attrs) | ||
assert piece.lat == Decimal.new("456.7") | ||
assert piece.lon == Decimal.new("456.7") | ||
assert piece.name == "some updated name" | ||
assert piece.url == "some updated url" | ||
end | ||
|
||
test "update_piece/2 with invalid data returns error changeset" do | ||
piece = piece_fixture() | ||
assert {:error, %Ecto.Changeset{}} = GameObjects.update_piece(piece, @invalid_attrs) | ||
assert piece == GameObjects.get_piece!(piece.id) | ||
end | ||
|
||
test "delete_piece/1 deletes the piece" do | ||
piece = piece_fixture() | ||
assert {:ok, %Piece{}} = GameObjects.delete_piece(piece) | ||
assert_raise Ecto.NoResultsError, fn -> GameObjects.get_piece!(piece.id) end | ||
end | ||
|
||
test "change_piece/1 returns a piece changeset" do | ||
piece = piece_fixture() | ||
assert %Ecto.Changeset{} = GameObjects.change_piece(piece) | ||
end | ||
end | ||
end |
Oops, something went wrong.