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