Skip to content

Commit

Permalink
Ran html generator for mediaprofile model - leaving as-is for now
Browse files Browse the repository at this point in the history
  • Loading branch information
kieraneglin committed Jan 23, 2024
1 parent b3def0d commit b29dec7
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 0 deletions.
104 changes: 104 additions & 0 deletions lib/pinchflat/profiles.ex
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions lib/pinchflat/profiles/media_profile.ex
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lib/pinchflat_web/controllers/media_profiles/media_profile_html.ex
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<.header>
Edit Media profile <%= @media_profile.id %>
<:subtitle>Use this form to manage media_profile records in your database.</:subtitle>
</.header>

<.media_profile_form changeset={@changeset} action={~p"/media_profiles/#{@media_profile}"} />

<.back navigate={~p"/media_profiles"}>Back to media_profiles</.back>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<.header>
Listing Media profiles
<:actions>
<.link href={~p"/media_profiles/new"}>
<.button>New Media profile</.button>
</.link>
</:actions>
</.header>

<.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>
<:col :let={media_profile} label="Output path template">
<%= media_profile.output_path_template %>
</:col>
<:action :let={media_profile}>
<div class="sr-only">
<.link navigate={~p"/media_profiles/#{media_profile}"}>Show</.link>
</div>
<.link navigate={~p"/media_profiles/#{media_profile}/edit"}>Edit</.link>
</:action>
<:action :let={media_profile}>
<.link href={~p"/media_profiles/#{media_profile}"} method="delete" data-confirm="Are you sure?">
Delete
</.link>
</:action>
</.table>
Original file line number Diff line number Diff line change
@@ -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.
</.error>
<.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</.button>
</:actions>
</.simple_form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<.header>
New Media profile
<:subtitle>Use this form to manage media_profile records in your database.</:subtitle>
</.header>

<.media_profile_form changeset={@changeset} action={~p"/media_profiles"} />

<.back navigate={~p"/media_profiles"}>Back to media_profiles</.back>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<.header>
Media profile <%= @media_profile.id %>
<:subtitle>This is a media_profile record from your database.</:subtitle>
<:actions>
<.link href={~p"/media_profiles/#{@media_profile}/edit"}>
<.button>Edit media_profile</.button>
</.link>
</:actions>
</.header>

<.list>
<:item title="Name"><%= @media_profile.name %></:item>
<:item title="Output path template"><%= @media_profile.output_path_template %></:item>
</.list>

<.back navigate={~p"/media_profiles"}>Back to media_profiles</.back>
2 changes: 2 additions & 0 deletions lib/pinchflat_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions priv/repo/migrations/20240122030944_create_media_profiles.exs
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions test/pinchflat/profiles_test.exs
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit b29dec7

Please sign in to comment.