Skip to content

Commit

Permalink
enum
Browse files Browse the repository at this point in the history
This commit has enum 1 and enum 2
  • Loading branch information
hexisdylan committed Jul 23, 2022
1 parent 4bad5c2 commit 2136779
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
11 changes: 11 additions & 0 deletions lib/bear.ex
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
30 changes: 30 additions & 0 deletions lib/bear_controller.ex
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
16 changes: 6 additions & 10 deletions lib/handler.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Servy.Handler do
alias Servy.Conv
alias Servy.BearController
import Servy.Plugins, only: [rewrite_path: 1, log: 1, track: 1]
import Servy.Parser, only: [parse: 1]
@moduledoc "Handles HTTP requests."
Expand All @@ -24,22 +25,17 @@ defmodule Servy.Handler do
end

def route(%Conv{method: "GET", path: "/bears"} = conv) do
%{conv | status: 200, resp_body: "Teddy, Smokey, Paddington"}
BearController.index(conv)
end

def route(%Conv{method: "GET", path: "/bears" <> id} = conv) do
%{conv | status: 200, resp_body: "Bear #{id}"}
def route(%Conv{method: "GET", path: "/bears/" <> id} = conv) do
params = Map.put(conv.params, "id", id)
BearController.show(conv, params)
end

# name=Baloo&type=Brown
def route(%Conv{method: "POST", path: "/bears"} = conv) do
params = %{"name" => "Baloo", "type" => "Brown"}

%{
conv
| status: 201,
resp_body: "Created a #{conv.params["type"]} bear named #{conv.params["name"]}"
}
BearController.create(conv, conv.params)
end

# def route(%Conv{method: "GET", path: "/about"} = conv) do
Expand Down
26 changes: 26 additions & 0 deletions lib/wildthings.ex
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

0 comments on commit 2136779

Please sign in to comment.