Skip to content

Commit

Permalink
Added json api for Pieces (Get all)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonyoungdev committed Apr 17, 2021
1 parent c93b28c commit 14c5ff4
Show file tree
Hide file tree
Showing 11 changed files with 381 additions and 5 deletions.
104 changes: 104 additions & 0 deletions lib/space_monopoly/game_objects.ex
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
20 changes: 20 additions & 0 deletions lib/space_monopoly/game_objects/piece.ex
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
2 changes: 1 addition & 1 deletion lib/space_monopoly_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ defmodule SpaceMonopolyWeb.PageController do
use SpaceMonopolyWeb, :controller

def index(conn, _params) do
render(conn, "select.html")
render(conn, "index.html")
end
end
43 changes: 43 additions & 0 deletions lib/space_monopoly_web/controllers/piece_controller.ex
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
7 changes: 4 additions & 3 deletions lib/space_monopoly_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ defmodule SpaceMonopolyWeb.Router do


# Other scopes may use custom stacks.
# scope "/api", SpaceMonopolyWeb do
# pipe_through :api
# end
scope "/api", SpaceMonopolyWeb do
pipe_through :api
get "/pieces", PieceController, :index
end

# Enables LiveDashboard only for development
#
Expand Down
3 changes: 3 additions & 0 deletions lib/space_monopoly_web/views/page_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule SpaceMonopolyWeb.PageView do
use SpaceMonopolyWeb, :view
end
20 changes: 20 additions & 0 deletions lib/space_monopoly_web/views/piece_view.ex
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
15 changes: 15 additions & 0 deletions priv/repo/migrations/20210417213144_create_pieces.exs
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
2 changes: 1 addition & 1 deletion priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.

alias SpaceMonopoly.Schema.Piece
alias SpaceMonopoly.GameObjects.Piece
alias SpaceMonopoly.Repo

Repo.insert!(%Piece{
Expand Down
70 changes: 70 additions & 0 deletions test/space_monopoly/game_objects_test.exs
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
Loading

0 comments on commit 14c5ff4

Please sign in to comment.