Skip to content

Commit

Permalink
Implemented start and after date filters
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantoine committed Sep 16, 2015
1 parent 8addc83 commit a925fc2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bloql",
"version": "0.5.2",
"version": "0.6.0",
"description": "Blog engine powered by blog engine powered by React using Relay and GraphQL to interact with data",
"scripts": {
"clean": "rm -rf dist/",
Expand Down
6 changes: 3 additions & 3 deletions src/client/PostList.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function createBlog(RelayPostList, PostList) {
initialVariables: {
count: PostList.postCount || 10,

beforeDate: PostList.beforeDate || null,
afterDate: PostList.afterDate || null,
startDate: PostList.startDate || null,
endDate: PostList.endDate || null,
date: PostList.date || null,
categories: PostList.categories || null,
tags: PostList.tags || null,
Expand All @@ -50,7 +50,7 @@ function createBlog(RelayPostList, PostList) {
blog: () => {
return Relay.QL`
fragment on Blog {
posts(first: $count beforeDate:$beforeDate afterDate:$afterDate date:$date categories:$categories tags:$tags) {
posts(first: $count startDate:$startDate endDate:$endDate date:$date categories:$categories tags:$tags) {
${RelayPostList.getFragment('posts')}
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/server/schema/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ export function getBlog() {

// Function to call by filter
var filterFunctions = {
startDate: startDateFilter,
endDate: endDateFilter,
date: dateFilter,
title: stringFilter,
slug: stringFilter,
categories: arrayFilterAnd,
tags: arrayFilterAnd,
};

// Filter by date equality field
// Filter by string equality field
function stringFilter(a, b) {
return a === b;
}
Expand All @@ -60,6 +62,16 @@ function dateFilter(a, b) {
return moment(a).format('YYYYMMDD') === moment(b).format('YYYYMMDD');
}

// Filter by date equality field
function startDateFilter(a, b, meta) {
return moment(meta.date).isAfter(moment(b));
}

// Filter by date equality field
function endDateFilter(a, b, meta) {
return moment(meta.date).isBefore(moment(b));
}

function isNullOrUndefined(a) {
return _.isNull(a) || _.isUndefined(a);
}
Expand All @@ -86,7 +98,7 @@ function checkFilters(filtersParam, meta) {

// Check that every filter pass
return _.every(filters, function (value, filter) {
return filterFunctions[filter](meta[filter], value);
return filterFunctions[filter](meta[filter], value, meta);
});

}
Expand Down
8 changes: 4 additions & 4 deletions src/server/schema/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ export const blogType = new GraphQLObjectType({
type: connection.connectionType,
description: 'Blog posts',
args: _.extend(connectionArgs, {
beforeDate: {
startDate: {
type: GraphQLString
},
afterDate: {
endDate: {
type: GraphQLString
},
date: {
Expand All @@ -104,8 +104,8 @@ export const blogType = new GraphQLObjectType({
}
}),
resolve: (blog, args) => connectionFromArray(getPostList({
beforeDate: args.beforeDate,
afterDate: args.afterDate,
startDate: args.startDate,
endDate: args.endDate,
date: args.date,
categories: args.categories,
tags: args.tags
Expand Down

0 comments on commit a925fc2

Please sign in to comment.