1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- defmodule GiomWeb.StockController do
- use GiomWeb, :controller
- alias Giom.Accounting
- alias Giom.Accounting.Stock
- def index(conn, _params) do
- stocks = Accounting.list_stocks()
- render(conn, "index.html", stocks: stocks)
- end
- def new(conn, _params) do
- changeset = Accounting.change_stock(%Stock{})
- render(conn, "new.html", changeset: changeset)
- end
- def create(conn, %{"stock" => stock_params}) do
- case Accounting.create_stock(stock_params) do
- {:ok, stock} ->
- conn
- |> put_flash(:info, "Stock created successfully.")
- |> redirect(to: stock_path(conn, :show, stock))
- {:error, %Ecto.Changeset{} = changeset} ->
- render(conn, "new.html", changeset: changeset)
- end
- end
- def show(conn, %{"id" => id}) do
- stock = Accounting.get_stock!(id)
- render(conn, "show.html", stock: stock)
- end
- def edit(conn, %{"id" => id}) do
- stock = Accounting.get_stock!(id)
- changeset = Accounting.change_stock(stock)
- render(conn, "edit.html", stock: stock, changeset: changeset)
- end
- def update(conn, %{"id" => id, "stock" => stock_params}) do
- stock = Accounting.get_stock!(id)
- case Accounting.update_stock(stock, stock_params) do
- {:ok, stock} ->
- conn
- |> put_flash(:info, "Stock updated successfully.")
- |> redirect(to: stock_path(conn, :show, stock))
- {:error, %Ecto.Changeset{} = changeset} ->
- render(conn, "edit.html", stock: stock, changeset: changeset)
- end
- end
- def delete(conn, %{"id" => id}) do
- stock = Accounting.get_stock!(id)
- {:ok, _stock} = Accounting.delete_stock(stock)
- conn
- |> put_flash(:info, "Stock deleted successfully.")
- |> redirect(to: stock_path(conn, :index))
- end
-
- # API Section
-
- def get_stockname(conn, %{"code" => stock_code}) do
- json(conn, Accounting.get_stock!(stock_code).name)
- end
-
- end
|