File size: 1,936 Bytes
0936134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var express = require('express');
var router = express.Router();

var Posts = require('../models/posts');


/* GET home page. */
router.get('/', function(req, res, next) {
  Posts.all(function(posts) {
    //FIXME: sort function should be modified
    posts.sort(function(a, b){
      return a.created_at > b.created_at;
    });
    console.log(posts);
    res.render('Posts/index', { title: 'HackerNews', showNavbar: true, posts: posts, menu:{}});
  });
});

/* Get new page */
router.get('/new', function(req, res, next) {
  Posts.all(function(posts) {
    posts.sort(function(a, b){
      console.log('compare'+a.created_at + '-' +  b.created_at + (a.created_at < b.created_at) )
      return b.created_at > a.created_at;
    });
    res.render('Posts/index', { title: 'HackerNews', showNavbar: true, posts: posts, menu:{new:true}});
  });
});

/* get show page */
router.get('/show', function(req, res, next) {
  Posts.allShowHN(function(posts) {
    posts.sort(function(a, b){
      // TODO: show filter
      return b.created_at > a.created_at;
    });
    res.render('Posts/index', { title: 'Show', showNavbar: true, posts: posts, menu:{show:true}});
  });
});

/* get ask page */
router.get('/ask', function(req, res, next) {
  Posts.allAskHN(function(posts) {
    posts.sort(function(a, b){
      // TODO: ask filter
      return b.created_at > a.created_at;
    });
    res.render('Posts/index', { title: 'Ask', showNavbar: true, posts: posts, menu:{ask:true}});
  });
});





/* get create new post page */
router.get('/submit', function(req, res, next) {
  res.render('Posts/submit', { title: 'Create New', showNavbar: 1, menu:{submit:true} });
});

/* create new post */
router.post('/submit', function(req, res, next) {
  // TODO: need to replace the user name here
  Posts.createNewPost('HocNX',req.body.title, req.body.url, req.body.text, function(){
    res.redirect('/posts');
  });
});





module.exports = router;