AUTOGEN: Performances.

This commit is contained in:
codevictory
2021-03-30 22:50:21 +03:00
parent adacd91591
commit 1ae7c26b5a
12 changed files with 450 additions and 0 deletions

104
lib/runosaari/schedule.ex Normal file
View File

@@ -0,0 +1,104 @@
defmodule Runosaari.Schedule do
@moduledoc """
The Schedule context.
"""
import Ecto.Query, warn: false
alias Runosaari.Repo
alias Runosaari.Schedule.Performance
@doc """
Returns the list of performances.
## Examples
iex> list_performances()
[%Performance{}, ...]
"""
def list_performances do
Repo.all(Performance)
end
@doc """
Gets a single performance.
Raises `Ecto.NoResultsError` if the Performance does not exist.
## Examples
iex> get_performance!(123)
%Performance{}
iex> get_performance!(456)
** (Ecto.NoResultsError)
"""
def get_performance!(id), do: Repo.get!(Performance, id)
@doc """
Creates a performance.
## Examples
iex> create_performance(%{field: value})
{:ok, %Performance{}}
iex> create_performance(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_performance(attrs \\ %{}) do
%Performance{}
|> Performance.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a performance.
## Examples
iex> update_performance(performance, %{field: new_value})
{:ok, %Performance{}}
iex> update_performance(performance, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_performance(%Performance{} = performance, attrs) do
performance
|> Performance.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a performance.
## Examples
iex> delete_performance(performance)
{:ok, %Performance{}}
iex> delete_performance(performance)
{:error, %Ecto.Changeset{}}
"""
def delete_performance(%Performance{} = performance) do
Repo.delete(performance)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking performance changes.
## Examples
iex> change_performance(performance)
%Ecto.Changeset{data: %Performance{}}
"""
def change_performance(%Performance{} = performance, attrs \\ %{}) do
Performance.changeset(performance, attrs)
end
end

View File

@@ -0,0 +1,21 @@
defmodule Runosaari.Schedule.Performance do
use Ecto.Schema
import Ecto.Changeset
schema "performances" do
field :description, :string
field :notes, :string
field :time, :naive_datetime
field :location_id, :id
field :performer_id, :id
timestamps()
end
@doc false
def changeset(performance, attrs) do
performance
|> cast(attrs, [:time, :description, :notes])
|> validate_required([:time, :description, :notes])
end
end

View File

@@ -0,0 +1,62 @@
defmodule RunosaariWeb.PerformanceController do
use RunosaariWeb, :controller
alias Runosaari.Schedule
alias Runosaari.Schedule.Performance
def index(conn, _params) do
performances = Schedule.list_performances()
render(conn, "index.html", performances: performances)
end
def new(conn, _params) do
changeset = Schedule.change_performance(%Performance{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"performance" => performance_params}) do
case Schedule.create_performance(performance_params) do
{:ok, performance} ->
conn
|> put_flash(:info, "Performance created successfully.")
|> redirect(to: Routes.performance_path(conn, :show, performance))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
performance = Schedule.get_performance!(id)
render(conn, "show.html", performance: performance)
end
def edit(conn, %{"id" => id}) do
performance = Schedule.get_performance!(id)
changeset = Schedule.change_performance(performance)
render(conn, "edit.html", performance: performance, changeset: changeset)
end
def update(conn, %{"id" => id, "performance" => performance_params}) do
performance = Schedule.get_performance!(id)
case Schedule.update_performance(performance, performance_params) do
{:ok, performance} ->
conn
|> put_flash(:info, "Performance updated successfully.")
|> redirect(to: Routes.performance_path(conn, :show, performance))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", performance: performance, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
performance = Schedule.get_performance!(id)
{:ok, _performance} = Schedule.delete_performance(performance)
conn
|> put_flash(:info, "Performance deleted successfully.")
|> redirect(to: Routes.performance_path(conn, :index))
end
end

View File

@@ -0,0 +1,5 @@
<h1>Edit Performance</h1>
<%= render "form.html", Map.put(assigns, :action, Routes.performance_path(@conn, :update, @performance)) %>
<span><%= link "Back", to: Routes.performance_path(@conn, :index) %></span>

View File

@@ -0,0 +1,23 @@
<%= 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, :time %>
<%= datetime_select f, :time %>
<%= error_tag f, :time %>
<%= label f, :description %>
<%= text_input f, :description %>
<%= error_tag f, :description %>
<%= label f, :notes %>
<%= text_input f, :notes %>
<%= error_tag f, :notes %>
<div>
<%= submit "Save" %>
</div>
<% end %>

View File

@@ -0,0 +1,30 @@
<h1>Listing Performances</h1>
<table>
<thead>
<tr>
<th>Time</th>
<th>Description</th>
<th>Notes</th>
<th></th>
</tr>
</thead>
<tbody>
<%= for performance <- @performances do %>
<tr>
<td><%= performance.time %></td>
<td><%= performance.description %></td>
<td><%= performance.notes %></td>
<td>
<span><%= link "Show", to: Routes.performance_path(@conn, :show, performance) %></span>
<span><%= link "Edit", to: Routes.performance_path(@conn, :edit, performance) %></span>
<span><%= link "Delete", to: Routes.performance_path(@conn, :delete, performance), method: :delete, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= link "New Performance", to: Routes.performance_path(@conn, :new) %></span>

View File

@@ -0,0 +1,5 @@
<h1>New Performance</h1>
<%= render "form.html", Map.put(assigns, :action, Routes.performance_path(@conn, :create)) %>
<span><%= link "Back", to: Routes.performance_path(@conn, :index) %></span>

View File

@@ -0,0 +1,23 @@
<h1>Show Performance</h1>
<ul>
<li>
<strong>Time:</strong>
<%= @performance.time %>
</li>
<li>
<strong>Description:</strong>
<%= @performance.description %>
</li>
<li>
<strong>Notes:</strong>
<%= @performance.notes %>
</li>
</ul>
<span><%= link "Edit", to: Routes.performance_path(@conn, :edit, @performance) %></span>
<span><%= link "Back", to: Routes.performance_path(@conn, :index) %></span>

View File

@@ -0,0 +1,3 @@
defmodule RunosaariWeb.PerformanceView do
use RunosaariWeb, :view
end

View File

@@ -0,0 +1,18 @@
defmodule Runosaari.Repo.Migrations.CreatePerformances do
use Ecto.Migration
def change do
create table(:performances) do
add :time, :naive_datetime
add :description, :string
add :notes, :string
add :location_id, references(:locations, on_delete: :nothing)
add :performer_id, references(:performers, on_delete: :nothing)
timestamps()
end
create index(:performances, [:location_id])
create index(:performances, [:performer_id])
end
end

View File

@@ -0,0 +1,68 @@
defmodule Runosaari.ScheduleTest do
use Runosaari.DataCase
alias Runosaari.Schedule
describe "performances" do
alias Runosaari.Schedule.Performance
@valid_attrs %{description: "some description", notes: "some notes", time: ~N[2010-04-17 14:00:00]}
@update_attrs %{description: "some updated description", notes: "some updated notes", time: ~N[2011-05-18 15:01:01]}
@invalid_attrs %{description: nil, notes: nil, time: nil}
def performance_fixture(attrs \\ %{}) do
{:ok, performance} =
attrs
|> Enum.into(@valid_attrs)
|> Schedule.create_performance()
performance
end
test "list_performances/0 returns all performances" do
performance = performance_fixture()
assert Schedule.list_performances() == [performance]
end
test "get_performance!/1 returns the performance with given id" do
performance = performance_fixture()
assert Schedule.get_performance!(performance.id) == performance
end
test "create_performance/1 with valid data creates a performance" do
assert {:ok, %Performance{} = performance} = Schedule.create_performance(@valid_attrs)
assert performance.description == "some description"
assert performance.notes == "some notes"
assert performance.time == ~N[2010-04-17 14:00:00]
end
test "create_performance/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Schedule.create_performance(@invalid_attrs)
end
test "update_performance/2 with valid data updates the performance" do
performance = performance_fixture()
assert {:ok, %Performance{} = performance} = Schedule.update_performance(performance, @update_attrs)
assert performance.description == "some updated description"
assert performance.notes == "some updated notes"
assert performance.time == ~N[2011-05-18 15:01:01]
end
test "update_performance/2 with invalid data returns error changeset" do
performance = performance_fixture()
assert {:error, %Ecto.Changeset{}} = Schedule.update_performance(performance, @invalid_attrs)
assert performance == Schedule.get_performance!(performance.id)
end
test "delete_performance/1 deletes the performance" do
performance = performance_fixture()
assert {:ok, %Performance{}} = Schedule.delete_performance(performance)
assert_raise Ecto.NoResultsError, fn -> Schedule.get_performance!(performance.id) end
end
test "change_performance/1 returns a performance changeset" do
performance = performance_fixture()
assert %Ecto.Changeset{} = Schedule.change_performance(performance)
end
end
end

View File

@@ -0,0 +1,88 @@
defmodule RunosaariWeb.PerformanceControllerTest do
use RunosaariWeb.ConnCase
alias Runosaari.Schedule
@create_attrs %{description: "some description", notes: "some notes", time: ~N[2010-04-17 14:00:00]}
@update_attrs %{description: "some updated description", notes: "some updated notes", time: ~N[2011-05-18 15:01:01]}
@invalid_attrs %{description: nil, notes: nil, time: nil}
def fixture(:performance) do
{:ok, performance} = Schedule.create_performance(@create_attrs)
performance
end
describe "index" do
test "lists all performances", %{conn: conn} do
conn = get(conn, Routes.performance_path(conn, :index))
assert html_response(conn, 200) =~ "Listing Performances"
end
end
describe "new performance" do
test "renders form", %{conn: conn} do
conn = get(conn, Routes.performance_path(conn, :new))
assert html_response(conn, 200) =~ "New Performance"
end
end
describe "create performance" do
test "redirects to show when data is valid", %{conn: conn} do
conn = post(conn, Routes.performance_path(conn, :create), performance: @create_attrs)
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.performance_path(conn, :show, id)
conn = get(conn, Routes.performance_path(conn, :show, id))
assert html_response(conn, 200) =~ "Show Performance"
end
test "renders errors when data is invalid", %{conn: conn} do
conn = post(conn, Routes.performance_path(conn, :create), performance: @invalid_attrs)
assert html_response(conn, 200) =~ "New Performance"
end
end
describe "edit performance" do
setup [:create_performance]
test "renders form for editing chosen performance", %{conn: conn, performance: performance} do
conn = get(conn, Routes.performance_path(conn, :edit, performance))
assert html_response(conn, 200) =~ "Edit Performance"
end
end
describe "update performance" do
setup [:create_performance]
test "redirects when data is valid", %{conn: conn, performance: performance} do
conn = put(conn, Routes.performance_path(conn, :update, performance), performance: @update_attrs)
assert redirected_to(conn) == Routes.performance_path(conn, :show, performance)
conn = get(conn, Routes.performance_path(conn, :show, performance))
assert html_response(conn, 200) =~ "some updated description"
end
test "renders errors when data is invalid", %{conn: conn, performance: performance} do
conn = put(conn, Routes.performance_path(conn, :update, performance), performance: @invalid_attrs)
assert html_response(conn, 200) =~ "Edit Performance"
end
end
describe "delete performance" do
setup [:create_performance]
test "deletes chosen performance", %{conn: conn, performance: performance} do
conn = delete(conn, Routes.performance_path(conn, :delete, performance))
assert redirected_to(conn) == Routes.performance_path(conn, :index)
assert_error_sent 404, fn ->
get(conn, Routes.performance_path(conn, :show, performance))
end
end
end
defp create_performance(_) do
performance = fixture(:performance)
%{performance: performance}
end
end