Add Translators/deepl_accounts

mix phx.gen.live Translators DeeplAccount deepl_accounts\
  name:string description:text auth_key:string character_limit:integer\
  character_count:integer our_character_count:integer user_id:references:users
This commit is contained in:
Thelonius Kort
2022-12-26 18:51:30 +01:00
parent f66521dba8
commit 9e9c7b5519
12 changed files with 585 additions and 0 deletions

104
lib/outlook/translators.ex Normal file
View File

@ -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

View File

@ -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

View File

@ -0,0 +1,86 @@
defmodule OutlookWeb.DeeplAccountLive.FormComponent do
use OutlookWeb, :live_component
alias Outlook.Translators
@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage deepl_account records in your database.</:subtitle>
</.header>
<.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</.button>
</:actions>
</.simple_form>
</div>
"""
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

View File

@ -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

View File

@ -0,0 +1,44 @@
<.header>
Listing Deepl accounts
<:actions>
<.link patch={~p"/deepl_accounts/new"}>
<.button>New Deepl account</.button>
</.link>
</:actions>
</.header>
<.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>
<:col :let={deepl_account} label="Description"><%= deepl_account.description %></:col>
<:col :let={deepl_account} label="Auth key"><%= deepl_account.auth_key %></:col>
<:col :let={deepl_account} label="Character limit"><%= deepl_account.character_limit %></:col>
<:col :let={deepl_account} label="Character count"><%= deepl_account.character_count %></:col>
<:col :let={deepl_account} label="Our character count"><%= deepl_account.our_character_count %></:col>
<:action :let={deepl_account}>
<div class="sr-only">
<.link navigate={~p"/deepl_accounts/#{deepl_account}"}>Show</.link>
</div>
<.link patch={~p"/deepl_accounts/#{deepl_account}/edit"}>Edit</.link>
</:action>
<:action :let={deepl_account}>
<.link phx-click={JS.push("delete", value: %{id: deepl_account.id})} data-confirm="Are you sure?">
Delete
</.link>
</:action>
</.table>
<.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"}
/>
</.modal>

View File

@ -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

View File

@ -0,0 +1,31 @@
<.header>
Deepl account <%= @deepl_account.id %>
<:subtitle>This is a deepl_account record from your database.</:subtitle>
<:actions>
<.link patch={~p"/deepl_accounts/#{@deepl_account}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit deepl_account</.button>
</.link>
</:actions>
</.header>
<.list>
<:item title="Name"><%= @deepl_account.name %></:item>
<:item title="Description"><%= @deepl_account.description %></:item>
<:item title="Auth key"><%= @deepl_account.auth_key %></:item>
<:item title="Character limit"><%= @deepl_account.character_limit %></:item>
<:item title="Character count"><%= @deepl_account.character_count %></:item>
<:item title="Our character count"><%= @deepl_account.our_character_count %></:item>
</.list>
<.back navigate={~p"/deepl_accounts"}>Back to deepl_accounts</.back>
<.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}"}
/>
</.modal>

View File

@ -90,6 +90,13 @@ defmodule OutlookWeb.Router do
live "/translations/:id", TranslationLive.Show, :show live "/translations/:id", TranslationLive.Show, :show
live "/translations/:id/show/edit", TranslationLive.Show, :edit 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 end
scope "/", OutlookWeb do scope "/", OutlookWeb do

View File

@ -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

View File

@ -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

View File

@ -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&#39;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&#39;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&#39;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

View File

@ -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