This repository was archived by the owner on Apr 11, 2021. It is now read-only.
File tree 5 files changed +35
-1
lines changed
5 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -8,4 +8,12 @@ defmodule CatuumApi.CatsController do
8
8
|> put_resp_content_type ( cat . content_type )
9
9
|> send_file ( 200 , cat . path )
10
10
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
11
19
end
Original file line number Diff line number Diff line change @@ -20,6 +20,19 @@ defmodule CatuumApi.Cat do
20
20
end
21
21
end
22
22
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
+
23
36
def random do
24
37
filename = Repo . srandmember ( @ small_cats_key )
25
38
ext = Path . extname ( filename ) |> String . replace ( "." , "" )
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ defmodule CatuumApi.Router do
2
2
use CatuumApi.Web , :router
3
3
4
4
pipeline :browser do
5
- plug :accepts , [ "html" ]
5
+ plug :accepts , [ "html" , "json" ]
6
6
plug :fetch_session
7
7
plug :fetch_flash
8
8
plug :protect_from_forgery
@@ -12,6 +12,7 @@ defmodule CatuumApi.Router do
12
12
scope "/" , CatuumApi do
13
13
pipe_through :browser
14
14
15
+ get "/cats/:count" , CatsController , :count
15
16
get "/cats" , CatsController , :index
16
17
end
17
18
end
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ defmodule CatuumApi.LayoutView do
2
+ @ moduledoc false
3
+
4
+ use CatuumApi.Web , :view
5
+ end
You can’t perform that action at this time.
0 commit comments