Skip to content
This repository was archived by the owner on Apr 11, 2021. It is now read-only.

Commit 34e6b4a

Browse files
committed
Add count api
1 parent bd4c0f5 commit 34e6b4a

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

web/controllers/cats_controller.ex

+8
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ defmodule CatuumApi.CatsController do
88
|> put_resp_content_type(cat.content_type)
99
|> send_file(200, cat.path)
1010
end
11+
12+
def count(conn, %{"count" => count}) do
13+
cats = CatuumApi.Cat.random(count)
14+
15+
conn
16+
|> assign(:cats, cats)
17+
|> render("index.json")
18+
end
1119
end

web/models/cat.ex

+13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ defmodule CatuumApi.Cat do
2020
end
2121
end
2222

23+
def random(count) do
24+
filenames = Repo.srandmember([@small_cats_key, count])
25+
26+
27+
filenames |> Enum.map(fn(filename) ->
28+
ext = Path.extname(filename) |> String.replace(".", "")
29+
%{
30+
path: @uploads_path <> "/" <> filename,
31+
content_type: "image/" <> ext
32+
}
33+
end)
34+
end
35+
2336
def random do
2437
filename = Repo.srandmember(@small_cats_key)
2538
ext = Path.extname(filename) |> String.replace(".", "")

web/router.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule CatuumApi.Router do
22
use CatuumApi.Web, :router
33

44
pipeline :browser do
5-
plug :accepts, ["html"]
5+
plug :accepts, ["html", "json"]
66
plug :fetch_session
77
plug :fetch_flash
88
plug :protect_from_forgery
@@ -12,6 +12,7 @@ defmodule CatuumApi.Router do
1212
scope "/", CatuumApi do
1313
pipe_through :browser
1414

15+
get "/cats/:count", CatsController, :count
1516
get "/cats", CatsController, :index
1617
end
1718
end

web/views/cats_view.ex

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule CatuumApi.CatsView do
2+
use CatuumApi.Web, :view
3+
4+
def render("index.json", %{cats: cats}) do
5+
cats
6+
end
7+
end

web/views/layout_view.ex

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule CatuumApi.LayoutView do
2+
@moduledoc false
3+
4+
use CatuumApi.Web, :view
5+
end

0 commit comments

Comments
 (0)