Open NathanBland opened 9 years ago
In combination with this, an api would need to be in place to make the application easier to access. A sample of such a route is listed below...
var express = require('express');
var router = express.Router();
var Course = require('../models/Course.js');
router.route('/')
/**
* Should include .get, .post, .put, .delete
* Since this is a sample, it only has .get, and .post
*/
/*
* .get lists all courses currently in the database.
* depending on how this resource is accessed, it should
* limit by organization, but as this is an example,
* and there are no organizations to limit by,
* this is not implemented.
*/
.get(, function(req, res) {
Course.find().exec(function(err, courses) {
if (err) return res.status(500).json(
{
description:'failed to find courses'
})
return res.status(400).json(courses)
}
})
/*
* .post is used for creating a new course.
* Currently expects a minimum of a short_name
* which could actually be parsed (in fact should be)
* from the main name attribute.
*/
.post(function(req, res){
if (!req.body.short_name){
return res.status(400).json({description: 'No name specified'})
}
Course.findOne({
'short_name': req.body.short_name
}, function(err, course) {
if (err)
return done(err);
if (course) {
return return res.status(409)
.json({
description: 'There is already a course with that name'
})
}
else {
var newCourse = new Course()
newCourse = req.body
newCourse.save(function(err) {
if (err) {
res.status(500).json({
description: 'There was a problem saving the course'
})
}
else {
response = newCourse
return res.status(201).json(response)
})
}
}
})
})
/*
* .put goes here
*/
/*
* .delete goes here
*/
module.exports = router;
This would be loaded by a file in the main router/app and would/could look something like this:
var resource = require('./Resource')
var lesson = require('./Lesson')
var course = require('./Course')
var user = require('./User')
var staff = require('./Staff')
var student = require('./Student')
var lessonStatus = require('./LessonStatus')
var organization = require('./Organization')
app.use('/resource', resource)
app.use('/lesson', lesson)
app.use('/course', course)
app.use('/user', user)
app.use('/staff', staff)
app.use('/student', student)
app.use('/lessonStatus', lessonStatus)
app.use('/organization', organization)
Such a route would end up looking like http://edu.sprout.io/course/
for the basic version and a more developed version would allow things like:
http://edu.sprout.io/orginization/union_college/course/web_tech
http://edu.sprout.io/orginization/union_college/course/web_tech/lesson
http://edu.sprout.io/orginization/union_college/course/web_tech/lesson/peter-piper
Depending on if an organizational structure was implemented, this would allow more easily for duplicate class names, but would also hurt the ability of people to create a course that wasn't tied to an organization.
@Malachi-M What do you think of this? @barberboy if malachi and I finished this up with this revised data, is it something you could see yourself implementing to help us test it out?
Please comment, and add thoughts/collections that you think would be helpful/needed.
_It should be noted that everywhere the _id is shown to be of type number, while this is fine, it breaks the ref to other objects currently, this can be fixed by either changing the ref type to Number, or by changing the id type to objectid