From 3347b2625676df4eddb7ee0fb2561b04d27a7b65 Mon Sep 17 00:00:00 2001 From: shibao Date: Mon, 25 Jul 2022 20:11:08 -0400 Subject: [PATCH] add contexts --- lib/memex/contexts.ex | 104 +++++++++++++++++ lib/memex/contexts/context.ex | 22 ++++ .../live/context_live/form_component.ex | 55 +++++++++ .../context_live/form_component.html.heex | 32 +++++ lib/memex_web/live/context_live/index.ex | 46 ++++++++ .../live/context_live/index.html.heex | 45 +++++++ lib/memex_web/live/context_live/show.ex | 21 ++++ .../live/context_live/show.html.heex | 41 +++++++ lib/memex_web/router.ex | 8 ++ .../20220726001039_create_contexts.exs | 15 +++ test/memex/contexts_test.exs | 65 +++++++++++ test/memex_web/live/context_live_test.exs | 110 ++++++++++++++++++ test/support/fixtures/contexts_fixtures.ex | 23 ++++ 13 files changed, 587 insertions(+) create mode 100644 lib/memex/contexts.ex create mode 100644 lib/memex/contexts/context.ex create mode 100644 lib/memex_web/live/context_live/form_component.ex create mode 100644 lib/memex_web/live/context_live/form_component.html.heex create mode 100644 lib/memex_web/live/context_live/index.ex create mode 100644 lib/memex_web/live/context_live/index.html.heex create mode 100644 lib/memex_web/live/context_live/show.ex create mode 100644 lib/memex_web/live/context_live/show.html.heex create mode 100644 priv/repo/migrations/20220726001039_create_contexts.exs create mode 100644 test/memex/contexts_test.exs create mode 100644 test/memex_web/live/context_live_test.exs create mode 100644 test/support/fixtures/contexts_fixtures.ex diff --git a/lib/memex/contexts.ex b/lib/memex/contexts.ex new file mode 100644 index 0000000..d8d3f5b --- /dev/null +++ b/lib/memex/contexts.ex @@ -0,0 +1,104 @@ +defmodule Memex.Contexts do + @moduledoc """ + The Contexts context. + """ + + import Ecto.Query, warn: false + alias Memex.Repo + + alias Memex.Contexts.Context + + @doc """ + Returns the list of contexts. + + ## Examples + + iex> list_contexts() + [%Context{}, ...] + + """ + def list_contexts do + Repo.all(Context) + end + + @doc """ + Gets a single context. + + Raises `Ecto.NoResultsError` if the Context does not exist. + + ## Examples + + iex> get_context!(123) + %Context{} + + iex> get_context!(456) + ** (Ecto.NoResultsError) + + """ + def get_context!(id), do: Repo.get!(Context, id) + + @doc """ + Creates a context. + + ## Examples + + iex> create_context(%{field: value}) + {:ok, %Context{}} + + iex> create_context(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_context(attrs \\ %{}) do + %Context{} + |> Context.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a context. + + ## Examples + + iex> update_context(context, %{field: new_value}) + {:ok, %Context{}} + + iex> update_context(context, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_context(%Context{} = context, attrs) do + context + |> Context.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a context. + + ## Examples + + iex> delete_context(context) + {:ok, %Context{}} + + iex> delete_context(context) + {:error, %Ecto.Changeset{}} + + """ + def delete_context(%Context{} = context) do + Repo.delete(context) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking context changes. + + ## Examples + + iex> change_context(context) + %Ecto.Changeset{data: %Context{}} + + """ + def change_context(%Context{} = context, attrs \\ %{}) do + Context.changeset(context, attrs) + end +end diff --git a/lib/memex/contexts/context.ex b/lib/memex/contexts/context.ex new file mode 100644 index 0000000..c31cc24 --- /dev/null +++ b/lib/memex/contexts/context.ex @@ -0,0 +1,22 @@ +defmodule Memex.Contexts.Context do + use Ecto.Schema + import Ecto.Changeset + + @primary_key {:id, :binary_id, autogenerate: true} + @foreign_key_type :binary_id + schema "contexts" do + field :content, :string + field :tag, {:array, :string} + field :title, :string + field :visibility, Ecto.Enum, values: [:public, :private, :unlisted] + + timestamps() + end + + @doc false + def changeset(context, attrs) do + context + |> cast(attrs, [:title, :content, :tag, :visibility]) + |> validate_required([:title, :content, :tag, :visibility]) + end +end diff --git a/lib/memex_web/live/context_live/form_component.ex b/lib/memex_web/live/context_live/form_component.ex new file mode 100644 index 0000000..b0aa971 --- /dev/null +++ b/lib/memex_web/live/context_live/form_component.ex @@ -0,0 +1,55 @@ +defmodule MemexWeb.ContextLive.FormComponent do + use MemexWeb, :live_component + + alias Memex.Contexts + + @impl true + def update(%{context: context} = assigns, socket) do + changeset = Contexts.change_context(context) + + {:ok, + socket + |> assign(assigns) + |> assign(:changeset, changeset)} + end + + @impl true + def handle_event("validate", %{"context" => context_params}, socket) do + changeset = + socket.assigns.context + |> Contexts.change_context(context_params) + |> Map.put(:action, :validate) + + {:noreply, assign(socket, :changeset, changeset)} + end + + def handle_event("save", %{"context" => context_params}, socket) do + save_context(socket, socket.assigns.action, context_params) + end + + defp save_context(socket, :edit, context_params) do + case Contexts.update_context(socket.assigns.context, context_params) do + {:ok, _context} -> + {:noreply, + socket + |> put_flash(:info, "Context updated successfully") + |> push_redirect(to: socket.assigns.return_to)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, :changeset, changeset)} + end + end + + defp save_context(socket, :new, context_params) do + case Contexts.create_context(context_params) do + {:ok, _context} -> + {:noreply, + socket + |> put_flash(:info, "Context created successfully") + |> push_redirect(to: socket.assigns.return_to)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, changeset: changeset)} + end + end +end diff --git a/lib/memex_web/live/context_live/form_component.html.heex b/lib/memex_web/live/context_live/form_component.html.heex new file mode 100644 index 0000000..2edc917 --- /dev/null +++ b/lib/memex_web/live/context_live/form_component.html.heex @@ -0,0 +1,32 @@ +
+

<%= @title %>

+ + <.form + let={f} + for={@changeset} + id="context-form" + phx-target={@myself} + phx-change="validate" + phx-submit="save"> + + <%= label f, :title %> + <%= text_input f, :title %> + <%= error_tag f, :title %> + + <%= label f, :content %> + <%= textarea f, :content %> + <%= error_tag f, :content %> + + <%= label f, :tag %> + <%= multiple_select f, :tag, ["Option 1": "option1", "Option 2": "option2"] %> + <%= error_tag f, :tag %> + + <%= label f, :visibility %> + <%= select f, :visibility, Ecto.Enum.values(Memex.Contexts.Context, :visibility), prompt: "Choose a value" %> + <%= error_tag f, :visibility %> + +
+ <%= submit "Save", phx_disable_with: "Saving..." %> +
+ +
diff --git a/lib/memex_web/live/context_live/index.ex b/lib/memex_web/live/context_live/index.ex new file mode 100644 index 0000000..0c5f843 --- /dev/null +++ b/lib/memex_web/live/context_live/index.ex @@ -0,0 +1,46 @@ +defmodule MemexWeb.ContextLive.Index do + use MemexWeb, :live_view + + alias Memex.Contexts + alias Memex.Contexts.Context + + @impl true + def mount(_params, _session, socket) do + {:ok, assign(socket, :contexts, list_contexts())} + end + + @impl true + def handle_params(params, _url, socket) do + {:noreply, apply_action(socket, socket.assigns.live_action, params)} + end + + defp apply_action(socket, :edit, %{"id" => id}) do + socket + |> assign(:page_title, "Edit Context") + |> assign(:context, Contexts.get_context!(id)) + end + + defp apply_action(socket, :new, _params) do + socket + |> assign(:page_title, "New Context") + |> assign(:context, %Context{}) + end + + defp apply_action(socket, :index, _params) do + socket + |> assign(:page_title, "Listing Contexts") + |> assign(:context, nil) + end + + @impl true + def handle_event("delete", %{"id" => id}, socket) do + context = Contexts.get_context!(id) + {:ok, _} = Contexts.delete_context(context) + + {:noreply, assign(socket, :contexts, list_contexts())} + end + + defp list_contexts do + Contexts.list_contexts() + end +end diff --git a/lib/memex_web/live/context_live/index.html.heex b/lib/memex_web/live/context_live/index.html.heex new file mode 100644 index 0000000..be335c6 --- /dev/null +++ b/lib/memex_web/live/context_live/index.html.heex @@ -0,0 +1,45 @@ +

Listing Contexts

+ +<%= if @live_action in [:new, :edit] do %> + <.modal return_to={Routes.context_index_path(@socket, :index)}> + <.live_component + module={MemexWeb.ContextLive.FormComponent} + id={@context.id || :new} + title={@page_title} + action={@live_action} + context={@context} + return_to={Routes.context_index_path(@socket, :index)} + /> + +<% end %> + + + + + + + + + + + + + + <%= for context <- @contexts do %> + + + + + + + + + <% end %> + +
TitleContentTagVisibility
<%= context.title %><%= context.content %><%= context.tag %><%= context.visibility %> + <%= live_redirect "Show", to: Routes.context_show_path(@socket, :show, context) %> + <%= live_patch "Edit", to: Routes.context_index_path(@socket, :edit, context) %> + <%= link "Delete", to: "#", phx_click: "delete", phx_value_id: context.id, data: [confirm: "Are you sure?"] %> +
+ +<%= live_patch "New Context", to: Routes.context_index_path(@socket, :new) %> diff --git a/lib/memex_web/live/context_live/show.ex b/lib/memex_web/live/context_live/show.ex new file mode 100644 index 0000000..079f00e --- /dev/null +++ b/lib/memex_web/live/context_live/show.ex @@ -0,0 +1,21 @@ +defmodule MemexWeb.ContextLive.Show do + use MemexWeb, :live_view + + alias Memex.Contexts + + @impl true + def mount(_params, _session, socket) do + {:ok, socket} + end + + @impl true + def handle_params(%{"id" => id}, _, socket) do + {:noreply, + socket + |> assign(:page_title, page_title(socket.assigns.live_action)) + |> assign(:context, Contexts.get_context!(id))} + end + + defp page_title(:show), do: "Show Context" + defp page_title(:edit), do: "Edit Context" +end diff --git a/lib/memex_web/live/context_live/show.html.heex b/lib/memex_web/live/context_live/show.html.heex new file mode 100644 index 0000000..c634d45 --- /dev/null +++ b/lib/memex_web/live/context_live/show.html.heex @@ -0,0 +1,41 @@ +

Show Context

+ +<%= if @live_action in [:edit] do %> + <.modal return_to={Routes.context_show_path(@socket, :show, @context)}> + <.live_component + module={MemexWeb.ContextLive.FormComponent} + id={@context.id} + title={@page_title} + action={@live_action} + context={@context} + return_to={Routes.context_show_path(@socket, :show, @context)} + /> + +<% end %> + + + +<%= live_patch "Edit", to: Routes.context_show_path(@socket, :edit, @context), class: "button" %> | +<%= live_redirect "Back", to: Routes.context_index_path(@socket, :index) %> diff --git a/lib/memex_web/router.ex b/lib/memex_web/router.ex index ebbe05a..b34c5d8 100644 --- a/lib/memex_web/router.ex +++ b/lib/memex_web/router.ex @@ -59,6 +59,10 @@ defmodule MemexWeb.Router do live "/notes", NoteLive.Index, :index live "/notes/:id", NoteLive.Show, :show + + live "/contexts", ContextLive.Index, :index + live "/contexts/:id", ContextLive.Show, :show + end scope "/", MemexWeb do @@ -68,6 +72,10 @@ defmodule MemexWeb.Router do live "/notes/:id/edit", NoteLive.Index, :edit live "/notes/:id/show/edit", NoteLive.Show, :edit + live "/contexts/new", ContextLive.Index, :new + live "/contexts/:id/edit", ContextLive.Index, :edit + live "/contexts/:id/show/edit", ContextLive.Show, :edit + get "/users/settings", UserSettingsController, :edit put "/users/settings", UserSettingsController, :update delete "/users/settings/:id", UserSettingsController, :delete diff --git a/priv/repo/migrations/20220726001039_create_contexts.exs b/priv/repo/migrations/20220726001039_create_contexts.exs new file mode 100644 index 0000000..177b2bc --- /dev/null +++ b/priv/repo/migrations/20220726001039_create_contexts.exs @@ -0,0 +1,15 @@ +defmodule Memex.Repo.Migrations.CreateContexts do + use Ecto.Migration + + def change do + create table(:contexts, primary_key: false) do + add :id, :binary_id, primary_key: true + add :title, :string + add :content, :text + add :tag, {:array, :string} + add :visibility, :string + + timestamps() + end + end +end diff --git a/test/memex/contexts_test.exs b/test/memex/contexts_test.exs new file mode 100644 index 0000000..8431ec1 --- /dev/null +++ b/test/memex/contexts_test.exs @@ -0,0 +1,65 @@ +defmodule Memex.ContextsTest do + use Memex.DataCase + + alias Memex.Contexts + + describe "contexts" do + alias Memex.Contexts.Context + + import Memex.ContextsFixtures + + @invalid_attrs %{content: nil, tag: nil, title: nil, visibility: nil} + + test "list_contexts/0 returns all contexts" do + context = context_fixture() + assert Contexts.list_contexts() == [context] + end + + test "get_context!/1 returns the context with given id" do + context = context_fixture() + assert Contexts.get_context!(context.id) == context + end + + test "create_context/1 with valid data creates a context" do + valid_attrs = %{content: "some content", tag: [], title: "some title", visibility: :public} + + assert {:ok, %Context{} = context} = Contexts.create_context(valid_attrs) + assert context.content == "some content" + assert context.tag == [] + assert context.title == "some title" + assert context.visibility == :public + end + + test "create_context/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Contexts.create_context(@invalid_attrs) + end + + test "update_context/2 with valid data updates the context" do + context = context_fixture() + update_attrs = %{content: "some updated content", tag: [], title: "some updated title", visibility: :private} + + assert {:ok, %Context{} = context} = Contexts.update_context(context, update_attrs) + assert context.content == "some updated content" + assert context.tag == [] + assert context.title == "some updated title" + assert context.visibility == :private + end + + test "update_context/2 with invalid data returns error changeset" do + context = context_fixture() + assert {:error, %Ecto.Changeset{}} = Contexts.update_context(context, @invalid_attrs) + assert context == Contexts.get_context!(context.id) + end + + test "delete_context/1 deletes the context" do + context = context_fixture() + assert {:ok, %Context{}} = Contexts.delete_context(context) + assert_raise Ecto.NoResultsError, fn -> Contexts.get_context!(context.id) end + end + + test "change_context/1 returns a context changeset" do + context = context_fixture() + assert %Ecto.Changeset{} = Contexts.change_context(context) + end + end +end diff --git a/test/memex_web/live/context_live_test.exs b/test/memex_web/live/context_live_test.exs new file mode 100644 index 0000000..d3ca969 --- /dev/null +++ b/test/memex_web/live/context_live_test.exs @@ -0,0 +1,110 @@ +defmodule MemexWeb.ContextLiveTest do + use MemexWeb.ConnCase + + import Phoenix.LiveViewTest + import Memex.ContextsFixtures + + @create_attrs %{content: "some content", tag: [], title: "some title", visibility: :public} + @update_attrs %{content: "some updated content", tag: [], title: "some updated title", visibility: :private} + @invalid_attrs %{content: nil, tag: [], title: nil, visibility: nil} + + defp create_context(_) do + context = context_fixture() + %{context: context} + end + + describe "Index" do + setup [:create_context] + + test "lists all contexts", %{conn: conn, context: context} do + {:ok, _index_live, html} = live(conn, Routes.context_index_path(conn, :index)) + + assert html =~ "Listing Contexts" + assert html =~ context.content + end + + test "saves new context", %{conn: conn} do + {:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index)) + + assert index_live |> element("a", "New Context") |> render_click() =~ + "New Context" + + assert_patch(index_live, Routes.context_index_path(conn, :new)) + + assert index_live + |> form("#context-form", context: @invalid_attrs) + |> render_change() =~ "can't be blank" + + {:ok, _, html} = + index_live + |> form("#context-form", context: @create_attrs) + |> render_submit() + |> follow_redirect(conn, Routes.context_index_path(conn, :index)) + + assert html =~ "Context created successfully" + assert html =~ "some content" + end + + test "updates context in listing", %{conn: conn, context: context} do + {:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index)) + + assert index_live |> element("#context-#{context.id} a", "Edit") |> render_click() =~ + "Edit Context" + + assert_patch(index_live, Routes.context_index_path(conn, :edit, context)) + + assert index_live + |> form("#context-form", context: @invalid_attrs) + |> render_change() =~ "can't be blank" + + {:ok, _, html} = + index_live + |> form("#context-form", context: @update_attrs) + |> render_submit() + |> follow_redirect(conn, Routes.context_index_path(conn, :index)) + + assert html =~ "Context updated successfully" + assert html =~ "some updated content" + end + + test "deletes context in listing", %{conn: conn, context: context} do + {:ok, index_live, _html} = live(conn, Routes.context_index_path(conn, :index)) + + assert index_live |> element("#context-#{context.id} a", "Delete") |> render_click() + refute has_element?(index_live, "#context-#{context.id}") + end + end + + describe "Show" do + setup [:create_context] + + test "displays context", %{conn: conn, context: context} do + {:ok, _show_live, html} = live(conn, Routes.context_show_path(conn, :show, context)) + + assert html =~ "Show Context" + assert html =~ context.content + end + + test "updates context within modal", %{conn: conn, context: context} do + {:ok, show_live, _html} = live(conn, Routes.context_show_path(conn, :show, context)) + + assert show_live |> element("a", "Edit") |> render_click() =~ + "Edit Context" + + assert_patch(show_live, Routes.context_show_path(conn, :edit, context)) + + assert show_live + |> form("#context-form", context: @invalid_attrs) + |> render_change() =~ "can't be blank" + + {:ok, _, html} = + show_live + |> form("#context-form", context: @update_attrs) + |> render_submit() + |> follow_redirect(conn, Routes.context_show_path(conn, :show, context)) + + assert html =~ "Context updated successfully" + assert html =~ "some updated content" + end + end +end diff --git a/test/support/fixtures/contexts_fixtures.ex b/test/support/fixtures/contexts_fixtures.ex new file mode 100644 index 0000000..0965668 --- /dev/null +++ b/test/support/fixtures/contexts_fixtures.ex @@ -0,0 +1,23 @@ +defmodule Memex.ContextsFixtures do + @moduledoc """ + This module defines test helpers for creating + entities via the `Memex.Contexts` context. + """ + + @doc """ + Generate a context. + """ + def context_fixture(attrs \\ %{}) do + {:ok, context} = + attrs + |> Enum.into(%{ + content: "some content", + tag: [], + title: "some title", + visibility: :public + }) + |> Memex.Contexts.create_context() + + context + end +end