defmodule BwcWeb.CardController do
  use BwcWeb, :controller

  def show(conn, params) do
    {id,_} = Integer.parse(params["id"])
    card = Bwc.get_card(id)
    render(conn, "show.html", card: card)
  end

  def new(conn, _params) do
    card = Bwc.new_card()
    render(conn, "new.html", card: card)
  end

  def create(conn,card_params) do
      case Bwc.create_card(Map.get(card_params,"card")) do
        {:ok, card} -> redirect(conn, to: Routes.card_path(conn, :show, card))
        {:error, card} -> render(conn, "new.html", card: card)
      end

  end

  def delete(conn, params) do
    id = Map.get(params, "id")
    {:ok, card} = Bwc.delete_card(Bwc.get_card(id))
    render(conn, "delete.json", card: card)
  end

  def index(conn, _params) do
    cards = Bwc.list_cards()
    render(conn, "index.html", cards: cards)
  end

end
