From cdd586f0fc0941e1a0f412fd7d30aa431a4346ff Mon Sep 17 00:00:00 2001 From: Khang Huynh Date: Sat, 30 Sep 2017 14:42:30 +0700 Subject: [PATCH] Sort post and source by created day (#78) * Sort post and source by created day * feat(Close #71): read/share post apis should respond to client as soon as possible (#77) * fix comment * fix syntax --- controllers/PostsService.js | 5 ++--- controllers/SourcesService.js | 3 +-- models/post.js | 2 +- models/source.js | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/controllers/PostsService.js b/controllers/PostsService.js index 1201ca3..c6ba600 100644 --- a/controllers/PostsService.js +++ b/controllers/PostsService.js @@ -50,10 +50,10 @@ const getPosts = async (req, res, next) => { ? params.page.value : 1) - 1 : 0; - const sort = args.sort.value === 'best' ? '-meta.numViewed' : '-created.at'; + const sort = params.sort.value === 'best' ? '-meta.numViewed' : '-created.at'; const query = {}; - if (args.sort.value === 'daily') { + if (params.sort.value === 'daily') { const now = new Date(); const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()); query['created.at'] = { @@ -107,7 +107,6 @@ const getPost = async (req, res, next) => { try { const post = await Post.get(params.id.value); - if (!post) { return next(new APIError('Post not found', NOT_FOUND)); } diff --git a/controllers/SourcesService.js b/controllers/SourcesService.js index 92af42f..3317f14 100644 --- a/controllers/SourcesService.js +++ b/controllers/SourcesService.js @@ -18,7 +18,6 @@ const addUserData = (user, source) => { const getSources = async (req, res, next) => { const params = req.swagger.params; let sources = []; - try { const limit = params.limit.value || 25; const page = params.page.value @@ -26,7 +25,7 @@ const getSources = async (req, res, next) => { ? params.page.value : 1) - 1 : 0; - const sort = params.sort.value || 'title'; + const sort = params.sort.value || '-created.at'; const query = {}; if (params.query.value) { diff --git a/models/post.js b/models/post.js index 3481342..3f42c70 100644 --- a/models/post.js +++ b/models/post.js @@ -78,7 +78,7 @@ postSchema.statics = { }, list({ query, page, sort, limit, select }) { return this.find(query || {}) - .sort(sort || 'title') + .sort(sort || '-created.at') .select(select || 'id title content image url source meta') .skip((limit || 0) * (page || 0)) .limit(limit || 0) diff --git a/models/source.js b/models/source.js index 4708a4a..3f17117 100644 --- a/models/source.js +++ b/models/source.js @@ -38,7 +38,7 @@ sourceSchema.statics = { }, list({ query, page, sort, limit, select }) { return this.find(query || {}) - .sort(sort || 'title') + .sort(sort || '-created.at') .select(select || 'id title') .skip((limit || 0) * (page || 0)) .limit(limit || 0)