From b29dec729440ba64ec8de23ab0a9954dde943b82 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Mon, 22 Jan 2024 19:39:22 -0800 Subject: [PATCH] Ran html generator for mediaprofile model - leaving as-is for now --- lib/pinchflat/profiles.ex | 104 ++++++++++++++++++ lib/pinchflat/profiles/media_profile.ex | 19 ++++ .../media_profile_controller.ex | 62 +++++++++++ .../media_profiles/media_profile_html.ex | 13 +++ .../media_profile_html/edit.html.heex | 8 ++ .../media_profile_html/index.html.heex | 30 +++++ .../media_profile_form.html.heex | 10 ++ .../media_profile_html/new.html.heex | 8 ++ .../media_profile_html/show.html.heex | 16 +++ lib/pinchflat_web/router.ex | 2 + .../20240122030944_create_media_profiles.exs | 14 +++ test/pinchflat/profiles_test.exs | 70 ++++++++++++ .../media_profile_controller_test.exs | 90 +++++++++++++++ test/support/fixtures/profiles_fixtures.ex | 21 ++++ 14 files changed, 467 insertions(+) create mode 100644 lib/pinchflat/profiles.ex create mode 100644 lib/pinchflat/profiles/media_profile.ex create mode 100644 lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex create mode 100644 lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex create mode 100644 lib/pinchflat_web/controllers/media_profiles/media_profile_html/edit.html.heex create mode 100644 lib/pinchflat_web/controllers/media_profiles/media_profile_html/index.html.heex create mode 100644 lib/pinchflat_web/controllers/media_profiles/media_profile_html/media_profile_form.html.heex create mode 100644 lib/pinchflat_web/controllers/media_profiles/media_profile_html/new.html.heex create mode 100644 lib/pinchflat_web/controllers/media_profiles/media_profile_html/show.html.heex create mode 100644 priv/repo/migrations/20240122030944_create_media_profiles.exs create mode 100644 test/pinchflat/profiles_test.exs create mode 100644 test/pinchflat_web/controllers/media_profile_controller_test.exs create mode 100644 test/support/fixtures/profiles_fixtures.ex diff --git a/lib/pinchflat/profiles.ex b/lib/pinchflat/profiles.ex new file mode 100644 index 00000000..2b58ee88 --- /dev/null +++ b/lib/pinchflat/profiles.ex @@ -0,0 +1,104 @@ +defmodule Pinchflat.Profiles do + @moduledoc """ + The Profiles context. + """ + + import Ecto.Query, warn: false + alias Pinchflat.Repo + + alias Pinchflat.Profiles.MediaProfile + + @doc """ + Returns the list of media_profiles. + + ## Examples + + iex> list_media_profiles() + [%MediaProfile{}, ...] + + """ + def list_media_profiles do + Repo.all(MediaProfile) + end + + @doc """ + Gets a single media_profile. + + Raises `Ecto.NoResultsError` if the Media profile does not exist. + + ## Examples + + iex> get_media_profile!(123) + %MediaProfile{} + + iex> get_media_profile!(456) + ** (Ecto.NoResultsError) + + """ + def get_media_profile!(id), do: Repo.get!(MediaProfile, id) + + @doc """ + Creates a media_profile. + + ## Examples + + iex> create_media_profile(%{field: value}) + {:ok, %MediaProfile{}} + + iex> create_media_profile(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_media_profile(attrs \\ %{}) do + %MediaProfile{} + |> MediaProfile.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a media_profile. + + ## Examples + + iex> update_media_profile(media_profile, %{field: new_value}) + {:ok, %MediaProfile{}} + + iex> update_media_profile(media_profile, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_media_profile(%MediaProfile{} = media_profile, attrs) do + media_profile + |> MediaProfile.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a media_profile. + + ## Examples + + iex> delete_media_profile(media_profile) + {:ok, %MediaProfile{}} + + iex> delete_media_profile(media_profile) + {:error, %Ecto.Changeset{}} + + """ + def delete_media_profile(%MediaProfile{} = media_profile) do + Repo.delete(media_profile) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking media_profile changes. + + ## Examples + + iex> change_media_profile(media_profile) + %Ecto.Changeset{data: %MediaProfile{}} + + """ + def change_media_profile(%MediaProfile{} = media_profile, attrs \\ %{}) do + MediaProfile.changeset(media_profile, attrs) + end +end diff --git a/lib/pinchflat/profiles/media_profile.ex b/lib/pinchflat/profiles/media_profile.ex new file mode 100644 index 00000000..13dd0e48 --- /dev/null +++ b/lib/pinchflat/profiles/media_profile.ex @@ -0,0 +1,19 @@ +defmodule Pinchflat.Profiles.MediaProfile do + use Ecto.Schema + import Ecto.Changeset + + schema "media_profiles" do + field :name, :string + field :output_path_template, :string + + timestamps(type: :utc_datetime) + end + + @doc false + def changeset(media_profile, attrs) do + media_profile + |> cast(attrs, [:name, :output_path_template]) + |> validate_required([:name, :output_path_template]) + |> unique_constraint(:name) + end +end diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex b/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex new file mode 100644 index 00000000..895b2199 --- /dev/null +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_controller.ex @@ -0,0 +1,62 @@ +defmodule PinchflatWeb.MediaProfiles.MediaProfileController do + use PinchflatWeb, :controller + + alias Pinchflat.Profiles + alias Pinchflat.Profiles.MediaProfile + + def index(conn, _params) do + media_profiles = Profiles.list_media_profiles() + render(conn, :index, media_profiles: media_profiles) + end + + def new(conn, _params) do + changeset = Profiles.change_media_profile(%MediaProfile{}) + render(conn, :new, changeset: changeset) + end + + def create(conn, %{"media_profile" => media_profile_params}) do + case Profiles.create_media_profile(media_profile_params) do + {:ok, media_profile} -> + conn + |> put_flash(:info, "Media profile created successfully.") + |> redirect(to: ~p"/media_profiles/#{media_profile}") + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, :new, changeset: changeset) + end + end + + def show(conn, %{"id" => id}) do + media_profile = Profiles.get_media_profile!(id) + render(conn, :show, media_profile: media_profile) + end + + def edit(conn, %{"id" => id}) do + media_profile = Profiles.get_media_profile!(id) + changeset = Profiles.change_media_profile(media_profile) + render(conn, :edit, media_profile: media_profile, changeset: changeset) + end + + def update(conn, %{"id" => id, "media_profile" => media_profile_params}) do + media_profile = Profiles.get_media_profile!(id) + + case Profiles.update_media_profile(media_profile, media_profile_params) do + {:ok, media_profile} -> + conn + |> put_flash(:info, "Media profile updated successfully.") + |> redirect(to: ~p"/media_profiles/#{media_profile}") + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, :edit, media_profile: media_profile, changeset: changeset) + end + end + + def delete(conn, %{"id" => id}) do + media_profile = Profiles.get_media_profile!(id) + {:ok, _media_profile} = Profiles.delete_media_profile(media_profile) + + conn + |> put_flash(:info, "Media profile deleted successfully.") + |> redirect(to: ~p"/media_profiles") + end +end diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex new file mode 100644 index 00000000..edb33ee7 --- /dev/null +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex @@ -0,0 +1,13 @@ +defmodule PinchflatWeb.MediaProfiles.MediaProfileHTML do + use PinchflatWeb, :html + + embed_templates "media_profile_html/*" + + @doc """ + Renders a media_profile form. + """ + attr :changeset, Ecto.Changeset, required: true + attr :action, :string, required: true + + def media_profile_form(assigns) +end diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/edit.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/edit.html.heex new file mode 100644 index 00000000..ca35b198 --- /dev/null +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/edit.html.heex @@ -0,0 +1,8 @@ +<.header> + Edit Media profile <%= @media_profile.id %> + <:subtitle>Use this form to manage media_profile records in your database. + + +<.media_profile_form changeset={@changeset} action={~p"/media_profiles/#{@media_profile}"} /> + +<.back navigate={~p"/media_profiles"}>Back to media_profiles diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index.html.heex new file mode 100644 index 00000000..eec319c3 --- /dev/null +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/index.html.heex @@ -0,0 +1,30 @@ +<.header> + Listing Media profiles + <:actions> + <.link href={~p"/media_profiles/new"}> + <.button>New Media profile + + + + +<.table + id="media_profiles" + rows={@media_profiles} + row_click={&JS.navigate(~p"/media_profiles/#{&1}")} +> + <:col :let={media_profile} label="Name"><%= media_profile.name %> + <:col :let={media_profile} label="Output path template"> + <%= media_profile.output_path_template %> + + <:action :let={media_profile}> +
+ <.link navigate={~p"/media_profiles/#{media_profile}"}>Show +
+ <.link navigate={~p"/media_profiles/#{media_profile}/edit"}>Edit + + <:action :let={media_profile}> + <.link href={~p"/media_profiles/#{media_profile}"} method="delete" data-confirm="Are you sure?"> + Delete + + + diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/media_profile_form.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/media_profile_form.html.heex new file mode 100644 index 00000000..2a3ab664 --- /dev/null +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/media_profile_form.html.heex @@ -0,0 +1,10 @@ +<.simple_form :let={f} for={@changeset} action={@action}> + <.error :if={@changeset.action}> + Oops, something went wrong! Please check the errors below. + + <.input field={f[:name]} type="text" label="Name" /> + <.input field={f[:output_path_template]} type="text" label="Output path template" /> + <:actions> + <.button>Save Media profile + + diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/new.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/new.html.heex new file mode 100644 index 00000000..0887e17e --- /dev/null +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/new.html.heex @@ -0,0 +1,8 @@ +<.header> + New Media profile + <:subtitle>Use this form to manage media_profile records in your database. + + +<.media_profile_form changeset={@changeset} action={~p"/media_profiles"} /> + +<.back navigate={~p"/media_profiles"}>Back to media_profiles diff --git a/lib/pinchflat_web/controllers/media_profiles/media_profile_html/show.html.heex b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/show.html.heex new file mode 100644 index 00000000..80ea07fa --- /dev/null +++ b/lib/pinchflat_web/controllers/media_profiles/media_profile_html/show.html.heex @@ -0,0 +1,16 @@ +<.header> + Media profile <%= @media_profile.id %> + <:subtitle>This is a media_profile record from your database. + <:actions> + <.link href={~p"/media_profiles/#{@media_profile}/edit"}> + <.button>Edit media_profile + + + + +<.list> + <:item title="Name"><%= @media_profile.name %> + <:item title="Output path template"><%= @media_profile.output_path_template %> + + +<.back navigate={~p"/media_profiles"}>Back to media_profiles diff --git a/lib/pinchflat_web/router.ex b/lib/pinchflat_web/router.ex index 186fcb3b..6b62a82e 100644 --- a/lib/pinchflat_web/router.ex +++ b/lib/pinchflat_web/router.ex @@ -18,6 +18,8 @@ defmodule PinchflatWeb.Router do pipe_through :browser get "/", PageController, :home + + resources "/media_profiles", MediaProfiles.MediaProfileController end # Other scopes may use custom stacks. diff --git a/priv/repo/migrations/20240122030944_create_media_profiles.exs b/priv/repo/migrations/20240122030944_create_media_profiles.exs new file mode 100644 index 00000000..5997a442 --- /dev/null +++ b/priv/repo/migrations/20240122030944_create_media_profiles.exs @@ -0,0 +1,14 @@ +defmodule Pinchflat.Repo.Migrations.CreateMediaProfiles do + use Ecto.Migration + + def change do + create table(:media_profiles) do + add :name, :string, null: false + add :output_path_template, :string, null: false + + timestamps(type: :utc_datetime) + end + + create unique_index(:media_profiles, [:name]) + end +end diff --git a/test/pinchflat/profiles_test.exs b/test/pinchflat/profiles_test.exs new file mode 100644 index 00000000..9f54b396 --- /dev/null +++ b/test/pinchflat/profiles_test.exs @@ -0,0 +1,70 @@ +defmodule Pinchflat.ProfilesTest do + use Pinchflat.DataCase + + alias Pinchflat.Profiles + + describe "media_profiles" do + alias Pinchflat.Profiles.MediaProfile + + import Pinchflat.ProfilesFixtures + + @invalid_attrs %{name: nil, output_path_template: nil} + + test "list_media_profiles/0 returns all media_profiles" do + media_profile = media_profile_fixture() + assert Profiles.list_media_profiles() == [media_profile] + end + + test "get_media_profile!/1 returns the media_profile with given id" do + media_profile = media_profile_fixture() + assert Profiles.get_media_profile!(media_profile.id) == media_profile + end + + test "create_media_profile/1 with valid data creates a media_profile" do + valid_attrs = %{name: "some name", output_path_template: "some output_path_template"} + + assert {:ok, %MediaProfile{} = media_profile} = Profiles.create_media_profile(valid_attrs) + assert media_profile.name == "some name" + assert media_profile.output_path_template == "some output_path_template" + end + + test "create_media_profile/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Profiles.create_media_profile(@invalid_attrs) + end + + test "update_media_profile/2 with valid data updates the media_profile" do + media_profile = media_profile_fixture() + + update_attrs = %{ + name: "some updated name", + output_path_template: "some updated output_path_template" + } + + assert {:ok, %MediaProfile{} = media_profile} = + Profiles.update_media_profile(media_profile, update_attrs) + + assert media_profile.name == "some updated name" + assert media_profile.output_path_template == "some updated output_path_template" + end + + test "update_media_profile/2 with invalid data returns error changeset" do + media_profile = media_profile_fixture() + + assert {:error, %Ecto.Changeset{}} = + Profiles.update_media_profile(media_profile, @invalid_attrs) + + assert media_profile == Profiles.get_media_profile!(media_profile.id) + end + + test "delete_media_profile/1 deletes the media_profile" do + media_profile = media_profile_fixture() + assert {:ok, %MediaProfile{}} = Profiles.delete_media_profile(media_profile) + assert_raise Ecto.NoResultsError, fn -> Profiles.get_media_profile!(media_profile.id) end + end + + test "change_media_profile/1 returns a media_profile changeset" do + media_profile = media_profile_fixture() + assert %Ecto.Changeset{} = Profiles.change_media_profile(media_profile) + end + end +end diff --git a/test/pinchflat_web/controllers/media_profile_controller_test.exs b/test/pinchflat_web/controllers/media_profile_controller_test.exs new file mode 100644 index 00000000..b5bac1e5 --- /dev/null +++ b/test/pinchflat_web/controllers/media_profile_controller_test.exs @@ -0,0 +1,90 @@ +defmodule PinchflatWeb.MediaProfileControllerTest do + use PinchflatWeb.ConnCase + + import Pinchflat.ProfilesFixtures + + @create_attrs %{name: "some name", output_path_template: "some output_path_template"} + @update_attrs %{ + name: "some updated name", + output_path_template: "some updated output_path_template" + } + @invalid_attrs %{name: nil, output_path_template: nil} + + describe "index" do + test "lists all media_profiles", %{conn: conn} do + conn = get(conn, ~p"/media_profiles") + assert html_response(conn, 200) =~ "Listing Media profiles" + end + end + + describe "new media_profile" do + test "renders form", %{conn: conn} do + conn = get(conn, ~p"/media_profiles/new") + assert html_response(conn, 200) =~ "New Media profile" + end + end + + describe "create media_profile" do + test "redirects to show when data is valid", %{conn: conn} do + conn = post(conn, ~p"/media_profiles", media_profile: @create_attrs) + + assert %{id: id} = redirected_params(conn) + assert redirected_to(conn) == ~p"/media_profiles/#{id}" + + conn = get(conn, ~p"/media_profiles/#{id}") + assert html_response(conn, 200) =~ "Media profile #{id}" + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = post(conn, ~p"/media_profiles", media_profile: @invalid_attrs) + assert html_response(conn, 200) =~ "New Media profile" + end + end + + describe "edit media_profile" do + setup [:create_media_profile] + + test "renders form for editing chosen media_profile", %{ + conn: conn, + media_profile: media_profile + } do + conn = get(conn, ~p"/media_profiles/#{media_profile}/edit") + assert html_response(conn, 200) =~ "Edit Media profile" + end + end + + describe "update media_profile" do + setup [:create_media_profile] + + test "redirects when data is valid", %{conn: conn, media_profile: media_profile} do + conn = put(conn, ~p"/media_profiles/#{media_profile}", media_profile: @update_attrs) + assert redirected_to(conn) == ~p"/media_profiles/#{media_profile}" + + conn = get(conn, ~p"/media_profiles/#{media_profile}") + assert html_response(conn, 200) =~ "some updated name" + end + + test "renders errors when data is invalid", %{conn: conn, media_profile: media_profile} do + conn = put(conn, ~p"/media_profiles/#{media_profile}", media_profile: @invalid_attrs) + assert html_response(conn, 200) =~ "Edit Media profile" + end + end + + describe "delete media_profile" do + setup [:create_media_profile] + + test "deletes chosen media_profile", %{conn: conn, media_profile: media_profile} do + conn = delete(conn, ~p"/media_profiles/#{media_profile}") + assert redirected_to(conn) == ~p"/media_profiles" + + assert_error_sent 404, fn -> + get(conn, ~p"/media_profiles/#{media_profile}") + end + end + end + + defp create_media_profile(_) do + media_profile = media_profile_fixture() + %{media_profile: media_profile} + end +end diff --git a/test/support/fixtures/profiles_fixtures.ex b/test/support/fixtures/profiles_fixtures.ex new file mode 100644 index 00000000..ef846f5b --- /dev/null +++ b/test/support/fixtures/profiles_fixtures.ex @@ -0,0 +1,21 @@ +defmodule Pinchflat.ProfilesFixtures do + @moduledoc """ + This module defines test helpers for creating + entities via the `Pinchflat.Profiles` context. + """ + + @doc """ + Generate a media_profile. + """ + def media_profile_fixture(attrs \\ %{}) do + {:ok, media_profile} = + attrs + |> Enum.into(%{ + name: "some name", + output_path_template: "some output_path_template" + }) + |> Pinchflat.Profiles.create_media_profile() + + media_profile + end +end