-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
73 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Servy.Bear do | ||
defstruct id: nil, name: "", type: "", hibernating: false | ||
|
||
def is_grizzly(bear) do | ||
bear.type == "Grizzly" | ||
end | ||
|
||
def order_asc_by_name(b1, b2) do | ||
b1.name <= b2.name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule Servy.BearController do | ||
alias Servy.Wildthings | ||
alias Servy.Bear | ||
|
||
defp bear_item(bear) do | ||
"<li>#{bear.name} - #{bear.type}</li>" | ||
end | ||
|
||
def index(conv) do | ||
items = | ||
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>"} | ||
end | ||
|
||
def show(conv, %{"id" => id}) do | ||
bear = Wildthings.get_bear(id) | ||
%{conv | status: 200, resp_body: "<h1>Bear #{bear.id}: #{bear.name}</h1>"} | ||
end | ||
|
||
def create(conv, params) do | ||
%{conv | status: 201, resp_body: "Created a #{params["type"]} bear named #{params["name"]}"} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Servy.Wildthings do | ||
alias Servy.Bear | ||
|
||
def list_bears do | ||
[ | ||
%Bear{id: 1, name: "Teddy", type: "Brown", hibernating: true}, | ||
%Bear{id: 1, name: "Smokey", type: "Black"}, | ||
%Bear{id: 1, name: "Paddington", type: "Brown"}, | ||
%Bear{id: 1, name: "Scarface", type: "Grizzly", hibernating: true}, | ||
%Bear{id: 1, name: "Snow", type: "Polar"}, | ||
%Bear{id: 1, name: "Brutus", type: "Grizzly"}, | ||
%Bear{id: 1, name: "Rosie", type: "Black", hibernating: true}, | ||
%Bear{id: 1, name: "Roscoe", type: "Panda"}, | ||
%Bear{id: 1, name: "Iceman", type: "Polar", hibernating: true}, | ||
%Bear{id: 1, name: "Kenai", type: "Grizzly"} | ||
] | ||
end | ||
|
||
def get_bear(id) when is_integer(id) do | ||
Enum.find(list_bears(), fn b -> b.id == id end) | ||
end | ||
|
||
def get_bear(id) when is_binary(id) do | ||
id |> String.to_integer() |> get_bear | ||
end | ||
end |