Skip to content

Commit

Permalink
Add post sorting (Top/New).
Browse files Browse the repository at this point in the history
  • Loading branch information
bilus committed Aug 17, 2020
1 parent 1201183 commit 1f469d0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -53,7 +53,8 @@ Good luck!
- [X] Downvoting only if upvoted
- [X] Upvoting up to the available budget
- [X] Show user's available votes
- [ ] Top/new
- [X] Top/new
- [ ] Show time ago post created
- [ ] Good embedded editor
- [ ] Tenancy
- [ ] Styling
Expand Down
20 changes: 20 additions & 0 deletions actions/posts.go
Expand Up @@ -42,6 +42,17 @@ func (v PostsResource) List(c buffalo.Context) error {
// Default values are "page=1" and "per_page=20".
q := tx.PaginateFromParams(c.Params())

order := c.Param("order")
if order == "" {
order = "top"
}
c.Set("orderClass", orderClass(order))
if order == "newest" {
q = q.Order("created_at DESC")
} else {
q = q.Order("votes DESC")
}

// Retrieve all Posts from the DB
if err := q.All(posts); err != nil {
return err
Expand All @@ -60,6 +71,15 @@ func (v PostsResource) List(c buffalo.Context) error {
}).Respond(c)
}

func orderClass(activeOrder string) func(order string) string {
return func(order string) string {
if order == activeOrder {
return "order-active"
}
return ""
}
}

// Show gets the data for one Post. This function is mapped to
// the path GET /posts/{post_id}
func (v PostsResource) Show(c buffalo.Context) error {
Expand Down
4 changes: 4 additions & 0 deletions assets/css/application.scss
Expand Up @@ -2,3 +2,7 @@
@import "~@fortawesome/fontawesome-free/css/all.min.css";

@import "buffalo";

.order-active {
color: black;
}
4 changes: 3 additions & 1 deletion templates/posts/index.plush.html
@@ -1,5 +1,7 @@
<div class="py-4 mb-2">
<h3 class="d-inline-block">Posts</h3>
<h3 class="d-inline-block">Posts</h3>
<%= linkTo(postsPath({order: "top"}), {body: "Top", class: orderClass("top")}) %>
<%= linkTo(postsPath({order: "newest"}), {body: "New", class: orderClass("newest")}) %>
<div class="float-right">
<%= linkTo(newPostsPath(), {class: "btn btn-primary"}) { %>
Create New Post
Expand Down

0 comments on commit 1f469d0

Please sign in to comment.