Skip to content

Commit b12362a

Browse files
author
joeltaylor
committed
Change home page to use News index
I decided to just pull the News index logic over into the index route, but I plan on cleaning all the routes up soon. This will do for now.
1 parent 0d1e9f0 commit b12362a

File tree

2 files changed

+72
-72
lines changed

2 files changed

+72
-72
lines changed

server/routes/index.js

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,85 @@ var router = express.Router();
33
var path = require('path');
44
var bodyParser = require('body-parser');
55
var parseForm = bodyParser.urlencoded({ extended: false });
6+
var Articles = require(path.join(__dirname, '..', 'services', 'articles'));
7+
var Users = require(path.join(__dirname, '..', 'services', 'users'));
8+
var Comments = require(path.join(__dirname, '..', 'services', 'comments'));
9+
var moment = require('moment');
10+
var _ = require('lodash');
611
var mcapi = require('mailchimp-api');
712

813
mc = new mcapi.Mailchimp(process.env.MAILCHIMP_API);
914

1015
/* GET home page. */
11-
router.get('/', function(req, res, next) {
12-
res.render('index');
16+
router.get('/', parseForm, function(req, res) {
17+
var offset = req.query.page;
18+
var more;
19+
if (offset) {
20+
Articles.paginated(offset, function(all) {
21+
Articles.totalPublished(function(total) {
22+
var lastDay;
23+
// TODO: Move date functionality into a serivce. It'll be used practically
24+
// everywhere. Oh, and refactor this blasphemy.
25+
//
26+
// TODO: We shouldn't be needing to use moment in order to make the time
27+
// UTC. There's an issue with pg and it's parsing the dates from the db
28+
// incorrectly. This is a temporary fix until I can snipe the bug.
29+
all.map(function(item){
30+
item.date = moment.utc(item.published_at).format('LL');
31+
if (item.date == moment.utc(Date.now()).format('LL')){
32+
item.date = 'Today'
33+
} else if (item.date == moment.utc(Date.now()).subtract(1, 'days').format('LL')){
34+
item.date = 'Yesterday'
35+
}else{
36+
item.date = moment(item.date).format('dddd, LL');
37+
}
38+
});
39+
// There are strings and integers here - not so good.
40+
if (all.length) {
41+
lastDay = all[all.length - 1].date;
42+
}
43+
more = (all.length == (total[0].count - offset )) ? false : true;
44+
all = _.groupBy(all, 'date');
45+
res.json({flow: all, more: more, lastDay: lastDay});
46+
});
47+
});
48+
}else{
49+
Articles.recent(function(all) {
50+
Articles.totalPublished(function(total) {
51+
var flow = [], news = [], lastDay;
52+
// TODO: Move date functionality into a serivce. It'll be used practically
53+
// everywhere. Oh, and refactor this blasphemy.
54+
//
55+
// TODO: We shouldn't be needing to use moment in order to make the time
56+
// UTC. There's an issue with pg and it's parsing the dates from the db
57+
// incorrectly. This is a temporary fix until I can snipe the bug.
58+
all.map(function(item){
59+
item.date = moment.utc(item.published_at).format('LL');
60+
if (item.date == moment.utc(Date.now()).format('LL')){
61+
item.date = 'Today'
62+
} else if (item.date == moment.utc(Date.now()).subtract(1, 'days').format('LL')){
63+
item.date = 'Yesterday'
64+
}else{
65+
item.date = moment(item.date).format('dddd, LL');
66+
}
67+
if (item.news){
68+
news.push(item);
69+
}else{
70+
flow.push(item);
71+
}
72+
});
73+
if(flow.length){
74+
lastDay = flow[flow.length - 1].date;
75+
}
76+
more = (flow.length === parseInt(total[0].count)) ? false : true;
77+
flow = _.groupBy(flow, 'date');
78+
res.render('news/index', {flow_collection: flow, news_collection: news, more: more, lastDay: lastDay });
79+
});
80+
});
81+
}
1382
});
1483

15-
/* POST subscribe an email to JS5 list. */
84+
/* POST subscribe an email to JS6 list. */
1685
router.post('/subscribe', parseForm, function(req, res) {
1786
mc.lists.subscribe(
1887
{

server/routes/news.js

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -129,74 +129,6 @@ router.
129129
res.redirect('/news');
130130
}).
131131

132-
get('/', parseForm, function(req, res) {
133-
var offset = req.query.page;
134-
var more;
135-
if (offset) {
136-
Articles.paginated(offset, function(all) {
137-
Articles.totalPublished(function(total) {
138-
var lastDay;
139-
// TODO: Move date functionality into a serivce. It'll be used practically
140-
// everywhere. Oh, and refactor this blasphemy.
141-
//
142-
// TODO: We shouldn't be needing to use moment in order to make the time
143-
// UTC. There's an issue with pg and it's parsing the dates from the db
144-
// incorrectly. This is a temporary fix until I can snipe the bug.
145-
all.map(function(item){
146-
item.date = moment.utc(item.published_at).format('LL');
147-
if (item.date == moment.utc(Date.now()).format('LL')){
148-
item.date = 'Today'
149-
} else if (item.date == moment.utc(Date.now()).subtract(1, 'days').format('LL')){
150-
item.date = 'Yesterday'
151-
}else{
152-
item.date = moment(item.date).format('dddd, LL');
153-
}
154-
});
155-
// There are strings and integers here - not so good.
156-
if (all.length) {
157-
lastDay = all[all.length - 1].date;
158-
}
159-
more = (all.length == (total[0].count - offset )) ? false : true;
160-
all = _.groupBy(all, 'date');
161-
res.json({flow: all, more: more, lastDay: lastDay});
162-
});
163-
});
164-
}else{
165-
Articles.recent(function(all) {
166-
Articles.totalPublished(function(total) {
167-
var flow = [], news = [], lastDay;
168-
// TODO: Move date functionality into a serivce. It'll be used practically
169-
// everywhere. Oh, and refactor this blasphemy.
170-
//
171-
// TODO: We shouldn't be needing to use moment in order to make the time
172-
// UTC. There's an issue with pg and it's parsing the dates from the db
173-
// incorrectly. This is a temporary fix until I can snipe the bug.
174-
all.map(function(item){
175-
item.date = moment.utc(item.published_at).format('LL');
176-
if (item.date == moment.utc(Date.now()).format('LL')){
177-
item.date = 'Today'
178-
} else if (item.date == moment.utc(Date.now()).subtract(1, 'days').format('LL')){
179-
item.date = 'Yesterday'
180-
}else{
181-
item.date = moment(item.date).format('dddd, LL');
182-
}
183-
if (item.news){
184-
news.push(item);
185-
}else{
186-
flow.push(item);
187-
}
188-
});
189-
if(flow.length){
190-
lastDay = flow[flow.length - 1].date;
191-
}
192-
more = (flow.length === parseInt(total[0].count)) ? false : true;
193-
flow = _.groupBy(flow, 'date');
194-
res.render('news/index', {flow_collection: flow, news_collection: news, more: more, lastDay: lastDay });
195-
});
196-
});
197-
}
198-
}).
199-
200132
get('/new', cookieParser, csrfProtection, function(req, res) {
201133
if(!req.isAuthenticated()){
202134
req.session.returnTo = '/news' + req.path
@@ -341,4 +273,3 @@ router.
341273
});
342274

343275
module.exports = router;
344-

0 commit comments

Comments
 (0)