Skip to content

Commit

Permalink
Added api to search Course, Concept and Content
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant-jain-94 committed Oct 21, 2017
1 parent b60785a commit 8310c1c
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 2 deletions.
12 changes: 11 additions & 1 deletion ontology_processors_api/app.js
@@ -1,19 +1,29 @@
// const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');

const courses = require('./course/course.router');
const concepts = require('./concept/concept.router');
const contents = require('./content/content.router');

const log = require('./commons/logger');
const bunyanMiddleware = require('bunyan-middleware');

const app = express();

// Enabling CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});

// bunyanMiddleware gives us request id and unique logger's for every incoming request.
app.use(bunyanMiddleware({
requestStart: true,
logger: log
}));


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

Expand Down
6 changes: 6 additions & 0 deletions ontology_processors_api/concept/concept.controller.js
Expand Up @@ -105,6 +105,12 @@ ConceptController.fetchConceptById = (conceptId, options, cb) => {
ConceptController.executeQueryAndFetchResults(query, cb);
};

ConceptController.search = (searchTerm, options, cb) => {
const skip = (options.page - 1) * options.limit;
const query = `MATCH (m:concept) where m.name CONTAINS '${searchTerm}' OR m.displayName CONTAINS '${searchTerm}' or m.url CONTAINS '${searchTerm}' return DISTINCT m ORDER BY m.courseId SKIP ${skip} LIMIT ${options.limit}`;
ConceptController.executeQueryAndFetchResults(query, cb);
};

ConceptController.fetchAllRelatedItems = (conceptId, options, cb) => {
async.parallel([
ConceptController.fetchConceptById.bind(null, conceptId, options),
Expand Down
12 changes: 12 additions & 0 deletions ontology_processors_api/concept/concept.router.js
Expand Up @@ -63,6 +63,18 @@ router.get('/:conceptId/subconcepts', (req, res, next) => {
});
});

router.post('/search', (req, res, next) => {
const pageOptions = {
page: req.query.page ? req.query.page : 1,
limit: 30
};

conceptController.search(req.body.searchTerm, pageOptions, (err, data) => {
if(!err) res.json(data);
else next(err);
});
});

router.get('/:conceptId/details', (req, res, next) => {
const options = {
page: req.query.page ? req.query.page : 1,
Expand Down
8 changes: 7 additions & 1 deletion ontology_processors_api/content/content.controller.js
Expand Up @@ -59,6 +59,12 @@ ContentController.fetchRelatedCourses = (contentId, options, cb) => {
ContentController.executeQueryAndFetchResults(query, cb);
};

ContentController.search = (searchTerm, options, cb) => {
const skip = (options.page - 1) * options.limit;
const query = `MATCH (m:content) where m.name CONTAINS '${searchTerm}' OR m.displayName CONTAINS '${searchTerm}' or m.url CONTAINS '${searchTerm}' return DISTINCT m ORDER BY m.courseId SKIP ${skip} LIMIT ${options.limit}`;
ContentController.executeQueryAndFetchResults(query, cb);
};

ContentController.fetchAllRelatedItems = (contentId, options, cb) => {
async.parallel([
ContentController.fetchContentById.bind(null, contentId, options),
Expand Down Expand Up @@ -104,4 +110,4 @@ ContentController.fetchAllRelatedItems = (contentId, options, cb) => {
};


module.exports = ContentController;
module.exports = ContentController;
13 changes: 13 additions & 0 deletions ontology_processors_api/content/content.router.js
Expand Up @@ -42,6 +42,7 @@ router.get('/:contentId/courses', (req, res, next) => {
page: req.query.page ? req.query.page : PAGE_NUMBER,
limit: req.query.limit ? req.query.limit : LIMIT_RESULTS,
};

contentController.fetchRelatedCourses(req.params.contentId, options, (err, data) => {
if (!err) res.json(data);
else {
Expand All @@ -63,4 +64,16 @@ router.get('/:contentId/details', (req, res, next) => {
});
});

router.post('/search', (req, res, next) => {
const pageOptions = {
page: req.query.page ? req.query.page : 1,
limit: 30
};

contentController.search(req.body.searchTerm, pageOptions, (err, data) => {
if(!err) res.json(data);
else next(err);
});
});

module.exports = router;
10 changes: 10 additions & 0 deletions ontology_processors_api/course/course.controller.js
Expand Up @@ -46,6 +46,16 @@ const fetchAllCourses = (options, cb) => {
});
};

const search = (searchTerm, options, cb) => {
const skip = (options.page - 1) * options.limit;
const query = `MATCH (m:content) where m.name CONTAINS '${searchTerm}' OR m.displayName CONTAINS '${searchTerm}' or m.url CONTAINS '${searchTerm}' return DISTINCT m ORDER BY m.courseId SKIP ${skip} LIMIT ${options.limit}`;
neo4j.queryExecutor(query, (err, result) => {
if(err) return cb(err, null);
const courses = result.records.map(record => record._fields[0].properties);
return cb(null, courses);
});
};

const fetchAllConceptsAssociatedWithCourse = (courseId, options, cb) => {
const skip = (options.page - 1) * options.maxResults;
const query = `MATCH (m:course {courseId: '${courseId}'})<-[:usedIn]-(n:resource)-[:explains]->(p:concept) return DISTINCT p ORDER BY p.name SKIP ${skip} LIMIT ${options.maxResults}`;
Expand Down
12 changes: 12 additions & 0 deletions ontology_processors_api/course/course.router.js
Expand Up @@ -45,6 +45,18 @@ router.get('/:courseId/contents', (req, res, next) => {
});
});

router.post('/search', (req, res, next) => {
const pageOptions = {
page: req.query.page ? req.query.page : 1,
limit: 30
};

courseController.search(req.body.searchTerm, pageOptions, (err, data) => {
if(!err) res.json(data);
else next(err);
});
});

router.get('/:courseId/details', (req, res, next) => {
const pageOptions = {
page: req.query.page ? req.query.page : 1,
Expand Down
67 changes: 67 additions & 0 deletions ontology_processors_api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ontology_processors_api/package.json
Expand Up @@ -14,6 +14,7 @@
"body-parser": "^1.17.2",
"bunyan": "^1.8.12",
"bunyan-format": "^0.2.1",
"bunyan-middleware": "^0.8.0",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"express": "~4.14.0",
Expand Down
67 changes: 67 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -19,6 +19,7 @@
"gulp": "^3.9.1"
},
"dependencies": {
"bunyan-middleware": "^0.8.0",
"docco": "^0.7.0",
"global": "^4.3.2",
"mocha": "^4.0.1",
Expand Down

0 comments on commit 8310c1c

Please sign in to comment.