defmodule ZoeyscomputerWeb.GistController do
  use ZoeyscomputerWeb, :controller

  alias Zoeyscomputer.Gists
  alias Zoeyscomputer.Gists.Gist

  action_fallback ZoeyscomputerWeb.FallbackController

  def index(conn, _params) do
    gists = Gists.list_gists()
    render(conn, :index, gists: gists)
  end

  def create(conn, %{"gist" => gist_params}) do
    with {:ok, %Gist{} = gist} <- Gists.create_gist(gist_params) do
      conn
      |> put_status(:created)
      |> put_resp_header("location", ~p"/api/gists/#{gist}")
      |> render(:show, gist: gist)
    end
  end

  def show(conn, %{"id" => id}) do
    gist = Gists.get_gist!(id)
    render(conn, :show, gist: gist)
  end

  def update(conn, %{"id" => id, "gist" => gist_params}) do
    gist = Gists.get_gist!(id)

    with {:ok, %Gist{} = gist} <- Gists.update_gist(gist, gist_params) do
      render(conn, :show, gist: gist)
    end
  end

  def delete(conn, %{"id" => id}) do
    gist = Gists.get_gist!(id)

    with {:ok, %Gist{}} <- Gists.delete_gist(gist) do
      send_resp(conn, :no_content, "")
    end
  end
end
