Skip to content

Commit

Permalink
admin index of posts
Browse files Browse the repository at this point in the history
  • Loading branch information
TangentFoxy committed Jan 6, 2018
1 parent 34f757c commit e3a8b59
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
25 changes: 20 additions & 5 deletions applications/posts.moon
Expand Up @@ -13,7 +13,7 @@ class extends lapis.Application

[index: "s(/:page[%d])"]: =>
@page = tonumber(@params.page) or 1
Paginator = Posts\paginated "WHERE status = ? ORDER BY published_at DESC", Posts.statuses.published, per_page: 10
Paginator = Posts\paginated "WHERE status = ? ORDER BY published_at DESC", Posts.statuses.published, per_page: 6

@last_page = Paginator\num_pages!
@posts = Paginator\get_page @page
Expand All @@ -26,12 +26,27 @@ class extends lapis.Application
return render: "posts.index"

[view: "/:slug"]: =>
if @post = Posts\find slug: @params.slug
@title = @post.title
return render: "posts.view"
else
@post = Posts\find slug: @params.slug
if (not @post) or (@post.status != Posts.statuses.published and not is_admin @)
@session.info = "That post does not exist."
return redirect_to: @url_for "posts_index"
else
@title = @post.title
return render: "posts.view"

[admin_index: "s/admin/index(/:page[%d])"]: =>
unless is_admin @ return redirect_to: @url_for "posts_index"

@page = tonumber(@params.page) or 1
Paginator = Posts\paginated "ORDER BY updated_at DESC", per_page: 50

@last_page = Paginator\num_pages!
@posts = Paginator\get_page @page
if #@posts < 1 and @last_page > 0
return redirect_to: @url_for "posts_admin_index", page: @last_page

@title = "Admin Posts Index (Page #{@page})"
return render: "posts.admin_index"

[new: "/new"]: respond_to {
before: =>
Expand Down
7 changes: 4 additions & 3 deletions layouts/default.moon
Expand Up @@ -62,7 +62,7 @@ class extends html.Widget
div class: "navbar-dropdown", ->
div class: "navbar-item menu", ->
ul class: "menu-list", ->
li -> a href: "/blog", "All Posts"
li -> a href: "/posts", "All Posts"
li -> a href: "/blog/art", "Art"
li -> a href: "/blog/reviewws", "Reviews"
div class: "navbar-item has-dropdown is-hoverable", ->
Expand Down Expand Up @@ -109,6 +109,7 @@ class extends html.Widget
div class: "navbar-dropdown", ->
div class: "navbar-item menu", ->
ul class: "menu-list", ->
li -> a href: @url_for("posts_admin_index"), "All Posts"
li -> a href: @url_for("posts_new"), "New Post"
div class: "navbar-item", ->
div class: "level", ->
Expand All @@ -126,8 +127,8 @@ class extends html.Widget
div class: "control", ->
a class: "button", href: @url_for("user_new"), "New User"
div class: "level-item", ->
text "This website is open source on "
-- raw "&nbsp;"
-- TODO modify to use icon ?
text "This website is open source on"
raw "&nbsp;"
a href: "https://github.com/Guard13007/guard13007.com", "GitHub"
text "."
34 changes: 34 additions & 0 deletions views/posts/admin_index.moon
@@ -0,0 +1,34 @@
import Widget from require "lapis.html"
import Posts from require "models"
import Pagination from require "widgets"

class PostIndex extends Widget
content: =>
widget Pagination

table class: "table is-bordered is-striped is-narrow is-fullwidth", ->
thead ->
tr ->
th "ID"
th "Title"
th "Status"
th "Type"
th "Updated"
th "Created"
th "View"
th "Edit"
th "Comments"
tbody ->
for post in *@posts
tr ->
td post.id
td post.title
td Posts.statuses\to_name post.status
td Posts.types\to_name post.type
td post.updated_at
td post.created_at
td -> a class: "button", href: @url_for("posts_view", slug: post.slug), "View"
td -> a class: "button", href: @url_for("posts_edit", id: post.id), "Edit"
td -> span class: "disqus-comment-count", "data-disqus-identifier": "https://guard13007.com#{@url_for "posts_view", slug: post.slug}"

widget Pagination
3 changes: 1 addition & 2 deletions views/posts/index.moon
Expand Up @@ -13,9 +13,8 @@ class PostIndex extends Widget
div class: "content", ->
raw post.preview_html
h3 class: "subtitle is-6", ->
text "Published #{pretty_date post.published_at}. "
a href: @url_for("posts_view", slug: post.slug), "Read More"
text " ("
text ". Published #{pretty_date post.published_at} ("
span class: "disqus-comment-count", "data-disqus-identifier": "https://guard13007.com#{@url_for "posts_view", slug: post.slug}"
text ")"
if is_admin @
Expand Down

0 comments on commit e3a8b59

Please sign in to comment.