Autogen registration UI.
This commit is contained in:
104
lib/runosaari/registration.ex
Normal file
104
lib/runosaari/registration.ex
Normal file
@@ -0,0 +1,104 @@
|
||||
defmodule Runosaari.Registration do
|
||||
@moduledoc """
|
||||
The Registration context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Runosaari.Repo
|
||||
|
||||
alias Runosaari.Registration.Performer
|
||||
|
||||
@doc """
|
||||
Returns the list of performers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_performers()
|
||||
[%Performer{}, ...]
|
||||
|
||||
"""
|
||||
def list_performers do
|
||||
Repo.all(Performer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single performer.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Performer does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_performer!(123)
|
||||
%Performer{}
|
||||
|
||||
iex> get_performer!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_performer!(id), do: Repo.get!(Performer, id)
|
||||
|
||||
@doc """
|
||||
Creates a performer.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_performer(%{field: value})
|
||||
{:ok, %Performer{}}
|
||||
|
||||
iex> create_performer(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_performer(attrs \\ %{}) do
|
||||
%Performer{}
|
||||
|> Performer.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a performer.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_performer(performer, %{field: new_value})
|
||||
{:ok, %Performer{}}
|
||||
|
||||
iex> update_performer(performer, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_performer(%Performer{} = performer, attrs) do
|
||||
performer
|
||||
|> Performer.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a performer.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_performer(performer)
|
||||
{:ok, %Performer{}}
|
||||
|
||||
iex> delete_performer(performer)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_performer(%Performer{} = performer) do
|
||||
Repo.delete(performer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking performer changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_performer(performer)
|
||||
%Ecto.Changeset{data: %Performer{}}
|
||||
|
||||
"""
|
||||
def change_performer(%Performer{} = performer, attrs \\ %{}) do
|
||||
Performer.changeset(performer, attrs)
|
||||
end
|
||||
end
|
||||
25
lib/runosaari/registration/performer.ex
Normal file
25
lib/runosaari/registration/performer.ex
Normal file
@@ -0,0 +1,25 @@
|
||||
defmodule Runosaari.Registration.Performer do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "performers" do
|
||||
field :confirmed, :boolean, default: false
|
||||
field :email, :string
|
||||
field :fname, :string
|
||||
field :lname, :string
|
||||
field :notes, :string
|
||||
field :performerId, :integer
|
||||
field :tel, :string
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(performer, attrs) do
|
||||
performer
|
||||
|> cast(attrs, [:performerId, :fname, :lname, :email, :tel, :confirmed, :notes])
|
||||
|> validate_required([:performerId, :fname, :lname, :email, :tel, :confirmed, :notes])
|
||||
|> unique_constraint(:performerId)
|
||||
|> unique_constraint(:email)
|
||||
end
|
||||
end
|
||||
62
lib/runosaari_web/controllers/performer_controller.ex
Normal file
62
lib/runosaari_web/controllers/performer_controller.ex
Normal file
@@ -0,0 +1,62 @@
|
||||
defmodule RunosaariWeb.PerformerController do
|
||||
use RunosaariWeb, :controller
|
||||
|
||||
alias Runosaari.Registration
|
||||
alias Runosaari.Registration.Performer
|
||||
|
||||
def index(conn, _params) do
|
||||
performers = Registration.list_performers()
|
||||
render(conn, "index.html", performers: performers)
|
||||
end
|
||||
|
||||
def new(conn, _params) do
|
||||
changeset = Registration.change_performer(%Performer{})
|
||||
render(conn, "new.html", changeset: changeset)
|
||||
end
|
||||
|
||||
def create(conn, %{"performer" => performer_params}) do
|
||||
case Registration.create_performer(performer_params) do
|
||||
{:ok, performer} ->
|
||||
conn
|
||||
|> put_flash(:info, "Performer created successfully.")
|
||||
|> redirect(to: Routes.performer_path(conn, :show, performer))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "new.html", changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
performer = Registration.get_performer!(id)
|
||||
render(conn, "show.html", performer: performer)
|
||||
end
|
||||
|
||||
def edit(conn, %{"id" => id}) do
|
||||
performer = Registration.get_performer!(id)
|
||||
changeset = Registration.change_performer(performer)
|
||||
render(conn, "edit.html", performer: performer, changeset: changeset)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "performer" => performer_params}) do
|
||||
performer = Registration.get_performer!(id)
|
||||
|
||||
case Registration.update_performer(performer, performer_params) do
|
||||
{:ok, performer} ->
|
||||
conn
|
||||
|> put_flash(:info, "Performer updated successfully.")
|
||||
|> redirect(to: Routes.performer_path(conn, :show, performer))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "edit.html", performer: performer, changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
performer = Registration.get_performer!(id)
|
||||
{:ok, _performer} = Registration.delete_performer(performer)
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "Performer deleted successfully.")
|
||||
|> redirect(to: Routes.performer_path(conn, :index))
|
||||
end
|
||||
end
|
||||
@@ -17,6 +17,7 @@ defmodule RunosaariWeb.Router do
|
||||
pipe_through :browser
|
||||
|
||||
get "/", PageController, :index
|
||||
resources "/performers", PerformerController
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
||||
5
lib/runosaari_web/templates/performer/edit.html.eex
Normal file
5
lib/runosaari_web/templates/performer/edit.html.eex
Normal file
@@ -0,0 +1,5 @@
|
||||
<h1>Edit Performer</h1>
|
||||
|
||||
<%= render "form.html", Map.put(assigns, :action, Routes.performer_path(@conn, :update, @performer)) %>
|
||||
|
||||
<span><%= link "Back", to: Routes.performer_path(@conn, :index) %></span>
|
||||
39
lib/runosaari_web/templates/performer/form.html.eex
Normal file
39
lib/runosaari_web/templates/performer/form.html.eex
Normal file
@@ -0,0 +1,39 @@
|
||||
<%= form_for @changeset, @action, fn f -> %>
|
||||
<%= if @changeset.action do %>
|
||||
<div class="alert alert-danger">
|
||||
<p>Oops, something went wrong! Please check the errors below.</p>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= label f, :performerId %>
|
||||
<%= number_input f, :performerId %>
|
||||
<%= error_tag f, :performerId %>
|
||||
|
||||
<%= label f, :fname %>
|
||||
<%= text_input f, :fname %>
|
||||
<%= error_tag f, :fname %>
|
||||
|
||||
<%= label f, :lname %>
|
||||
<%= text_input f, :lname %>
|
||||
<%= error_tag f, :lname %>
|
||||
|
||||
<%= label f, :email %>
|
||||
<%= text_input f, :email %>
|
||||
<%= error_tag f, :email %>
|
||||
|
||||
<%= label f, :tel %>
|
||||
<%= text_input f, :tel %>
|
||||
<%= error_tag f, :tel %>
|
||||
|
||||
<%= label f, :confirmed %>
|
||||
<%= checkbox f, :confirmed %>
|
||||
<%= error_tag f, :confirmed %>
|
||||
|
||||
<%= label f, :notes %>
|
||||
<%= text_input f, :notes %>
|
||||
<%= error_tag f, :notes %>
|
||||
|
||||
<div>
|
||||
<%= submit "Save" %>
|
||||
</div>
|
||||
<% end %>
|
||||
38
lib/runosaari_web/templates/performer/index.html.eex
Normal file
38
lib/runosaari_web/templates/performer/index.html.eex
Normal file
@@ -0,0 +1,38 @@
|
||||
<h1>Listing Performers</h1>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Performerid</th>
|
||||
<th>Fname</th>
|
||||
<th>Lname</th>
|
||||
<th>Email</th>
|
||||
<th>Tel</th>
|
||||
<th>Confirmed</th>
|
||||
<th>Notes</th>
|
||||
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<%= for performer <- @performers do %>
|
||||
<tr>
|
||||
<td><%= performer.performerId %></td>
|
||||
<td><%= performer.fname %></td>
|
||||
<td><%= performer.lname %></td>
|
||||
<td><%= performer.email %></td>
|
||||
<td><%= performer.tel %></td>
|
||||
<td><%= performer.confirmed %></td>
|
||||
<td><%= performer.notes %></td>
|
||||
|
||||
<td>
|
||||
<span><%= link "Show", to: Routes.performer_path(@conn, :show, performer) %></span>
|
||||
<span><%= link "Edit", to: Routes.performer_path(@conn, :edit, performer) %></span>
|
||||
<span><%= link "Delete", to: Routes.performer_path(@conn, :delete, performer), method: :delete, data: [confirm: "Are you sure?"] %></span>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<span><%= link "New Performer", to: Routes.performer_path(@conn, :new) %></span>
|
||||
5
lib/runosaari_web/templates/performer/new.html.eex
Normal file
5
lib/runosaari_web/templates/performer/new.html.eex
Normal file
@@ -0,0 +1,5 @@
|
||||
<h1>New Performer</h1>
|
||||
|
||||
<%= render "form.html", Map.put(assigns, :action, Routes.performer_path(@conn, :create)) %>
|
||||
|
||||
<span><%= link "Back", to: Routes.performer_path(@conn, :index) %></span>
|
||||
43
lib/runosaari_web/templates/performer/show.html.eex
Normal file
43
lib/runosaari_web/templates/performer/show.html.eex
Normal file
@@ -0,0 +1,43 @@
|
||||
<h1>Show Performer</h1>
|
||||
|
||||
<ul>
|
||||
|
||||
<li>
|
||||
<strong>Performerid:</strong>
|
||||
<%= @performer.performerId %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Fname:</strong>
|
||||
<%= @performer.fname %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Lname:</strong>
|
||||
<%= @performer.lname %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Email:</strong>
|
||||
<%= @performer.email %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Tel:</strong>
|
||||
<%= @performer.tel %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Confirmed:</strong>
|
||||
<%= @performer.confirmed %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Notes:</strong>
|
||||
<%= @performer.notes %>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<span><%= link "Edit", to: Routes.performer_path(@conn, :edit, @performer) %></span>
|
||||
<span><%= link "Back", to: Routes.performer_path(@conn, :index) %></span>
|
||||
3
lib/runosaari_web/views/performer_view.ex
Normal file
3
lib/runosaari_web/views/performer_view.ex
Normal file
@@ -0,0 +1,3 @@
|
||||
defmodule RunosaariWeb.PerformerView do
|
||||
use RunosaariWeb, :view
|
||||
end
|
||||
20
priv/repo/migrations/20210327142456_create_performers.exs
Normal file
20
priv/repo/migrations/20210327142456_create_performers.exs
Normal file
@@ -0,0 +1,20 @@
|
||||
defmodule Runosaari.Repo.Migrations.CreatePerformers do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:performers) do
|
||||
add :performerId, :integer
|
||||
add :fname, :string
|
||||
add :lname, :string
|
||||
add :email, :string
|
||||
add :tel, :string
|
||||
add :confirmed, :boolean, default: false, null: false
|
||||
add :notes, :string
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
create unique_index(:performers, [:performerId])
|
||||
create unique_index(:performers, [:email])
|
||||
end
|
||||
end
|
||||
76
test/runosaari/registration_test.exs
Normal file
76
test/runosaari/registration_test.exs
Normal file
@@ -0,0 +1,76 @@
|
||||
defmodule Runosaari.RegistrationTest do
|
||||
use Runosaari.DataCase
|
||||
|
||||
alias Runosaari.Registration
|
||||
|
||||
describe "performers" do
|
||||
alias Runosaari.Registration.Performer
|
||||
|
||||
@valid_attrs %{confirmed: true, email: "some email", fname: "some fname", lname: "some lname", notes: "some notes", performerId: 42, tel: "some tel"}
|
||||
@update_attrs %{confirmed: false, email: "some updated email", fname: "some updated fname", lname: "some updated lname", notes: "some updated notes", performerId: 43, tel: "some updated tel"}
|
||||
@invalid_attrs %{confirmed: nil, email: nil, fname: nil, lname: nil, notes: nil, performerId: nil, tel: nil}
|
||||
|
||||
def performer_fixture(attrs \\ %{}) do
|
||||
{:ok, performer} =
|
||||
attrs
|
||||
|> Enum.into(@valid_attrs)
|
||||
|> Registration.create_performer()
|
||||
|
||||
performer
|
||||
end
|
||||
|
||||
test "list_performers/0 returns all performers" do
|
||||
performer = performer_fixture()
|
||||
assert Registration.list_performers() == [performer]
|
||||
end
|
||||
|
||||
test "get_performer!/1 returns the performer with given id" do
|
||||
performer = performer_fixture()
|
||||
assert Registration.get_performer!(performer.id) == performer
|
||||
end
|
||||
|
||||
test "create_performer/1 with valid data creates a performer" do
|
||||
assert {:ok, %Performer{} = performer} = Registration.create_performer(@valid_attrs)
|
||||
assert performer.confirmed == true
|
||||
assert performer.email == "some email"
|
||||
assert performer.fname == "some fname"
|
||||
assert performer.lname == "some lname"
|
||||
assert performer.notes == "some notes"
|
||||
assert performer.performerId == 42
|
||||
assert performer.tel == "some tel"
|
||||
end
|
||||
|
||||
test "create_performer/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Registration.create_performer(@invalid_attrs)
|
||||
end
|
||||
|
||||
test "update_performer/2 with valid data updates the performer" do
|
||||
performer = performer_fixture()
|
||||
assert {:ok, %Performer{} = performer} = Registration.update_performer(performer, @update_attrs)
|
||||
assert performer.confirmed == false
|
||||
assert performer.email == "some updated email"
|
||||
assert performer.fname == "some updated fname"
|
||||
assert performer.lname == "some updated lname"
|
||||
assert performer.notes == "some updated notes"
|
||||
assert performer.performerId == 43
|
||||
assert performer.tel == "some updated tel"
|
||||
end
|
||||
|
||||
test "update_performer/2 with invalid data returns error changeset" do
|
||||
performer = performer_fixture()
|
||||
assert {:error, %Ecto.Changeset{}} = Registration.update_performer(performer, @invalid_attrs)
|
||||
assert performer == Registration.get_performer!(performer.id)
|
||||
end
|
||||
|
||||
test "delete_performer/1 deletes the performer" do
|
||||
performer = performer_fixture()
|
||||
assert {:ok, %Performer{}} = Registration.delete_performer(performer)
|
||||
assert_raise Ecto.NoResultsError, fn -> Registration.get_performer!(performer.id) end
|
||||
end
|
||||
|
||||
test "change_performer/1 returns a performer changeset" do
|
||||
performer = performer_fixture()
|
||||
assert %Ecto.Changeset{} = Registration.change_performer(performer)
|
||||
end
|
||||
end
|
||||
end
|
||||
88
test/runosaari_web/controllers/performer_controller_test.exs
Normal file
88
test/runosaari_web/controllers/performer_controller_test.exs
Normal file
@@ -0,0 +1,88 @@
|
||||
defmodule RunosaariWeb.PerformerControllerTest do
|
||||
use RunosaariWeb.ConnCase
|
||||
|
||||
alias Runosaari.Registration
|
||||
|
||||
@create_attrs %{confirmed: true, email: "some email", fname: "some fname", lname: "some lname", notes: "some notes", performerId: 42, tel: "some tel"}
|
||||
@update_attrs %{confirmed: false, email: "some updated email", fname: "some updated fname", lname: "some updated lname", notes: "some updated notes", performerId: 43, tel: "some updated tel"}
|
||||
@invalid_attrs %{confirmed: nil, email: nil, fname: nil, lname: nil, notes: nil, performerId: nil, tel: nil}
|
||||
|
||||
def fixture(:performer) do
|
||||
{:ok, performer} = Registration.create_performer(@create_attrs)
|
||||
performer
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
test "lists all performers", %{conn: conn} do
|
||||
conn = get(conn, Routes.performer_path(conn, :index))
|
||||
assert html_response(conn, 200) =~ "Listing Performers"
|
||||
end
|
||||
end
|
||||
|
||||
describe "new performer" do
|
||||
test "renders form", %{conn: conn} do
|
||||
conn = get(conn, Routes.performer_path(conn, :new))
|
||||
assert html_response(conn, 200) =~ "New Performer"
|
||||
end
|
||||
end
|
||||
|
||||
describe "create performer" do
|
||||
test "redirects to show when data is valid", %{conn: conn} do
|
||||
conn = post(conn, Routes.performer_path(conn, :create), performer: @create_attrs)
|
||||
|
||||
assert %{id: id} = redirected_params(conn)
|
||||
assert redirected_to(conn) == Routes.performer_path(conn, :show, id)
|
||||
|
||||
conn = get(conn, Routes.performer_path(conn, :show, id))
|
||||
assert html_response(conn, 200) =~ "Show Performer"
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn} do
|
||||
conn = post(conn, Routes.performer_path(conn, :create), performer: @invalid_attrs)
|
||||
assert html_response(conn, 200) =~ "New Performer"
|
||||
end
|
||||
end
|
||||
|
||||
describe "edit performer" do
|
||||
setup [:create_performer]
|
||||
|
||||
test "renders form for editing chosen performer", %{conn: conn, performer: performer} do
|
||||
conn = get(conn, Routes.performer_path(conn, :edit, performer))
|
||||
assert html_response(conn, 200) =~ "Edit Performer"
|
||||
end
|
||||
end
|
||||
|
||||
describe "update performer" do
|
||||
setup [:create_performer]
|
||||
|
||||
test "redirects when data is valid", %{conn: conn, performer: performer} do
|
||||
conn = put(conn, Routes.performer_path(conn, :update, performer), performer: @update_attrs)
|
||||
assert redirected_to(conn) == Routes.performer_path(conn, :show, performer)
|
||||
|
||||
conn = get(conn, Routes.performer_path(conn, :show, performer))
|
||||
assert html_response(conn, 200) =~ "some updated email"
|
||||
end
|
||||
|
||||
test "renders errors when data is invalid", %{conn: conn, performer: performer} do
|
||||
conn = put(conn, Routes.performer_path(conn, :update, performer), performer: @invalid_attrs)
|
||||
assert html_response(conn, 200) =~ "Edit Performer"
|
||||
end
|
||||
end
|
||||
|
||||
describe "delete performer" do
|
||||
setup [:create_performer]
|
||||
|
||||
test "deletes chosen performer", %{conn: conn, performer: performer} do
|
||||
conn = delete(conn, Routes.performer_path(conn, :delete, performer))
|
||||
assert redirected_to(conn) == Routes.performer_path(conn, :index)
|
||||
assert_error_sent 404, fn ->
|
||||
get(conn, Routes.performer_path(conn, :show, performer))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp create_performer(_) do
|
||||
performer = fixture(:performer)
|
||||
%{performer: performer}
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user