Skip to content

Commit

Permalink
KnowledgeGroup POST and GET routes
Browse files Browse the repository at this point in the history
  • Loading branch information
nehal96 committed Jul 23, 2019
1 parent 0dacaa8 commit edaee1c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = app => {
require("./contentCreatorRoutes")(app),
require("./contentModuleRoutes")(app),
require("./contentRoutes")(app),
require("./knowledgeGroupRoutes")(app),
require("./knowledgeMapRoutes")(app),
require("./sourceMapRoutes")(app)
);
Expand Down
49 changes: 49 additions & 0 deletions routes/knowledgeGroupRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const mongoose = require("mongoose");
const KnowledgeGroup = mongoose.model("knowledgeGroup");
const arrayToObjectById = require("../utils/helpers");

module.exports = app => {
// POST request to create a knowledge group
app.post("/api/knowledge-group/create", async (req, res) => {
const { name } = req.body;

const knowledgeGroup = new KnowledgeGroup({
name
});

try {
await knowledgeGroup.save();

res.send(knowledgeGroup);
} catch (error) {
res.send(error);
}
});

// GET request to fetch knowledge groups by id
app.get("/api/knowledge-group/by-id", (req, res) => {
KnowledgeGroup.find({}, (error, data) => {
if (error) {
res.send(error);
} else {
const knowledgeGroupById = arrayToObjectById(data);
res.send(knowledgeGroupById);
}
});
});

// GET request to fetch knowledge groups grouped by name
app.get("/api/knowledge-group/by-name", (req, res) => {
KnowledgeGroup.find({}, (error, data) => {
if (error) {
res.send(error);
} else {
const knowledgeGroupByName = {};

data.forEach(kGroup => (knowledgeGroupByName[kGroup.name] = kGroup));

res.send(knowledgeGroupByName);
}
});
});
};

0 comments on commit edaee1c

Please sign in to comment.