From edaee1c4694ade8fd94f353ab9b5375da1f76cfa Mon Sep 17 00:00:00 2001 From: Nehal Udyavar Date: Tue, 23 Jul 2019 01:27:20 -0700 Subject: [PATCH] KnowledgeGroup POST and GET routes --- routes/index.js | 1 + routes/knowledgeGroupRoutes.js | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 routes/knowledgeGroupRoutes.js diff --git a/routes/index.js b/routes/index.js index afa4557..713d279 100644 --- a/routes/index.js +++ b/routes/index.js @@ -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) ); diff --git a/routes/knowledgeGroupRoutes.js b/routes/knowledgeGroupRoutes.js new file mode 100644 index 0000000..a9d1cde --- /dev/null +++ b/routes/knowledgeGroupRoutes.js @@ -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); + } + }); + }); +};