Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Job. #2

Merged
merged 1 commit into from Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions priv/repo/migrations/20160819173233_create_job.exs
@@ -0,0 +1,14 @@
defmodule Outcomes.Repo.Migrations.CreateJob do
use Ecto.Migration

def change do
create table(:jobs) do
add :name, :string
add :company_id, references(:companies, on_delete: :nothing)

timestamps()
end
create index(:jobs, [:company_id])

end
end
66 changes: 66 additions & 0 deletions test/controllers/job_controller_test.exs
@@ -0,0 +1,66 @@
defmodule Outcomes.JobControllerTest do
use Outcomes.ConnCase

alias Outcomes.Job
@valid_attrs %{name: "some content"}
@invalid_attrs %{}

test "lists all entries on index", %{conn: conn} do
conn = get conn, job_path(conn, :index)
assert html_response(conn, 200) =~ "Listing jobs"
end

test "renders form for new resources", %{conn: conn} do
conn = get conn, job_path(conn, :new)
assert html_response(conn, 200) =~ "New job"
end

test "creates resource and redirects when data is valid", %{conn: conn} do
conn = post conn, job_path(conn, :create), job: @valid_attrs
assert redirected_to(conn) == job_path(conn, :index)
assert Repo.get_by(Job, @valid_attrs)
end

test "does not create resource and renders errors when data is invalid", %{conn: conn} do
conn = post conn, job_path(conn, :create), job: @invalid_attrs
assert html_response(conn, 200) =~ "New job"
end

test "shows chosen resource", %{conn: conn} do
job = Repo.insert! %Job{}
conn = get conn, job_path(conn, :show, job)
assert html_response(conn, 200) =~ "Show job"
end

test "renders page not found when id is nonexistent", %{conn: conn} do
assert_error_sent 404, fn ->
get conn, job_path(conn, :show, -1)
end
end

test "renders form for editing chosen resource", %{conn: conn} do
job = Repo.insert! %Job{}
conn = get conn, job_path(conn, :edit, job)
assert html_response(conn, 200) =~ "Edit job"
end

test "updates chosen resource and redirects when data is valid", %{conn: conn} do
job = Repo.insert! %Job{}
conn = put conn, job_path(conn, :update, job), job: @valid_attrs
assert redirected_to(conn) == job_path(conn, :show, job)
assert Repo.get_by(Job, @valid_attrs)
end

test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
job = Repo.insert! %Job{}
conn = put conn, job_path(conn, :update, job), job: @invalid_attrs
assert html_response(conn, 200) =~ "Edit job"
end

test "deletes chosen resource", %{conn: conn} do
job = Repo.insert! %Job{}
conn = delete conn, job_path(conn, :delete, job)
assert redirected_to(conn) == job_path(conn, :index)
refute Repo.get(Job, job.id)
end
end
18 changes: 18 additions & 0 deletions test/models/job_test.exs
@@ -0,0 +1,18 @@
defmodule Outcomes.JobTest do
use Outcomes.ModelCase

alias Outcomes.Job

@valid_attrs %{name: "some content"}
@invalid_attrs %{}

test "changeset with valid attributes" do
changeset = Job.changeset(%Job{}, @valid_attrs)
assert changeset.valid?
end

test "changeset with invalid attributes" do
changeset = Job.changeset(%Job{}, @invalid_attrs)
refute changeset.valid?
end
end
70 changes: 70 additions & 0 deletions web/controllers/job_controller.ex
@@ -0,0 +1,70 @@
defmodule Outcomes.JobController do
use Outcomes.Web, :controller

alias Outcomes.Job
alias Outcomes.Company

def index(conn, _params) do
jobs = Repo.all(Job)
render(conn, "index.html", jobs: jobs)
end

def new(conn, _params) do
changeset = Job.changeset(%Job{})
companies = Company.select_opts(Repo.all(Company))
render(conn, "new.html", changeset: changeset, companies: companies)
end

def create(conn, %{"job" => job_params}) do
changeset = Job.changeset(%Job{}, job_params)
companies = Company.select_opts(Repo.all(Company))

case Repo.insert(changeset) do
{:ok, _job} ->
conn
|> put_flash(:info, "Job created successfully.")
|> redirect(to: job_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset, companies: companies)
end
end

def show(conn, %{"id" => id}) do
job = Repo.get!(Job, id)
render(conn, "show.html", job: job)
end

def edit(conn, %{"id" => id}) do
job = Repo.get!(Job, id)
changeset = Job.changeset(job)
companies = Company.select_opts(Repo.all(Company))
render(conn, "edit.html", job: job, changeset: changeset, companies: companies)
end

def update(conn, %{"id" => id, "job" => job_params}) do
job = Repo.get!(Job, id)
changeset = Job.changeset(job, job_params)
companies = Company.select_opts(Repo.all(Company))

case Repo.update(changeset) do
{:ok, job} ->
conn
|> put_flash(:info, "Job updated successfully.")
|> redirect(to: job_path(conn, :show, job))
{:error, changeset} ->
render(conn, "edit.html", job: job, changeset: changeset, companies: companies)
end
end

def delete(conn, %{"id" => id}) do
job = Repo.get!(Job, 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!(job)

conn
|> put_flash(:info, "Job deleted successfully.")
|> redirect(to: job_path(conn, :index))
end
end
10 changes: 10 additions & 0 deletions web/models/company.ex
Expand Up @@ -5,6 +5,8 @@ defmodule Outcomes.Company do
field :name, :string

timestamps()

has_many :jobs, Outcomes.Job
end

@doc """
Expand All @@ -15,4 +17,12 @@ defmodule Outcomes.Company do
|> cast(params, [:name])
|> validate_required([:name])
end

def select_opts(companies) do
Enum.flat_map companies, fn(company) -> company_tuple(company) end
end

def company_tuple(company) do
%{"#{company.name}": company.id}
end
end
21 changes: 21 additions & 0 deletions web/models/job.ex
@@ -0,0 +1,21 @@
defmodule Outcomes.Job do
use Outcomes.Web, :model

schema "jobs" do
field :name, :string

timestamps()

belongs_to :company, Outcomes.Company
end

@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name, :company_id])
|> validate_required([:name])
|> validate_required([:company_id])
end
end
1 change: 1 addition & 0 deletions web/router.ex
Expand Up @@ -19,6 +19,7 @@ defmodule Outcomes.Router do
get "/", PageController, :index
resources "/users", UserController
resources "/companies", CompanyController
resources "/jobs", JobController
end

# Other scopes may use custom stacks.
Expand Down
7 changes: 7 additions & 0 deletions web/templates/job/edit.html.eex
@@ -0,0 +1,7 @@
<h2>Edit job</h2>

<%= render "form.html", changeset: @changeset,
companies: @companies,
action: job_path(@conn, :update, @job) %>

<%= link "Back", to: job_path(@conn, :index) %>
26 changes: 26 additions & 0 deletions web/templates/job/form.html.eex
@@ -0,0 +1,26 @@
<%= form_for @changeset, @action, fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<%
IO.inspect @companies
%>

<div class="form-group">
<%= label f, :company_id, class: "control-label" %>
<%= select f, :company_id, @companies, prompt: "Company", class: "form-control" %>
<%= error_tag f, :company_id %>
</div>

<div class="form-group">
<%= label f, :name, class: "control-label" %>
<%= text_input f, :name, class: "form-control" %>
<%= error_tag f, :name %>
</div>

<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
28 changes: 28 additions & 0 deletions web/templates/job/index.html.eex
@@ -0,0 +1,28 @@
<h2>Listing jobs</h2>

<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Company</th>

<th></th>
</tr>
</thead>
<tbody>
<%= for job <- @jobs do %>
<tr>
<td><%= job.name %></td>
<td><%= job.company_id %></td>

<td class="text-right">
<%= link "Show", to: job_path(@conn, :show, job), class: "btn btn-default btn-xs" %>
<%= link "Edit", to: job_path(@conn, :edit, job), class: "btn btn-default btn-xs" %>
<%= link "Delete", to: job_path(@conn, :delete, job), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %>
</td>
</tr>
<% end %>
</tbody>
</table>

<%= link "New job", to: job_path(@conn, :new) %>
7 changes: 7 additions & 0 deletions web/templates/job/new.html.eex
@@ -0,0 +1,7 @@
<h2>New job</h2>

<%= render "form.html", changeset: @changeset,
companies: @companies,
action: job_path(@conn, :create) %>

<%= link "Back", to: job_path(@conn, :index) %>
18 changes: 18 additions & 0 deletions web/templates/job/show.html.eex
@@ -0,0 +1,18 @@
<h2>Show job</h2>

<ul>

<li>
<strong>Name:</strong>
<%= @job.name %>
</li>

<li>
<strong>Company:</strong>
<%= @job.company_id %>
</li>

</ul>

<%= link "Edit", to: job_path(@conn, :edit, @job) %>
<%= link "Back", to: job_path(@conn, :index) %>
3 changes: 1 addition & 2 deletions web/templates/layout/app.html.eex
Expand Up @@ -7,7 +7,7 @@
<meta name="description" content="">
<meta name="author" content="">

<title>Hello Outcomes!</title>
<title>Outcomes!</title>
<link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>">
</head>

Expand All @@ -19,7 +19,6 @@
<li><a href="http://www.phoenixframework.org/docs">Get Started</a></li>
</ul>
</nav>
<span class="logo"></span>
</header>

<p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
Expand Down
2 changes: 1 addition & 1 deletion web/templates/page/index.html.eex
@@ -1,5 +1,5 @@
<div class="jumbotron">
<h2><%= gettext "Welcome to %{name}", name: "Phoenix!" %></h2>
<h2><%= gettext "%{name}", name: "Outcomes!" %></h2>
<p class="lead">A productive web framework that<br />does not compromise speed and maintainability.</p>
</div>

Expand Down
3 changes: 3 additions & 0 deletions web/views/job_view.ex
@@ -0,0 +1,3 @@
defmodule Outcomes.JobView do
use Outcomes.Web, :view
end