Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const users = require('./src/routes/users');
const authJwt = require('./src/middlewares/authJWT');
const interaction = require('./src/routes/interaction');
const stats = require('./src/routes/stats');
const trees = require('./src/routes/trees');
// Add other service routes here. e.g. questionaires


Expand Down Expand Up @@ -54,3 +55,4 @@ app.use('/api/questionnaire/', questionnaire)
app.use('/api/user/', users)
app.use('/api/interaction/', interaction)
app.use('/api/stats/', stats)
app.use('/api/trees/', trees)
78 changes: 78 additions & 0 deletions src/controllers/trees.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const Tree = require("../models/tree");
const axios = require('axios');

module.exports.create = async (req, res) => {
const data = new Tree(req.body)
try {
const dataToSave = await data.save();
res.status(200).json(dataToSave)
}
catch (error) {
res.status(400).json({ message: error.message })
}
}

module.exports.list = async (req, res) => {
try {
const path = req.query.path
console.log(path)
if (path) {
const data = await Tree.find({ path: path });
res.json(data)
} else {
const data = await Tree.find({});
res.json(data)
}

}
catch (error) {
res.status(500).json({ message: error.message })
}
}


module.exports.get = async (req, res) => {
try {
const data = await Tree.findById(req.params.id);
if (data) {
res.json(data);
} else {
res.status(404).json({ message: "not found" });
}
} catch (error) {
res.status(500).json({ message: error });
}
};


module.exports.update = async (req, res) => {
try {
const id = req.params.id;
const updatedData = req.body;
const options = { new: true };

const result = await Tree.findByIdAndUpdate(
id, updatedData, options
)
if (result) {
res.send(result)
} else {
res.status(404).json({ message: "Tree not found" })
}
}
catch (error) {
res.status(400).json({ message: error.message })
}
}



// module.exports.delete = async (req, res) => {
// try {
// const id = req.params.id;
// const data = await Usecase.findByIdAndDelete(id);
// res.send(`Document with ${data.name} has been deleted..`);
// } catch (error) {
// res.status(400).json({ message: error.message });
// }
// };
20 changes: 20 additions & 0 deletions src/models/tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

// To support the BT Editor Queries

const mongoose = require('mongoose');
const treeSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
}, { strict: false, timestamps: true });

// Duplicate the ID field.
treeSchema.virtual('id').get(function(){
return this._id.toHexString();
});

treeSchema.set('toJSON', {
virtuals: true
});
module.exports = mongoose.model('Tree', treeSchema)
16 changes: 16 additions & 0 deletions src/routes/trees.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');
const router = express.Router()
const treectrl = require('../controllers/trees');

//----------------------------------------------------
// Trees Related Endpoints
//---------------------------------------------------

// Capital P to support the BT Editor Code
router.get('/Projects', treectrl.list);
router.get('/Projects/:id', treectrl.get)

router.post('/Projects', treectrl.create);
router.patch('/Projects/:id', treectrl.update)

module.exports = router;