From adacd9159155c764de1069ed16168a2e385a8c71 Mon Sep 17 00:00:00 2001 From: codevictory Date: Tue, 30 Mar 2021 22:40:29 +0300 Subject: [PATCH] AUTOGEN: Locations. --- lib/runosaari/area.ex | 104 ++++++++++++++++++ lib/runosaari/area/location.ex | 21 ++++ .../controllers/location_controller.ex | 62 +++++++++++ .../templates/location/edit.html.eex | 5 + .../templates/location/form.html.eex | 31 ++++++ .../templates/location/index.html.eex | 34 ++++++ .../templates/location/new.html.eex | 5 + .../templates/location/show.html.eex | 33 ++++++ lib/runosaari_web/views/location_view.ex | 3 + .../20210330193344_create_locations.exs | 16 +++ test/runosaari/area_test.exs | 72 ++++++++++++ .../controllers/location_controller_test.exs | 88 +++++++++++++++ 12 files changed, 474 insertions(+) create mode 100644 lib/runosaari/area.ex create mode 100644 lib/runosaari/area/location.ex create mode 100644 lib/runosaari_web/controllers/location_controller.ex create mode 100644 lib/runosaari_web/templates/location/edit.html.eex create mode 100644 lib/runosaari_web/templates/location/form.html.eex create mode 100644 lib/runosaari_web/templates/location/index.html.eex create mode 100644 lib/runosaari_web/templates/location/new.html.eex create mode 100644 lib/runosaari_web/templates/location/show.html.eex create mode 100644 lib/runosaari_web/views/location_view.ex create mode 100644 priv/repo/migrations/20210330193344_create_locations.exs create mode 100644 test/runosaari/area_test.exs create mode 100644 test/runosaari_web/controllers/location_controller_test.exs diff --git a/lib/runosaari/area.ex b/lib/runosaari/area.ex new file mode 100644 index 0000000..bb2b4ea --- /dev/null +++ b/lib/runosaari/area.ex @@ -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 diff --git a/lib/runosaari/area/location.ex b/lib/runosaari/area/location.ex new file mode 100644 index 0000000..e42ed5f --- /dev/null +++ b/lib/runosaari/area/location.ex @@ -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 diff --git a/lib/runosaari_web/controllers/location_controller.ex b/lib/runosaari_web/controllers/location_controller.ex new file mode 100644 index 0000000..bbdcba9 --- /dev/null +++ b/lib/runosaari_web/controllers/location_controller.ex @@ -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 diff --git a/lib/runosaari_web/templates/location/edit.html.eex b/lib/runosaari_web/templates/location/edit.html.eex new file mode 100644 index 0000000..ff3f501 --- /dev/null +++ b/lib/runosaari_web/templates/location/edit.html.eex @@ -0,0 +1,5 @@ +

Edit Location

+ +<%= render "form.html", Map.put(assigns, :action, Routes.location_path(@conn, :update, @location)) %> + +<%= link "Back", to: Routes.location_path(@conn, :index) %> diff --git a/lib/runosaari_web/templates/location/form.html.eex b/lib/runosaari_web/templates/location/form.html.eex new file mode 100644 index 0000000..7b29336 --- /dev/null +++ b/lib/runosaari_web/templates/location/form.html.eex @@ -0,0 +1,31 @@ +<%= form_for @changeset, @action, fn f -> %> + <%= if @changeset.action do %> +
+

Oops, something went wrong! Please check the errors below.

+
+ <% 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 %> + +
+ <%= submit "Save" %> +
+<% end %> diff --git a/lib/runosaari_web/templates/location/index.html.eex b/lib/runosaari_web/templates/location/index.html.eex new file mode 100644 index 0000000..2bc7014 --- /dev/null +++ b/lib/runosaari_web/templates/location/index.html.eex @@ -0,0 +1,34 @@ +

Listing Locations

+ + + + + + + + + + + + + + +<%= for location <- @locations do %> + + + + + + + + + +<% end %> + +
NameAddressReserved seatsMax seatsDescription
<%= location.name %><%= location.address %><%= location.reserved_seats %><%= location.max_seats %><%= location.description %> + <%= link "Show", to: Routes.location_path(@conn, :show, location) %> + <%= link "Edit", to: Routes.location_path(@conn, :edit, location) %> + <%= link "Delete", to: Routes.location_path(@conn, :delete, location), method: :delete, data: [confirm: "Are you sure?"] %> +
+ +<%= link "New Location", to: Routes.location_path(@conn, :new) %> diff --git a/lib/runosaari_web/templates/location/new.html.eex b/lib/runosaari_web/templates/location/new.html.eex new file mode 100644 index 0000000..2d846d9 --- /dev/null +++ b/lib/runosaari_web/templates/location/new.html.eex @@ -0,0 +1,5 @@ +

New Location

+ +<%= render "form.html", Map.put(assigns, :action, Routes.location_path(@conn, :create)) %> + +<%= link "Back", to: Routes.location_path(@conn, :index) %> diff --git a/lib/runosaari_web/templates/location/show.html.eex b/lib/runosaari_web/templates/location/show.html.eex new file mode 100644 index 0000000..e26e258 --- /dev/null +++ b/lib/runosaari_web/templates/location/show.html.eex @@ -0,0 +1,33 @@ +

Show Location

+ + + +<%= link "Edit", to: Routes.location_path(@conn, :edit, @location) %> +<%= link "Back", to: Routes.location_path(@conn, :index) %> diff --git a/lib/runosaari_web/views/location_view.ex b/lib/runosaari_web/views/location_view.ex new file mode 100644 index 0000000..a446578 --- /dev/null +++ b/lib/runosaari_web/views/location_view.ex @@ -0,0 +1,3 @@ +defmodule RunosaariWeb.LocationView do + use RunosaariWeb, :view +end diff --git a/priv/repo/migrations/20210330193344_create_locations.exs b/priv/repo/migrations/20210330193344_create_locations.exs new file mode 100644 index 0000000..de86211 --- /dev/null +++ b/priv/repo/migrations/20210330193344_create_locations.exs @@ -0,0 +1,16 @@ +defmodule Runosaari.Repo.Migrations.CreateLocations do + use Ecto.Migration + + def change do + create table(:locations) do + add :name, :string + add :address, :string + add :reserved_seats, :integer + add :max_seats, :integer + add :description, :string + + timestamps() + end + + end +end diff --git a/test/runosaari/area_test.exs b/test/runosaari/area_test.exs new file mode 100644 index 0000000..86e7df3 --- /dev/null +++ b/test/runosaari/area_test.exs @@ -0,0 +1,72 @@ +defmodule Runosaari.AreaTest do + use Runosaari.DataCase + + alias Runosaari.Area + + describe "locations" do + alias Runosaari.Area.Location + + @valid_attrs %{address: "some address", description: "some description", max_seats: 42, name: "some name", reserved_seats: 42} + @update_attrs %{address: "some updated address", description: "some updated description", max_seats: 43, name: "some updated name", reserved_seats: 43} + @invalid_attrs %{address: nil, description: nil, max_seats: nil, name: nil, reserved_seats: nil} + + def location_fixture(attrs \\ %{}) do + {:ok, location} = + attrs + |> Enum.into(@valid_attrs) + |> Area.create_location() + + location + end + + test "list_locations/0 returns all locations" do + location = location_fixture() + assert Area.list_locations() == [location] + end + + test "get_location!/1 returns the location with given id" do + location = location_fixture() + assert Area.get_location!(location.id) == location + end + + test "create_location/1 with valid data creates a location" do + assert {:ok, %Location{} = location} = Area.create_location(@valid_attrs) + assert location.address == "some address" + assert location.description == "some description" + assert location.max_seats == 42 + assert location.name == "some name" + assert location.reserved_seats == 42 + end + + test "create_location/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Area.create_location(@invalid_attrs) + end + + test "update_location/2 with valid data updates the location" do + location = location_fixture() + assert {:ok, %Location{} = location} = Area.update_location(location, @update_attrs) + assert location.address == "some updated address" + assert location.description == "some updated description" + assert location.max_seats == 43 + assert location.name == "some updated name" + assert location.reserved_seats == 43 + end + + test "update_location/2 with invalid data returns error changeset" do + location = location_fixture() + assert {:error, %Ecto.Changeset{}} = Area.update_location(location, @invalid_attrs) + assert location == Area.get_location!(location.id) + end + + test "delete_location/1 deletes the location" do + location = location_fixture() + assert {:ok, %Location{}} = Area.delete_location(location) + assert_raise Ecto.NoResultsError, fn -> Area.get_location!(location.id) end + end + + test "change_location/1 returns a location changeset" do + location = location_fixture() + assert %Ecto.Changeset{} = Area.change_location(location) + end + end +end diff --git a/test/runosaari_web/controllers/location_controller_test.exs b/test/runosaari_web/controllers/location_controller_test.exs new file mode 100644 index 0000000..e9c3d18 --- /dev/null +++ b/test/runosaari_web/controllers/location_controller_test.exs @@ -0,0 +1,88 @@ +defmodule RunosaariWeb.LocationControllerTest do + use RunosaariWeb.ConnCase + + alias Runosaari.Area + + @create_attrs %{address: "some address", description: "some description", max_seats: 42, name: "some name", reserved_seats: 42} + @update_attrs %{address: "some updated address", description: "some updated description", max_seats: 43, name: "some updated name", reserved_seats: 43} + @invalid_attrs %{address: nil, description: nil, max_seats: nil, name: nil, reserved_seats: nil} + + def fixture(:location) do + {:ok, location} = Area.create_location(@create_attrs) + location + end + + describe "index" do + test "lists all locations", %{conn: conn} do + conn = get(conn, Routes.location_path(conn, :index)) + assert html_response(conn, 200) =~ "Listing Locations" + end + end + + describe "new location" do + test "renders form", %{conn: conn} do + conn = get(conn, Routes.location_path(conn, :new)) + assert html_response(conn, 200) =~ "New Location" + end + end + + describe "create location" do + test "redirects to show when data is valid", %{conn: conn} do + conn = post(conn, Routes.location_path(conn, :create), location: @create_attrs) + + assert %{id: id} = redirected_params(conn) + assert redirected_to(conn) == Routes.location_path(conn, :show, id) + + conn = get(conn, Routes.location_path(conn, :show, id)) + assert html_response(conn, 200) =~ "Show Location" + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = post(conn, Routes.location_path(conn, :create), location: @invalid_attrs) + assert html_response(conn, 200) =~ "New Location" + end + end + + describe "edit location" do + setup [:create_location] + + test "renders form for editing chosen location", %{conn: conn, location: location} do + conn = get(conn, Routes.location_path(conn, :edit, location)) + assert html_response(conn, 200) =~ "Edit Location" + end + end + + describe "update location" do + setup [:create_location] + + test "redirects when data is valid", %{conn: conn, location: location} do + conn = put(conn, Routes.location_path(conn, :update, location), location: @update_attrs) + assert redirected_to(conn) == Routes.location_path(conn, :show, location) + + conn = get(conn, Routes.location_path(conn, :show, location)) + assert html_response(conn, 200) =~ "some updated address" + end + + test "renders errors when data is invalid", %{conn: conn, location: location} do + conn = put(conn, Routes.location_path(conn, :update, location), location: @invalid_attrs) + assert html_response(conn, 200) =~ "Edit Location" + end + end + + describe "delete location" do + setup [:create_location] + + test "deletes chosen location", %{conn: conn, location: location} do + conn = delete(conn, Routes.location_path(conn, :delete, location)) + assert redirected_to(conn) == Routes.location_path(conn, :index) + assert_error_sent 404, fn -> + get(conn, Routes.location_path(conn, :show, location)) + end + end + end + + defp create_location(_) do + location = fixture(:location) + %{location: location} + end +end