Skip to content

Instantly share code, notes, and snippets.

@MartinElvar
Created September 5, 2015 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinElvar/f53174ac8b2df1e85287 to your computer and use it in GitHub Desktop.
Save MartinElvar/f53174ac8b2df1e85287 to your computer and use it in GitHub Desktop.
defmodule Mobil.Admin.CompanyController do
use Mobil.Web, :controller
alias Mobil.Company
plug :scrub_params, "company" when action in [:create, :update]
def index(conn, _params) do
companies = Repo.all(Company)
render(conn, "index.html", companies: companies)
end
def new(conn, _params) do
IO.puts "----"
IO.inspect conn.assigns
IO.puts "----"
changeset = Company.changeset(%Company{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"company" => company_params}) do
IO.inspect company_params
changeset = Company.changeset(%Company{}, company_params)
Logo.store({company_params.logo, changeset})
case Repo.insert(changeset) do
{:ok, _company} ->
conn
|> put_flash(:info, "Company created successfully.")
|> redirect(to: admin_company_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
company = Repo.get!(Company, id)
render(conn, "show.html", company: company)
end
def edit(conn, %{"id" => id}) do
company = Repo.get!(Company, id)
changeset = Company.changeset(company)
render(conn, "edit.html", company: company, changeset: changeset)
end
def update(conn, %{"id" => id, "company" => company_params}) do
company = Repo.get!(Company, id)
changeset = Company.changeset(company, company_params)
case Repo.update(changeset) do
{:ok, company} ->
conn
|> put_flash(:info, "Company updated successfully.")
|> redirect(to: admin_company_path(conn, :show, company))
{:error, changeset} ->
render(conn, "edit.html", company: company, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
company = Repo.get!(Company, id)
# Here we use delete! (with a bang) because we expect
# it to always work (and if it does not, it will raise).
Repo.delete!(company)
conn
|> put_flash(:info, "Company deleted successfully.")
|> redirect(to: admin_company_path(conn, :index))
end
end
defmodule Mobil.Admin.CompanyView do
use Mobil.Web, :view
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment