Refactor and add securities controller
This commit is contained in:
104
lib/tradex/instruments.ex
Normal file
104
lib/tradex/instruments.ex
Normal file
@@ -0,0 +1,104 @@
|
||||
defmodule Tradex.Instruments do
|
||||
@moduledoc """
|
||||
The Instruments context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
|
||||
alias Tradex.Instruments.Security
|
||||
alias Tradex.Repo
|
||||
|
||||
@doc """
|
||||
Returns the list of securities.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_securities()
|
||||
[%Security{}, ...]
|
||||
|
||||
"""
|
||||
def list_securities do
|
||||
Repo.all(Security)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single security.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Security does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_security!(123)
|
||||
%Security{}
|
||||
|
||||
iex> get_security!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_security!(id), do: Repo.get!(Security, id)
|
||||
|
||||
@doc """
|
||||
Creates a security.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_security(%{field: value})
|
||||
{:ok, %Security{}}
|
||||
|
||||
iex> create_security(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_security(attrs \\ %{}) do
|
||||
%Security{}
|
||||
|> Security.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a security.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_security(security, %{field: new_value})
|
||||
{:ok, %Security{}}
|
||||
|
||||
iex> update_security(security, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_security(%Security{} = security, attrs) do
|
||||
security
|
||||
|> Security.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a security.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_security(security)
|
||||
{:ok, %Security{}}
|
||||
|
||||
iex> delete_security(security)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_security(%Security{} = security) do
|
||||
Repo.delete(security)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking security changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_security(security)
|
||||
%Ecto.Changeset{data: %Security{}}
|
||||
|
||||
"""
|
||||
def change_security(%Security{} = security, attrs \\ %{}) do
|
||||
Security.changeset(security, attrs)
|
||||
end
|
||||
end
|
18
lib/tradex/instruments/historical_quote.ex
Normal file
18
lib/tradex/instruments/historical_quote.ex
Normal file
@@ -0,0 +1,18 @@
|
||||
defmodule Tradex.Instruments.HistoricalQuote do
|
||||
@moduledoc false
|
||||
use Tradex.Schema
|
||||
|
||||
schema "historical_quotes" do
|
||||
field :date, :date
|
||||
field :open, :decimal
|
||||
field :high, :decimal
|
||||
field :low, :decimal
|
||||
field :close, :decimal
|
||||
field :adj_close, :decimal
|
||||
field :volume, :integer
|
||||
|
||||
belongs_to :security, Tradex.Instruments.Security
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
end
|
24
lib/tradex/instruments/security.ex
Normal file
24
lib/tradex/instruments/security.ex
Normal file
@@ -0,0 +1,24 @@
|
||||
defmodule Tradex.Instruments.Security do
|
||||
@moduledoc false
|
||||
use Tradex.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "securities" do
|
||||
field :name, :string
|
||||
field :ticker, :string
|
||||
field :wkn, :string
|
||||
field :isin, :string
|
||||
field :expiration_date, :date
|
||||
field :strike_price, :decimal
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(security, attrs) do
|
||||
security
|
||||
|> cast(attrs, [:name, :ticker, :wkn, :isin, :expiration_date, :strike_price])
|
||||
|> validate_required([:name, :ticker, :wkn, :isin, :expiration_date, :strike_price])
|
||||
end
|
||||
end
|
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>
|
@@ -22,6 +22,7 @@ defmodule TradexWeb.Router do
|
||||
pipe_through :browser
|
||||
|
||||
get "/", PageController, :home
|
||||
resources "/securities", SecurityController
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
|
Reference in New Issue
Block a user