Commits

Carbo Kuo committed 4810b06

Implement archive list

  • Participants
  • Parent commits 148799f

Comments (0)

Files changed (4)

models/post.coffee

   contents: [contentSchema]
   contentsFormat: String
   author: String
-  postTime: Date
+  postTime:
+    type: Date
+    index: true
   clicks:
     type: Number
     index: true
 Post.getPopularPosts = (count, next) ->
   Post.find({private:false, list:true}).limit(count).sort('-clicks').exec next
 
+Post.getArchive = (next) ->
+  now = new Date
+  cond = {private:false, list:true}
+  #Get the oldest post
+  Post.find(cond).limit(1).sort('postTime').exec obtain(oldest)
+  if oldest.length is 0
+    oldest = now
+  else
+    oldest = oldest[0].postTime
+  
+  #Generate all months bewteen oldest day until now
+  months = []
+  if now.getFullYear() is oldest.getFullYear()
+    year = now.getFullYear()
+    month = oldest.getMonth()
+    while month <= now.getMonth()
+      months.push new Date(year, month, 1)
+      month++
+  else
+    year = oldest.getFullYear()
+    month = oldest.getMonth()
+    while month < 12
+      months.push new Date(year, month, 1)
+      month++
+    year++
+    while year < now.getFullYear()
+      month = 0
+      while month < 12
+        months.push new Date(year, month, 1)
+        month++
+      year++
+    month = 0
+    while month <= now.getMonth()
+      months.push new Date(year, month, 1)
+      month++
+  
+  #Get post count of each month
+  archives = []
+  for i, start of months
+    i = parseInt(i)
+    end = months[i + 1]
+    cond =
+      private:false
+      list:true
+      postTime:
+        $gte: start
+    if end?
+      cond.postTime.$lt = end
+    Post.count cond, obtain(count)
+    if count > 0
+      archives.push
+        month: start
+        count: count
+  next null, archives
+
 parseTags = (tags) ->
   return [] if not tags
   tags = tags.split ','

routes/post.coffee

   Post.getPosts {private:false, list:true}, page, config.options.postsPerPage, obtain posts
   Post.render posts, language, obtain(posts)
   Post.getPopularPosts config.options.popularPosts, obtain popularPosts
+  Post.getArchive obtain archives
   
   res.render 'postslist',
     posts: posts
     popularPosts: popularPosts
+    archives: archives
     page: page
 
 exports.displayPost = (req, res, next) ->
     post.save obtain()
   
   Post.getPopularPosts config.options.popularPosts, obtain popularPosts
+  Post.getArchive obtain archives
   
   post.render language, obtain(post)
   res.render 'post',
     post: post
     popularPosts: popularPosts
+    archives: archives
 
 exports.displayTag = (req, res, next) ->
   language = req.params[1]
   Post.getPosts {private:false, list:true, tags:tagName}, page, config.options.postsPerPage, obtain posts
   Post.render posts, language, obtain(posts)
   Post.getPopularPosts config.options.popularPosts, obtain popularPosts
+  Post.getArchive obtain archives
   
   res.render 'postslist',
     posts: posts
     popularPosts: popularPosts
+    archives: archives
     page: page

tests/archive.coffee

+'use continuation'
+Post = require '../models/post'
+
+Post.getArchive obtain(archives)
+console.log archives

views/layout.jade

             .sidebarBox
               h4 Archives
               ul
-                li
-                  a(href='/blog/archive/2012/09/') September 2012
-                li
-                  a(href='/blog/archive/2012/07/') July 2012
+                each archive in archives
+                  li
+                    a(href=langLink('/blog/archive/' + archive.month.getFullYear() + '/' + (archive.month.getMonth() + 1))) #{dateFormat(archive.month, 'mmmm yyyy')}
+                    = ' (' + archive.count + ')'
       footer#footer
         .container.clearfix
           .one-fourth.widget-container