Skip to content

Commit

Permalink
Sort post and source by created day (#78)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
khanghuynh92 authored and phatpham9 committed Sep 30, 2017
1 parent 495f2e5 commit cdd586f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 7 deletions.
5 changes: 2 additions & 3 deletions controllers/PostsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = {
Expand Down Expand Up @@ -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));
}
Expand Down
3 changes: 1 addition & 2 deletions controllers/SourcesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ 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
? (params.page.value > 0
? 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) {
Expand Down
2 changes: 1 addition & 1 deletion models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion models/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cdd586f

Please sign in to comment.