Refactor and add securities controller
This commit is contained in:
62
lib/tradex_web/controllers/security_controller.ex
Normal file
62
lib/tradex_web/controllers/security_controller.ex
Normal file
@@ -0,0 +1,62 @@
|
||||
defmodule TradexWeb.SecurityController do
|
||||
use TradexWeb, :controller
|
||||
|
||||
alias Tradex.Instruments
|
||||
alias Tradex.Instruments.Security
|
||||
|
||||
def index(conn, _params) do
|
||||
securities = Instruments.list_securities()
|
||||
render(conn, :index, securities: securities)
|
||||
end
|
||||
|
||||
def new(conn, _params) do
|
||||
changeset = Instruments.change_security(%Security{})
|
||||
render(conn, :new, changeset: changeset)
|
||||
end
|
||||
|
||||
def create(conn, %{"security" => security_params}) do
|
||||
case Instruments.create_security(security_params) do
|
||||
{:ok, security} ->
|
||||
conn
|
||||
|> put_flash(:info, "Security created successfully.")
|
||||
|> redirect(to: ~p"/securities/#{security}")
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, :new, changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
security = Instruments.get_security!(id)
|
||||
render(conn, :show, security: security)
|
||||
end
|
||||
|
||||
def edit(conn, %{"id" => id}) do
|
||||
security = Instruments.get_security!(id)
|
||||
changeset = Instruments.change_security(security)
|
||||
render(conn, :edit, security: security, changeset: changeset)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "security" => security_params}) do
|
||||
security = Instruments.get_security!(id)
|
||||
|
||||
case Instruments.update_security(security, security_params) do
|
||||
{:ok, security} ->
|
||||
conn
|
||||
|> put_flash(:info, "Security updated successfully.")
|
||||
|> redirect(to: ~p"/securities/#{security}")
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, :edit, security: security, changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
security = Instruments.get_security!(id)
|
||||
{:ok, _security} = Instruments.delete_security(security)
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "Security deleted successfully.")
|
||||
|> redirect(to: ~p"/securities")
|
||||
end
|
||||
end
|
13
lib/tradex_web/controllers/security_html.ex
Normal file
13
lib/tradex_web/controllers/security_html.ex
Normal file
@@ -0,0 +1,13 @@
|
||||
defmodule TradexWeb.SecurityHTML do
|
||||
use TradexWeb, :html
|
||||
|
||||
embed_templates "security_html/*"
|
||||
|
||||
@doc """
|
||||
Renders a security form.
|
||||
"""
|
||||
attr :changeset, Ecto.Changeset, required: true
|
||||
attr :action, :string, required: true
|
||||
|
||||
def security_form(assigns)
|
||||
end
|
8
lib/tradex_web/controllers/security_html/edit.html.heex
Normal file
8
lib/tradex_web/controllers/security_html/edit.html.heex
Normal file
@@ -0,0 +1,8 @@
|
||||
<.header>
|
||||
Edit Security {@security.id}
|
||||
<:subtitle>Use this form to manage security records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.security_form changeset={@changeset} action={~p"/securities/#{@security}"} />
|
||||
|
||||
<.back navigate={~p"/securities"}>Back to securities</.back>
|
28
lib/tradex_web/controllers/security_html/index.html.heex
Normal file
28
lib/tradex_web/controllers/security_html/index.html.heex
Normal file
@@ -0,0 +1,28 @@
|
||||
<.header>
|
||||
Listing Securities
|
||||
<:actions>
|
||||
<.link href={~p"/securities/new"}>
|
||||
<.button>New Security</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.table id="securities" rows={@securities} row_click={&JS.navigate(~p"/securities/#{&1}")}>
|
||||
<:col :let={security} label="Name">{security.name}</:col>
|
||||
<:col :let={security} label="Ticker">{security.ticker}</:col>
|
||||
<:col :let={security} label="Wkn">{security.wkn}</:col>
|
||||
<:col :let={security} label="Isin">{security.isin}</:col>
|
||||
<:col :let={security} label="Expiration date">{security.expiration_date}</:col>
|
||||
<:col :let={security} label="Strike price">{security.strike_price}</:col>
|
||||
<:action :let={security}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/securities/#{security}"}>Show</.link>
|
||||
</div>
|
||||
<.link navigate={~p"/securities/#{security}/edit"}>Edit</.link>
|
||||
</:action>
|
||||
<:action :let={security}>
|
||||
<.link href={~p"/securities/#{security}"} method="delete" data-confirm="Are you sure?">
|
||||
Delete
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
8
lib/tradex_web/controllers/security_html/new.html.heex
Normal file
8
lib/tradex_web/controllers/security_html/new.html.heex
Normal file
@@ -0,0 +1,8 @@
|
||||
<.header>
|
||||
New Security
|
||||
<:subtitle>Use this form to manage security records in your database.</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.security_form changeset={@changeset} action={~p"/securities"} />
|
||||
|
||||
<.back navigate={~p"/securities"}>Back to securities</.back>
|
@@ -0,0 +1,14 @@
|
||||
<.simple_form :let={f} for={@changeset} action={@action}>
|
||||
<.error :if={@changeset.action}>
|
||||
Oops, something went wrong! Please check the errors below.
|
||||
</.error>
|
||||
<.input field={f[:name]} type="text" label="Name" />
|
||||
<.input field={f[:ticker]} type="text" label="Ticker" />
|
||||
<.input field={f[:wkn]} type="text" label="Wkn" />
|
||||
<.input field={f[:isin]} type="text" label="Isin" />
|
||||
<.input field={f[:expiration_date]} type="date" label="Expiration date" />
|
||||
<.input field={f[:strike_price]} type="number" label="Strike price" step="any" />
|
||||
<:actions>
|
||||
<.button>Save Security</.button>
|
||||
</:actions>
|
||||
</.simple_form>
|
20
lib/tradex_web/controllers/security_html/show.html.heex
Normal file
20
lib/tradex_web/controllers/security_html/show.html.heex
Normal file
@@ -0,0 +1,20 @@
|
||||
<.header>
|
||||
Security {@security.id}
|
||||
<:subtitle>This is a security record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.link href={~p"/securities/#{@security}/edit"}>
|
||||
<.button>Edit security</.button>
|
||||
</.link>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Name">{@security.name}</:item>
|
||||
<:item title="Ticker">{@security.ticker}</:item>
|
||||
<:item title="Wkn">{@security.wkn}</:item>
|
||||
<:item title="Isin">{@security.isin}</:item>
|
||||
<:item title="Expiration date">{@security.expiration_date}</:item>
|
||||
<:item title="Strike price">{@security.strike_price}</:item>
|
||||
</.list>
|
||||
|
||||
<.back navigate={~p"/securities"}>Back to securities</.back>
|
Reference in New Issue
Block a user