AUTOGEN: Locations.

This commit is contained in:
codevictory
2021-03-30 22:40:29 +03:00
parent 200d695586
commit adacd91591
12 changed files with 474 additions and 0 deletions

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

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

View File

@@ -0,0 +1,21 @@
defmodule Runosaari.Area.Location do
use Ecto.Schema
import Ecto.Changeset
schema "locations" do
field :address, :string
field :description, :string
field :max_seats, :integer
field :name, :string
field :reserved_seats, :integer
timestamps()
end
@doc false
def changeset(location, attrs) do
location
|> cast(attrs, [:name, :address, :reserved_seats, :max_seats, :description])
|> validate_required([:name, :address, :reserved_seats, :max_seats, :description])
end
end

View File

@@ -0,0 +1,62 @@
defmodule RunosaariWeb.LocationController do
use RunosaariWeb, :controller
alias Runosaari.Area
alias Runosaari.Area.Location
def index(conn, _params) do
locations = Area.list_locations()
render(conn, "index.html", locations: locations)
end
def new(conn, _params) do
changeset = Area.change_location(%Location{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"location" => location_params}) do
case Area.create_location(location_params) do
{:ok, location} ->
conn
|> put_flash(:info, "Location created successfully.")
|> redirect(to: Routes.location_path(conn, :show, location))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
location = Area.get_location!(id)
render(conn, "show.html", location: location)
end
def edit(conn, %{"id" => id}) do
location = Area.get_location!(id)
changeset = Area.change_location(location)
render(conn, "edit.html", location: location, changeset: changeset)
end
def update(conn, %{"id" => id, "location" => location_params}) do
location = Area.get_location!(id)
case Area.update_location(location, location_params) do
{:ok, location} ->
conn
|> put_flash(:info, "Location updated successfully.")
|> redirect(to: Routes.location_path(conn, :show, location))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", location: location, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
location = Area.get_location!(id)
{:ok, _location} = Area.delete_location(location)
conn
|> put_flash(:info, "Location deleted successfully.")
|> redirect(to: Routes.location_path(conn, :index))
end
end

View File

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

View File

@@ -0,0 +1,31 @@
<%= 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, :name %>
<%= text_input f, :name %>
<%= error_tag f, :name %>
<%= label f, :address %>
<%= text_input f, :address %>
<%= error_tag f, :address %>
<%= label f, :reserved_seats %>
<%= number_input f, :reserved_seats %>
<%= error_tag f, :reserved_seats %>
<%= label f, :max_seats %>
<%= number_input f, :max_seats %>
<%= error_tag f, :max_seats %>
<%= label f, :description %>
<%= text_input f, :description %>
<%= error_tag f, :description %>
<div>
<%= submit "Save" %>
</div>
<% end %>

View File

@@ -0,0 +1,34 @@
<h1>Listing Locations</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Reserved seats</th>
<th>Max seats</th>
<th>Description</th>
<th></th>
</tr>
</thead>
<tbody>
<%= for location <- @locations do %>
<tr>
<td><%= location.name %></td>
<td><%= location.address %></td>
<td><%= location.reserved_seats %></td>
<td><%= location.max_seats %></td>
<td><%= location.description %></td>
<td>
<span><%= link "Show", to: Routes.location_path(@conn, :show, location) %></span>
<span><%= link "Edit", to: Routes.location_path(@conn, :edit, location) %></span>
<span><%= link "Delete", to: Routes.location_path(@conn, :delete, location), method: :delete, data: [confirm: "Are you sure?"] %></span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span><%= link "New Location", to: Routes.location_path(@conn, :new) %></span>

View File

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

View File

@@ -0,0 +1,33 @@
<h1>Show Location</h1>
<ul>
<li>
<strong>Name:</strong>
<%= @location.name %>
</li>
<li>
<strong>Address:</strong>
<%= @location.address %>
</li>
<li>
<strong>Reserved seats:</strong>
<%= @location.reserved_seats %>
</li>
<li>
<strong>Max seats:</strong>
<%= @location.max_seats %>
</li>
<li>
<strong>Description:</strong>
<%= @location.description %>
</li>
</ul>
<span><%= link "Edit", to: Routes.location_path(@conn, :edit, @location) %></span>
<span><%= link "Back", to: Routes.location_path(@conn, :index) %></span>

View File

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