diff --git a/src/controllers/bmdashboard/bmNewLessonController.js b/src/controllers/bmdashboard/bmNewLessonController.js new file mode 100644 index 000000000..049c79222 --- /dev/null +++ b/src/controllers/bmdashboard/bmNewLessonController.js @@ -0,0 +1,27 @@ +const mongoose = require('mongoose'); + +const bmNewLessonController = function (BuildingNewLesson) { + const bmGetLessonList = async (req, res) => { + try { + BuildingNewLesson + .find() + .populate() + .then(result => res.status(200).send(result)) + .catch(error => res.status(500).send(error)); + } catch (err) { + res.json(err); + } + }; + const bmPostLessonList = async (req, res) => { + try { + const newLesson = BuildingNewLesson.create(req.body) + .then(result => res.status(201).send(result)) + .catch(error => res.status(500).send(error)); + } catch (err) { + res.json(err); + } + }; + return { bmPostLessonList, bmGetLessonList }; +}; + +module.exports = bmNewLessonController; diff --git a/src/models/bmdashboard/buildingNewLesson.js b/src/models/bmdashboard/buildingNewLesson.js new file mode 100644 index 000000000..21bf77fc6 --- /dev/null +++ b/src/models/bmdashboard/buildingNewLesson.js @@ -0,0 +1,14 @@ +const mongoose = require('mongoose'); + +const { Schema } = mongoose; + +const buildingNewLesson = new Schema({ + title: { type: String, required: true, maxLength: 20 }, + content: { type: String, required: true, maxLength: 500 }, + date: { type: Date, required: true, default: Date.now() }, + author: { type: mongoose.SchemaTypes.ObjectId, ref: 'userProfile', required: true }, + tags: [{ type: String, required: true, maxLength: 10 }], + relatedProject: { type: mongoose.SchemaTypes.ObjectId, ref: 'buildingProject', required: true }, +}); + +module.exports = mongoose.model('buildingNewLesson', buildingNewLesson, 'buildingNewLessons'); diff --git a/src/routes/bmdashboard/bmNewLessonRouter.js b/src/routes/bmdashboard/bmNewLessonRouter.js new file mode 100644 index 000000000..b1cf3fb85 --- /dev/null +++ b/src/routes/bmdashboard/bmNewLessonRouter.js @@ -0,0 +1,15 @@ +const express = require('express'); + +const routes = function (buildingNewLesson) { + const NewLessonRouter = express.Router(); + const controller = require('../../controllers/bmdashboard/bmNewLessonController')(buildingNewLesson); + + // having GET request just for testing: + NewLessonRouter.route('/lessons') + .get(controller.bmGetLessonList); + + NewLessonRouter.route('/lessons/new') + .post(controller.bmPostLessonList); + return NewLessonRouter; +}; +module.exports = routes; diff --git a/src/startup/routes.js b/src/startup/routes.js index 492e455cd..4701a9f61 100644 --- a/src/startup/routes.js +++ b/src/startup/routes.js @@ -24,6 +24,7 @@ const mouseoverText = require('../models/mouseoverText'); // const inventoryItemMaterial = require('../models/inventoryItemMaterial'); const mapLocations = require('../models/mapLocation'); const buildingProject = require('../models/bmdashboard/buildingProject'); +const buildingNewLesson = require('../models/bmdashboard/buildingNewLesson'); // const buildingMaterial = require('../models/bmdashboard/buildingMaterial'); const { invTypeBase, @@ -78,6 +79,7 @@ const mapLocationRouter = require('../routes/mapLocationsRouter')(mapLocations); const bmLoginRouter = require('../routes/bmdashboard/bmLoginRouter')(); const bmMaterialsRouter = require('../routes/bmdashboard/bmMaterialsRouter')(buildingMaterial); const bmProjectRouter = require('../routes/bmdashboard/bmProjectRouter')(buildingProject); +const bmNewLessonRouter = require('../routes/bmdashboard/bmNewLessonRouter')(buildingNewLesson); const bmConsumablesRouter = require('../routes/bmdashboard/bmConsumablesRouter')(buildingConsumable); const bmInventoryTypeRouter = require('../routes/bmdashboard/bmInventoryTypeRouter')(invTypeBase, materialType, consumableType, reusableType, toolType, equipmentType); const bmToolRouter = require('../routes/bmdashboard/bmToolRouter')(buildingTool); @@ -116,7 +118,8 @@ module.exports = function (app) { app.use('/api/bm', bmLoginRouter); app.use('/api/bm', bmMaterialsRouter); app.use('/api/bm', bmProjectRouter); + app.use('/api/bm', bmNewLessonRouter); app.use('/api/bm', bmInventoryTypeRouter); app.use('/api/bm', bmToolRouter); app.use('/api/bm', bmConsumablesRouter); -}; \ No newline at end of file +};