initial commit
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
defmodule Bwc do
|
||||
@moduledoc """
|
||||
The core module for the Blank White Cards game.
|
||||
"""
|
||||
|
||||
alias Bwc.{Repo, Card}
|
||||
|
||||
@repo Repo
|
||||
|
||||
@doc """
|
||||
List all cards.
|
||||
"""
|
||||
def list_cards, do: @repo.all(Card)
|
||||
|
||||
def get_card(id), do: @repo.get!(Card, id)
|
||||
|
||||
def get_card_by(attrs), do: @repo.get_by(Card, attrs)
|
||||
|
||||
def create_card(attrs), do: Card.changeset(%Card{},attrs) |> @repo.insert()
|
||||
|
||||
def new_card, do: Card.changeset(%Card{})
|
||||
|
||||
def size, do: Bwc.list_cards |> length
|
||||
|
||||
def draw_card, do: Bwc.list_cards |> Enum.random
|
||||
def draw, do: draw_card()
|
||||
|
||||
def delete_card(%Bwc.Card{} = card), do: @repo.delete(card)
|
||||
def delete_card!(%Bwc.Card{} = card), do: @repo.delete!(card)
|
||||
|
||||
def update_card(%Bwc.Card{} = card, updates) do
|
||||
card
|
||||
|> Card.changeset(updates)
|
||||
|> @repo.update
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
defmodule Bwc do
|
||||
@moduledoc """
|
||||
The core module for the Blank White Cards game.
|
||||
"""
|
||||
|
||||
alias Bwc.{TestRepo, Card}
|
||||
|
||||
@repo TestRepo
|
||||
|
||||
@doc """
|
||||
List all cards.
|
||||
"""
|
||||
def list_cards, do: @repo.all(Card)
|
||||
|
||||
def get_card(id), do: @repo.get!(Card, id)
|
||||
|
||||
def get_card_by(attrs), do: @repo.get_by(Card, attrs)
|
||||
|
||||
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
defmodule Bwc.Application do
|
||||
# See https://hexdocs.pm/elixir/Application.html
|
||||
# for more information on OTP Applications
|
||||
@moduledoc false
|
||||
|
||||
use Application
|
||||
|
||||
@impl true
|
||||
def start(_type, _args) do
|
||||
children = [
|
||||
# Starts a worker by calling: Bwc.Worker.start_link(arg)
|
||||
# {Bwc.Worker, arg}
|
||||
{Bwc.Repo, []}
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
# for other strategies and supported options
|
||||
opts = [strategy: :one_for_one, name: Bwc.Supervisor]
|
||||
Supervisor.start_link(children, opts)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
defmodule Bwc.Application do
|
||||
# See https://hexdocs.pm/elixir/Application.html
|
||||
# for more information on OTP Applications
|
||||
@moduledoc false
|
||||
|
||||
use Application
|
||||
|
||||
@impl true
|
||||
def start(_type, _args) do
|
||||
children = [
|
||||
# Starts a worker by calling: Bwc.Worker.start_link(arg)
|
||||
# {Bwc.Worker, arg}
|
||||
{Phoenix.PubSub, name: BwcWeb.PubSub}
|
||||
]
|
||||
|
||||
# See https://hexdocs.pm/elixir/Supervisor.html
|
||||
# for other strategies and supported options
|
||||
opts = [strategy: :one_for_one, name: Bwc.Supervisor]
|
||||
Supervisor.start_link(children, opts)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,26 @@
|
||||
defmodule Bwc.Card do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "cards" do
|
||||
field :title, :string
|
||||
field :description, :string
|
||||
field :picture, :string
|
||||
field :played, :boolean
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def delete(%Bwc.Card{} = card), do: Bwc.delete_card(card)
|
||||
|
||||
def changeset(card, attrs \\ %{}) do
|
||||
card
|
||||
|> cast(attrs, [:title, :picture, :description, :played])
|
||||
|> validate_required(:title)
|
||||
|> validate_length(:title, min: 3)
|
||||
end
|
||||
|
||||
def format_description(card) do
|
||||
card.description
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,4 @@
|
||||
defmodule Bwc.Card do
|
||||
defstruct [:id, :title, :picture, :description]
|
||||
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
defmodule Bwc.Repo do
|
||||
use Ecto.Repo,
|
||||
otp_app: :bwc,
|
||||
adapter: Ecto.Adapters.Postgres
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
defmodule Auction.Repo do
|
||||
use Ecto.Repo,
|
||||
otp_app: :bwc,
|
||||
adapter: Ecto.Adapters.Postgres
|
||||
end
|
||||
@@ -0,0 +1,23 @@
|
||||
defmodule Bwc.TestRepo do
|
||||
alias Bwc.Card
|
||||
|
||||
@cards [
|
||||
%Card{id: 0, title: "Baby's First Card", picture: "0.jpg", description: "This card does nothing."},
|
||||
%Card{id: 0, title: "YUGE", picture: "1.jpg", description: ""},
|
||||
%Card{id: 0, title: "Test Card", picture: "2.jpg", description: "Angelica approves."}
|
||||
]
|
||||
|
||||
def all(Card), do: @cards
|
||||
|
||||
def get!(Card, id), do: Enum.find(@cards,fn(card) -> card.id == id end)
|
||||
|
||||
def get_by(Card, attrs) do
|
||||
Enum.find(@cards, fn(card) ->
|
||||
|
||||
Enum.all?(Map.keys(attrs), fn(key) ->
|
||||
Map.get(card,key) == attrs[key]
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,10 @@
|
||||
defmodule Bwc.TestRepo do
|
||||
alias Bwc.Card
|
||||
|
||||
@cards = [
|
||||
%Card{id: 0, title: "Baby's First Card", picture: "0.jpg", description: "This card does nothing."},
|
||||
%Card{id: 0, title: "YUGE", picture: "1.jpg", description: ""},
|
||||
%Card{id: 0, title: "Test Card", picture: "2.jpg", description: "Angelica approves."}
|
||||
]
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user