Skip to content

Commit

Permalink
comprehensions
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 24, 2022
1 parent 2136779 commit e0ddef2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## PragTechnologies - Coding Bootcamp

### Each Code Progress can be viewed on my [Commit History](https://github.com/skedaddl3/elixir-pragstudio/commits/master)
#### Each Code Progress can be viewed on my [Commit History](https://github.com/skedaddl3/elixir-pragstudio/commits/master)

1. pragstudio-elixir-01-intro - [Introduction](https://github.com/skedaddl3/elixir-pragstudio/commit/2d7cedd6e95697d686a85a9da1c3b8ddf929c073#diff-ba7efc6b78ec91b2d2aed8b82c1a8eb5ec11ed843c6ab6ff2db4f04e0ccb05b0)

Expand Down
23 changes: 13 additions & 10 deletions lib/bear_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ defmodule Servy.BearController do
alias Servy.Wildthings
alias Servy.Bear

defp bear_item(bear) do
"<li>#{bear.name} - #{bear.type}</li>"
@templates_path Path.expand("../templates", __DIR__)

defp render(conv, template, bindings \\ []) do
content =
@templates_path
|> Path.join(template)
|> EEx.eval_file(bindings)

%{conv | status: 200, resp_body: content}
end

def index(conv) do
items =
bears =
Wildthings.list_bears()
|> Enum.filter(&Bear.is_grizzly(&1))
|> Enum.sort(&Bear.order_asc_by_name(&1, &2))
|> Enum.map(&bear_item(&1))
|> Enum.join()

# TODO: Transform bears to an HTML List

%{conv | status: 200, resp_body: "<ul>#{items}</ul>"}
render(conv, "index.eex", bears: bears)
end

def show(conv, %{"id" => id}) do
bear = Wildthings.get_bear(id)
%{conv | status: 200, resp_body: "<h1>Bear #{bear.id}: #{bear.name}</h1>"}

render(conv, "show.eex", bear: bear)
end

def create(conv, params) do
Expand Down
7 changes: 7 additions & 0 deletions templates/index.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>All the Bears!</h1>

<ul>
<%= for bear <- bears do %>
<li><%= bear.name %> - <%= bear.type %></li>
<%end%>
</ul>
4 changes: 4 additions & 0 deletions templates/show.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>Show Bear</h1>
<p>
Is <%= bear.name %> Hibernating? <strong><%= bear.hibernating %></strong>
</p>

0 comments on commit e0ddef2

Please sign in to comment.