diff --git a/lib/outlook/translators.ex b/lib/outlook/translators.ex new file mode 100644 index 0000000..15abfeb --- /dev/null +++ b/lib/outlook/translators.ex @@ -0,0 +1,104 @@ +defmodule Outlook.Translators do + @moduledoc """ + The Translators context. + """ + + import Ecto.Query, warn: false + alias Outlook.Repo + + alias Outlook.Translators.DeeplAccount + + @doc """ + Returns the list of deepl_accounts. + + ## Examples + + iex> list_deepl_accounts() + [%DeeplAccount{}, ...] + + """ + def list_deepl_accounts do + Repo.all(DeeplAccount) + end + + @doc """ + Gets a single deepl_account. + + Raises `Ecto.NoResultsError` if the Deepl account does not exist. + + ## Examples + + iex> get_deepl_account!(123) + %DeeplAccount{} + + iex> get_deepl_account!(456) + ** (Ecto.NoResultsError) + + """ + def get_deepl_account!(id), do: Repo.get!(DeeplAccount, id) + + @doc """ + Creates a deepl_account. + + ## Examples + + iex> create_deepl_account(%{field: value}) + {:ok, %DeeplAccount{}} + + iex> create_deepl_account(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_deepl_account(attrs \\ %{}) do + %DeeplAccount{} + |> DeeplAccount.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a deepl_account. + + ## Examples + + iex> update_deepl_account(deepl_account, %{field: new_value}) + {:ok, %DeeplAccount{}} + + iex> update_deepl_account(deepl_account, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_deepl_account(%DeeplAccount{} = deepl_account, attrs) do + deepl_account + |> DeeplAccount.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a deepl_account. + + ## Examples + + iex> delete_deepl_account(deepl_account) + {:ok, %DeeplAccount{}} + + iex> delete_deepl_account(deepl_account) + {:error, %Ecto.Changeset{}} + + """ + def delete_deepl_account(%DeeplAccount{} = deepl_account) do + Repo.delete(deepl_account) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking deepl_account changes. + + ## Examples + + iex> change_deepl_account(deepl_account) + %Ecto.Changeset{data: %DeeplAccount{}} + + """ + def change_deepl_account(%DeeplAccount{} = deepl_account, attrs \\ %{}) do + DeeplAccount.changeset(deepl_account, attrs) + end +end diff --git a/lib/outlook/translators/deepl_account.ex b/lib/outlook/translators/deepl_account.ex new file mode 100644 index 0000000..7c67cae --- /dev/null +++ b/lib/outlook/translators/deepl_account.ex @@ -0,0 +1,23 @@ +defmodule Outlook.Translators.DeeplAccount do + use Ecto.Schema + import Ecto.Changeset + + schema "deepl_accounts" do + field :auth_key, :string + field :character_count, :integer + field :character_limit, :integer + field :description, :string + field :name, :string + field :our_character_count, :integer + field :user_id, :id + + timestamps() + end + + @doc false + def changeset(deepl_account, attrs) do + deepl_account + |> cast(attrs, [:name, :description, :auth_key, :character_limit, :character_count, :our_character_count]) + |> validate_required([:name, :description, :auth_key, :character_limit, :character_count, :our_character_count]) + end +end diff --git a/lib/outlook_web/live/deepl_account_live/form_component.ex b/lib/outlook_web/live/deepl_account_live/form_component.ex new file mode 100644 index 0000000..8aaf717 --- /dev/null +++ b/lib/outlook_web/live/deepl_account_live/form_component.ex @@ -0,0 +1,86 @@ +defmodule OutlookWeb.DeeplAccountLive.FormComponent do + use OutlookWeb, :live_component + + alias Outlook.Translators + + @impl true + def render(assigns) do + ~H""" +
+ <.header> + <%= @title %> + <:subtitle>Use this form to manage deepl_account records in your database. + + + <.simple_form + :let={f} + for={@changeset} + id="deepl_account-form" + phx-target={@myself} + phx-change="validate" + phx-submit="save" + > + <.input field={{f, :name}} type="text" label="name" /> + <.input field={{f, :description}} type="text" label="description" /> + <.input field={{f, :auth_key}} type="text" label="auth_key" /> + <.input field={{f, :character_limit}} type="number" label="character_limit" /> + <.input field={{f, :character_count}} type="number" label="character_count" /> + <.input field={{f, :our_character_count}} type="number" label="our_character_count" /> + <:actions> + <.button phx-disable-with="Saving...">Save Deepl account + + +
+ """ + end + + @impl true + def update(%{deepl_account: deepl_account} = assigns, socket) do + changeset = Translators.change_deepl_account(deepl_account) + + {:ok, + socket + |> assign(assigns) + |> assign(:changeset, changeset)} + end + + @impl true + def handle_event("validate", %{"deepl_account" => deepl_account_params}, socket) do + changeset = + socket.assigns.deepl_account + |> Translators.change_deepl_account(deepl_account_params) + |> Map.put(:action, :validate) + + {:noreply, assign(socket, :changeset, changeset)} + end + + def handle_event("save", %{"deepl_account" => deepl_account_params}, socket) do + save_deepl_account(socket, socket.assigns.action, deepl_account_params) + end + + defp save_deepl_account(socket, :edit, deepl_account_params) do + case Translators.update_deepl_account(socket.assigns.deepl_account, deepl_account_params) do + {:ok, _deepl_account} -> + {:noreply, + socket + |> put_flash(:info, "Deepl account updated successfully") + |> push_navigate(to: socket.assigns.navigate)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, :changeset, changeset)} + end + end + + defp save_deepl_account(socket, :new, deepl_account_params) do + case Translators.create_deepl_account(deepl_account_params) do + {:ok, _deepl_account} -> + {:noreply, + socket + |> put_flash(:info, "Deepl account created successfully") + |> push_navigate(to: socket.assigns.navigate)} + + {:error, %Ecto.Changeset{} = changeset} -> + {:noreply, assign(socket, changeset: changeset)} + end + end +end diff --git a/lib/outlook_web/live/deepl_account_live/index.ex b/lib/outlook_web/live/deepl_account_live/index.ex new file mode 100644 index 0000000..aefd01c --- /dev/null +++ b/lib/outlook_web/live/deepl_account_live/index.ex @@ -0,0 +1,46 @@ +defmodule OutlookWeb.DeeplAccountLive.Index do + use OutlookWeb, :live_view + + alias Outlook.Translators + alias Outlook.Translators.DeeplAccount + + @impl true + def mount(_params, _session, socket) do + {:ok, assign(socket, :deepl_accounts, list_deepl_accounts())} + 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 Deepl account") + |> assign(:deepl_account, Translators.get_deepl_account!(id)) + end + + defp apply_action(socket, :new, _params) do + socket + |> assign(:page_title, "New Deepl account") + |> assign(:deepl_account, %DeeplAccount{}) + end + + defp apply_action(socket, :index, _params) do + socket + |> assign(:page_title, "Listing Deepl accounts") + |> assign(:deepl_account, nil) + end + + @impl true + def handle_event("delete", %{"id" => id}, socket) do + deepl_account = Translators.get_deepl_account!(id) + {:ok, _} = Translators.delete_deepl_account(deepl_account) + + {:noreply, assign(socket, :deepl_accounts, list_deepl_accounts())} + end + + defp list_deepl_accounts do + Translators.list_deepl_accounts() + end +end diff --git a/lib/outlook_web/live/deepl_account_live/index.html.heex b/lib/outlook_web/live/deepl_account_live/index.html.heex new file mode 100644 index 0000000..db3754f --- /dev/null +++ b/lib/outlook_web/live/deepl_account_live/index.html.heex @@ -0,0 +1,44 @@ +<.header> + Listing Deepl accounts + <:actions> + <.link patch={~p"/deepl_accounts/new"}> + <.button>New Deepl account + + + + +<.table id="deepl_accounts" rows={@deepl_accounts} row_click={&JS.navigate(~p"/deepl_accounts/#{&1}")}> + <:col :let={deepl_account} label="Name"><%= deepl_account.name %> + <:col :let={deepl_account} label="Description"><%= deepl_account.description %> + <:col :let={deepl_account} label="Auth key"><%= deepl_account.auth_key %> + <:col :let={deepl_account} label="Character limit"><%= deepl_account.character_limit %> + <:col :let={deepl_account} label="Character count"><%= deepl_account.character_count %> + <:col :let={deepl_account} label="Our character count"><%= deepl_account.our_character_count %> + <:action :let={deepl_account}> +
+ <.link navigate={~p"/deepl_accounts/#{deepl_account}"}>Show +
+ <.link patch={~p"/deepl_accounts/#{deepl_account}/edit"}>Edit + + <:action :let={deepl_account}> + <.link phx-click={JS.push("delete", value: %{id: deepl_account.id})} data-confirm="Are you sure?"> + Delete + + + + +<.modal + :if={@live_action in [:new, :edit]} + id="deepl_account-modal" + show + on_cancel={JS.navigate(~p"/deepl_accounts")} +> + <.live_component + module={OutlookWeb.DeeplAccountLive.FormComponent} + id={@deepl_account.id || :new} + title={@page_title} + action={@live_action} + deepl_account={@deepl_account} + navigate={~p"/deepl_accounts"} + /> + diff --git a/lib/outlook_web/live/deepl_account_live/show.ex b/lib/outlook_web/live/deepl_account_live/show.ex new file mode 100644 index 0000000..2e5e4c2 --- /dev/null +++ b/lib/outlook_web/live/deepl_account_live/show.ex @@ -0,0 +1,21 @@ +defmodule OutlookWeb.DeeplAccountLive.Show do + use OutlookWeb, :live_view + + alias Outlook.Translators + + @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(:deepl_account, Translators.get_deepl_account!(id))} + end + + defp page_title(:show), do: "Show Deepl account" + defp page_title(:edit), do: "Edit Deepl account" +end diff --git a/lib/outlook_web/live/deepl_account_live/show.html.heex b/lib/outlook_web/live/deepl_account_live/show.html.heex new file mode 100644 index 0000000..35555ab --- /dev/null +++ b/lib/outlook_web/live/deepl_account_live/show.html.heex @@ -0,0 +1,31 @@ +<.header> + Deepl account <%= @deepl_account.id %> + <:subtitle>This is a deepl_account record from your database. + <:actions> + <.link patch={~p"/deepl_accounts/#{@deepl_account}/show/edit"} phx-click={JS.push_focus()}> + <.button>Edit deepl_account + + + + +<.list> + <:item title="Name"><%= @deepl_account.name %> + <:item title="Description"><%= @deepl_account.description %> + <:item title="Auth key"><%= @deepl_account.auth_key %> + <:item title="Character limit"><%= @deepl_account.character_limit %> + <:item title="Character count"><%= @deepl_account.character_count %> + <:item title="Our character count"><%= @deepl_account.our_character_count %> + + +<.back navigate={~p"/deepl_accounts"}>Back to deepl_accounts + +<.modal :if={@live_action == :edit} id="deepl_account-modal" show on_cancel={JS.patch(~p"/deepl_accounts/#{@deepl_account}")}> + <.live_component + module={OutlookWeb.DeeplAccountLive.FormComponent} + id={@deepl_account.id} + title={@page_title} + action={@live_action} + deepl_account={@deepl_account} + navigate={~p"/deepl_accounts/#{@deepl_account}"} + /> + diff --git a/lib/outlook_web/router.ex b/lib/outlook_web/router.ex index c189ead..673010d 100644 --- a/lib/outlook_web/router.ex +++ b/lib/outlook_web/router.ex @@ -90,6 +90,13 @@ defmodule OutlookWeb.Router do live "/translations/:id", TranslationLive.Show, :show live "/translations/:id/show/edit", TranslationLive.Show, :edit + + live "/deepl_accounts", DeeplAccountLive.Index, :index + live "/deepl_accounts/new", DeeplAccountLive.Index, :new + live "/deepl_accounts/:id/edit", DeeplAccountLive.Index, :edit + + live "/deepl_accounts/:id", DeeplAccountLive.Show, :show + live "/deepl_accounts/:id/show/edit", DeeplAccountLive.Show, :edit end scope "/", OutlookWeb do diff --git a/priv/repo/migrations/20221226174741_create_deepl_accounts.exs b/priv/repo/migrations/20221226174741_create_deepl_accounts.exs new file mode 100644 index 0000000..33b4da4 --- /dev/null +++ b/priv/repo/migrations/20221226174741_create_deepl_accounts.exs @@ -0,0 +1,19 @@ +defmodule Outlook.Repo.Migrations.CreateDeeplAccounts do + use Ecto.Migration + + def change do + create table(:deepl_accounts) do + add :name, :string + add :description, :text + add :auth_key, :string + add :character_limit, :integer + add :character_count, :integer + add :our_character_count, :integer + add :user_id, references(:users, on_delete: :nothing) + + timestamps() + end + + create index(:deepl_accounts, [:user_id]) + end +end diff --git a/test/outlook/translators_test.exs b/test/outlook/translators_test.exs new file mode 100644 index 0000000..3f38a5e --- /dev/null +++ b/test/outlook/translators_test.exs @@ -0,0 +1,69 @@ +defmodule Outlook.TranslatorsTest do + use Outlook.DataCase + + alias Outlook.Translators + + describe "deepl_accounts" do + alias Outlook.Translators.DeeplAccount + + import Outlook.TranslatorsFixtures + + @invalid_attrs %{auth_key: nil, character_count: nil, character_limit: nil, description: nil, name: nil, our_character_count: nil} + + test "list_deepl_accounts/0 returns all deepl_accounts" do + deepl_account = deepl_account_fixture() + assert Translators.list_deepl_accounts() == [deepl_account] + end + + test "get_deepl_account!/1 returns the deepl_account with given id" do + deepl_account = deepl_account_fixture() + assert Translators.get_deepl_account!(deepl_account.id) == deepl_account + end + + test "create_deepl_account/1 with valid data creates a deepl_account" do + valid_attrs = %{auth_key: "some auth_key", character_count: 42, character_limit: 42, description: "some description", name: "some name", our_character_count: 42} + + assert {:ok, %DeeplAccount{} = deepl_account} = Translators.create_deepl_account(valid_attrs) + assert deepl_account.auth_key == "some auth_key" + assert deepl_account.character_count == 42 + assert deepl_account.character_limit == 42 + assert deepl_account.description == "some description" + assert deepl_account.name == "some name" + assert deepl_account.our_character_count == 42 + end + + test "create_deepl_account/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Translators.create_deepl_account(@invalid_attrs) + end + + test "update_deepl_account/2 with valid data updates the deepl_account" do + deepl_account = deepl_account_fixture() + update_attrs = %{auth_key: "some updated auth_key", character_count: 43, character_limit: 43, description: "some updated description", name: "some updated name", our_character_count: 43} + + assert {:ok, %DeeplAccount{} = deepl_account} = Translators.update_deepl_account(deepl_account, update_attrs) + assert deepl_account.auth_key == "some updated auth_key" + assert deepl_account.character_count == 43 + assert deepl_account.character_limit == 43 + assert deepl_account.description == "some updated description" + assert deepl_account.name == "some updated name" + assert deepl_account.our_character_count == 43 + end + + test "update_deepl_account/2 with invalid data returns error changeset" do + deepl_account = deepl_account_fixture() + assert {:error, %Ecto.Changeset{}} = Translators.update_deepl_account(deepl_account, @invalid_attrs) + assert deepl_account == Translators.get_deepl_account!(deepl_account.id) + end + + test "delete_deepl_account/1 deletes the deepl_account" do + deepl_account = deepl_account_fixture() + assert {:ok, %DeeplAccount{}} = Translators.delete_deepl_account(deepl_account) + assert_raise Ecto.NoResultsError, fn -> Translators.get_deepl_account!(deepl_account.id) end + end + + test "change_deepl_account/1 returns a deepl_account changeset" do + deepl_account = deepl_account_fixture() + assert %Ecto.Changeset{} = Translators.change_deepl_account(deepl_account) + end + end +end diff --git a/test/outlook_web/live/deepl_account_live_test.exs b/test/outlook_web/live/deepl_account_live_test.exs new file mode 100644 index 0000000..ffa7520 --- /dev/null +++ b/test/outlook_web/live/deepl_account_live_test.exs @@ -0,0 +1,110 @@ +defmodule OutlookWeb.DeeplAccountLiveTest do + use OutlookWeb.ConnCase + + import Phoenix.LiveViewTest + import Outlook.TranslatorsFixtures + + @create_attrs %{auth_key: "some auth_key", character_count: 42, character_limit: 42, description: "some description", name: "some name", our_character_count: 42} + @update_attrs %{auth_key: "some updated auth_key", character_count: 43, character_limit: 43, description: "some updated description", name: "some updated name", our_character_count: 43} + @invalid_attrs %{auth_key: nil, character_count: nil, character_limit: nil, description: nil, name: nil, our_character_count: nil} + + defp create_deepl_account(_) do + deepl_account = deepl_account_fixture() + %{deepl_account: deepl_account} + end + + describe "Index" do + setup [:create_deepl_account] + + test "lists all deepl_accounts", %{conn: conn, deepl_account: deepl_account} do + {:ok, _index_live, html} = live(conn, ~p"/deepl_accounts") + + assert html =~ "Listing Deepl accounts" + assert html =~ deepl_account.auth_key + end + + test "saves new deepl_account", %{conn: conn} do + {:ok, index_live, _html} = live(conn, ~p"/deepl_accounts") + + assert index_live |> element("a", "New Deepl account") |> render_click() =~ + "New Deepl account" + + assert_patch(index_live, ~p"/deepl_accounts/new") + + assert index_live + |> form("#deepl_account-form", deepl_account: @invalid_attrs) + |> render_change() =~ "can't be blank" + + {:ok, _, html} = + index_live + |> form("#deepl_account-form", deepl_account: @create_attrs) + |> render_submit() + |> follow_redirect(conn, ~p"/deepl_accounts") + + assert html =~ "Deepl account created successfully" + assert html =~ "some auth_key" + end + + test "updates deepl_account in listing", %{conn: conn, deepl_account: deepl_account} do + {:ok, index_live, _html} = live(conn, ~p"/deepl_accounts") + + assert index_live |> element("#deepl_accounts-#{deepl_account.id} a", "Edit") |> render_click() =~ + "Edit Deepl account" + + assert_patch(index_live, ~p"/deepl_accounts/#{deepl_account}/edit") + + assert index_live + |> form("#deepl_account-form", deepl_account: @invalid_attrs) + |> render_change() =~ "can't be blank" + + {:ok, _, html} = + index_live + |> form("#deepl_account-form", deepl_account: @update_attrs) + |> render_submit() + |> follow_redirect(conn, ~p"/deepl_accounts") + + assert html =~ "Deepl account updated successfully" + assert html =~ "some updated auth_key" + end + + test "deletes deepl_account in listing", %{conn: conn, deepl_account: deepl_account} do + {:ok, index_live, _html} = live(conn, ~p"/deepl_accounts") + + assert index_live |> element("#deepl_accounts-#{deepl_account.id} a", "Delete") |> render_click() + refute has_element?(index_live, "#deepl_account-#{deepl_account.id}") + end + end + + describe "Show" do + setup [:create_deepl_account] + + test "displays deepl_account", %{conn: conn, deepl_account: deepl_account} do + {:ok, _show_live, html} = live(conn, ~p"/deepl_accounts/#{deepl_account}") + + assert html =~ "Show Deepl account" + assert html =~ deepl_account.auth_key + end + + test "updates deepl_account within modal", %{conn: conn, deepl_account: deepl_account} do + {:ok, show_live, _html} = live(conn, ~p"/deepl_accounts/#{deepl_account}") + + assert show_live |> element("a", "Edit") |> render_click() =~ + "Edit Deepl account" + + assert_patch(show_live, ~p"/deepl_accounts/#{deepl_account}/show/edit") + + assert show_live + |> form("#deepl_account-form", deepl_account: @invalid_attrs) + |> render_change() =~ "can't be blank" + + {:ok, _, html} = + show_live + |> form("#deepl_account-form", deepl_account: @update_attrs) + |> render_submit() + |> follow_redirect(conn, ~p"/deepl_accounts/#{deepl_account}") + + assert html =~ "Deepl account updated successfully" + assert html =~ "some updated auth_key" + end + end +end diff --git a/test/support/fixtures/translators_fixtures.ex b/test/support/fixtures/translators_fixtures.ex new file mode 100644 index 0000000..d81c573 --- /dev/null +++ b/test/support/fixtures/translators_fixtures.ex @@ -0,0 +1,25 @@ +defmodule Outlook.TranslatorsFixtures do + @moduledoc """ + This module defines test helpers for creating + entities via the `Outlook.Translators` context. + """ + + @doc """ + Generate a deepl_account. + """ + def deepl_account_fixture(attrs \\ %{}) do + {:ok, deepl_account} = + attrs + |> Enum.into(%{ + auth_key: "some auth_key", + character_count: 42, + character_limit: 42, + description: "some description", + name: "some name", + our_character_count: 42 + }) + |> Outlook.Translators.create_deepl_account() + + deepl_account + end +end