Skip to content

Commit b8fc115

Browse files
Adds random stories
1 parent 9d1ea73 commit b8fc115

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

web/controllers/story_controller.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ defmodule Fmylife.StoryController do
5252
render(conn, :top_stories, stories: stories, categories: categories)
5353
end
5454

55+
def random(conn, _params) do
56+
stories = Story.random()
57+
categories = Repo.all(Category)
58+
render(conn, :random, stories: stories, categories: categories)
59+
end
60+
5561
end

web/models/story.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@ defmodule Fmylife.Story do
3232
preload: [:user]
3333
)
3434
end
35+
36+
def random do
37+
Fmylife.Repo.all(
38+
from s in Fmylife.Story,
39+
order_by: [desc: fragment("Random()")],
40+
limit: 10,
41+
preload: [:user]
42+
)
43+
end
3544
end

web/router.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ defmodule Fmylife.Router do
4545

4646
get "/", StoryController, :index
4747
get "/top-stories", StoryController, :top_stories
48+
get "/random", StoryController, :random
4849
resources "/stories", StoryController, only: [:show]
4950
end
5051

web/templates/layout/_header.html.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div class="collapse navbar-collapse" id="navbar">
1515
<ul class="nav navbar-nav">
1616
<li class="active"><a href="<%= story_path(@conn, :top_stories) %>">Top Stories<span class="sr-only">(current)</span></a></li>
17-
<li><a href="">Random Stories</a></li>
17+
<li><a href="<%= story_path(@conn, :random) %>">Random Stories</a></li>
1818
</ul>
1919
<form class="navbar-form navbar-left" role="search">
2020
<div class="input-group">

web/templates/story/random.html.eex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div class="container">
2+
<div class="row">
3+
<div class="col-md-8">
4+
<%= render_many @stories, StoryView, "_stories.html", conn: @conn, current_user: @current_user %>
5+
</div><!-- /.col-md-8 -->
6+
<div class="col-md-4 col-sm-12">
7+
<div class="panel panel-default">
8+
<div class="panel-heading text-center">Categories</div>
9+
<ul class="list-group">
10+
<%= render_many @categories, StoryView, "_categories.html", as: :category %>
11+
</ul>
12+
</div><!-- /.panel panel-default -->
13+
</div><!-- /.col-md-4 col-sm-12 -->
14+
</div><!-- /.row -->
15+
</div><!-- /.container -->

0 commit comments

Comments
 (0)