fp/services/bright/lib/bright_web/controllers/stream_controller.ex

67 lines
1.7 KiB
Elixir

defmodule BrightWeb.StreamController do
use BrightWeb, :controller
alias Bright.Streams
alias Bright.Streams.Stream
def index(conn, _params) do
streams = Streams.list_streams()
render(conn, :index, streams: streams)
end
def new(conn, _params) do
changeset = Streams.change_stream(%Stream{})
render(conn, :new, changeset: changeset)
end
def create(conn, %{"stream" => stream_params}) do
case Streams.create_stream(stream_params) do
{:ok, stream} ->
conn
|> put_flash(:info, "Stream created successfully.")
|> redirect(to: ~p"/streams/#{stream}")
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :new, changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
stream =
id
|> Streams.get_stream!()
|> Streams.inc_page_views()
render(conn, :show, stream: stream)
end
def edit(conn, %{"id" => id}) do
stream = Streams.get_stream!(id)
changeset = Streams.change_stream(stream)
render(conn, :edit, stream: stream, changeset: changeset)
end
def update(conn, %{"id" => id, "stream" => stream_params}) do
stream = Streams.get_stream!(id)
case Streams.update_stream(stream, stream_params) do
{:ok, stream} ->
conn
|> put_flash(:info, "Stream updated successfully.")
|> redirect(to: ~p"/streams/#{stream}")
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, :edit, stream: stream, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
stream = Streams.get_stream!(id)
{:ok, _stream} = Streams.delete_stream(stream)
conn
|> put_flash(:info, "Stream deleted successfully.")
|> redirect(to: ~p"/streams")
end
end