New entity: Visitors.

This commit is contained in:
codevictory
2021-05-03 20:28:36 +03:00
parent 811454f2e7
commit 1308f691a4
12 changed files with 521 additions and 0 deletions

View File

@@ -114,4 +114,100 @@ defmodule Runosaari.Registration do
def change_performer(%Performer{} = performer, attrs \\ %{}) do
Performer.changeset(performer, attrs)
end
alias Runosaari.Registration.Visitor
@doc """
Returns the list of visitors.
## Examples
iex> list_visitors()
[%Visitor{}, ...]
"""
def list_visitors do
Repo.all(Visitor)
end
@doc """
Gets a single visitor.
Raises `Ecto.NoResultsError` if the Visitor does not exist.
## Examples
iex> get_visitor!(123)
%Visitor{}
iex> get_visitor!(456)
** (Ecto.NoResultsError)
"""
def get_visitor!(id), do: Repo.get!(Visitor, id)
@doc """
Creates a visitor.
## Examples
iex> create_visitor(%{field: value})
{:ok, %Visitor{}}
iex> create_visitor(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_visitor(attrs \\ %{}) do
%Visitor{}
|> Visitor.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a visitor.
## Examples
iex> update_visitor(visitor, %{field: new_value})
{:ok, %Visitor{}}
iex> update_visitor(visitor, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_visitor(%Visitor{} = visitor, attrs) do
visitor
|> Visitor.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a visitor.
## Examples
iex> delete_visitor(visitor)
{:ok, %Visitor{}}
iex> delete_visitor(visitor)
{:error, %Ecto.Changeset{}}
"""
def delete_visitor(%Visitor{} = visitor) do
Repo.delete(visitor)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking visitor changes.
## Examples
iex> change_visitor(visitor)
%Ecto.Changeset{data: %Visitor{}}
"""
def change_visitor(%Visitor{} = visitor, attrs \\ %{}) do
Visitor.changeset(visitor, attrs)
end
end

View File

@@ -0,0 +1,25 @@
defmodule Runosaari.Registration.Visitor do
use Ecto.Schema
import Ecto.Changeset
schema "visitors" do
field :accom, :boolean, default: false
field :bus, :boolean, default: false
field :date1, :boolean, default: false
field :date2, :boolean, default: false
field :date3, :boolean, default: false
field :email, :string
field :fname, :string
field :lname, :string
field :tel, :string
timestamps()
end
@doc false
def changeset(visitor, attrs) do
visitor
|> cast(attrs, [:fname, :lname, :email, :tel, :date1, :date2, :date3, :bus, :accom])
|> validate_required([:fname, :lname, :email, :tel, :date1, :date2, :date3, :bus, :accom])
end
end

View File

@@ -0,0 +1,62 @@
defmodule RunosaariWeb.VisitorController do
use RunosaariWeb, :controller
alias Runosaari.Registration
alias Runosaari.Registration.Visitor
def index(conn, _params) do
visitors = Registration.list_visitors()
render(conn, "index.html", visitors: visitors)
end
def new(conn, _params) do
changeset = Registration.change_visitor(%Visitor{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"visitor" => visitor_params}) do
case Registration.create_visitor(visitor_params) do
{:ok, visitor} ->
conn
|> put_flash(:info, "Visitor created successfully.")
|> redirect(to: Routes.visitor_path(conn, :show, visitor))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
visitor = Registration.get_visitor!(id)
render(conn, "show.html", visitor: visitor)
end
def edit(conn, %{"id" => id}) do
visitor = Registration.get_visitor!(id)
changeset = Registration.change_visitor(visitor)
render(conn, "edit.html", visitor: visitor, changeset: changeset)
end
def update(conn, %{"id" => id, "visitor" => visitor_params}) do
visitor = Registration.get_visitor!(id)
case Registration.update_visitor(visitor, visitor_params) do
{:ok, visitor} ->
conn
|> put_flash(:info, "Visitor updated successfully.")
|> redirect(to: Routes.visitor_path(conn, :show, visitor))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", visitor: visitor, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
visitor = Registration.get_visitor!(id)
{:ok, _visitor} = Registration.delete_visitor(visitor)
conn
|> put_flash(:info, "Visitor deleted successfully.")
|> redirect(to: Routes.visitor_path(conn, :index))
end
end

View File

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

View File

@@ -0,0 +1,47 @@
<%= 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, :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, :date1 %>
<%= checkbox f, :date1 %>
<%= error_tag f, :date1 %>
<%= label f, :date2 %>
<%= checkbox f, :date2 %>
<%= error_tag f, :date2 %>
<%= label f, :date3 %>
<%= checkbox f, :date3 %>
<%= error_tag f, :date3 %>
<%= label f, :bus %>
<%= checkbox f, :bus %>
<%= error_tag f, :bus %>
<%= label f, :accom %>
<%= checkbox f, :accom %>
<%= error_tag f, :accom %>
<div>
<%= submit "Save" %>
</div>
<% end %>

View File

@@ -0,0 +1,42 @@
<h1>Listing Visitors</h1>
<table>
<thead>
<tr>
<th>Fname</th>
<th>Lname</th>
<th>Email</th>
<th>Tel</th>
<th>Date1</th>
<th>Date2</th>
<th>Date3</th>
<th>Bus</th>
<th>Accom</th>
<th></th>
</tr>
</thead>
<tbody>
<%= for visitor <- @visitors do %>
<tr>
<td><%= visitor.fname %></td>
<td><%= visitor.lname %></td>
<td><%= visitor.email %></td>
<td><%= visitor.tel %></td>
<td><%= visitor.date1 %></td>
<td><%= visitor.date2 %></td>
<td><%= visitor.date3 %></td>
<td><%= visitor.bus %></td>
<td><%= visitor.accom %></td>
<td>
<span><%= link "Show", to: Routes.visitor_path(@conn, :show, visitor) %></span>
<span><%= link "Edit", to: Routes.visitor_path(@conn, :edit, visitor) %></span>
<span><%= link "Delete", to: Routes.visitor_path(@conn, :delete, visitor), method: :delete, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= link "New Visitor", to: Routes.visitor_path(@conn, :new) %></span>

View File

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

View File

@@ -0,0 +1,53 @@
<h1>Show Visitor</h1>
<ul>
<li>
<strong>Fname:</strong>
<%= @visitor.fname %>
</li>
<li>
<strong>Lname:</strong>
<%= @visitor.lname %>
</li>
<li>
<strong>Email:</strong>
<%= @visitor.email %>
</li>
<li>
<strong>Tel:</strong>
<%= @visitor.tel %>
</li>
<li>
<strong>Date1:</strong>
<%= @visitor.date1 %>
</li>
<li>
<strong>Date2:</strong>
<%= @visitor.date2 %>
</li>
<li>
<strong>Date3:</strong>
<%= @visitor.date3 %>
</li>
<li>
<strong>Bus:</strong>
<%= @visitor.bus %>
</li>
<li>
<strong>Accom:</strong>
<%= @visitor.accom %>
</li>
</ul>
<span><%= link "Edit", to: Routes.visitor_path(@conn, :edit, @visitor) %></span>
<span><%= link "Back", to: Routes.visitor_path(@conn, :index) %></span>

View File

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