NathanBland / Sprout

An open learning platform written with node js
MIT License
0 stars 1 forks source link

New Proposed Schema #5

Open NathanBland opened 9 years ago

NathanBland commented 9 years ago

@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

var mongoose = require('mongoose')
  , Schema = mongoose.Schema
/**
* This is an overview of the proposed schema,
* and is probably not complete.
* please add missing things.
*/
var organizationSchema = Schema({
  _id: Number,
  name: String,     //'Union College'
  short_name: String, //'union_college'
  location: String  //'Lincoln, NE'
  staff: [{type: Schema.Types.Objectid, ref: 'Staff'}],
  students: [{type: Schema.Types.Objectid, ref: 'Student'}],
  //maybe students should only be in coures?
  courses: [{type: Schema.Types.Objectid, ref: 'Course'}]
})

var courseSchema = Schema({
  _id: Number,
  name: String,       // 'Web Technologies'
  short_name: String, // 'web_tech'
  description: String,
  location: String,
  pre_reqs: [{type: Schema.Types.Objectid, ref: 'Course'}],
  instructors: [{type: Schema.Types.Objectid, ref: 'Staff'}],
  students: [{type: Schema.Types.Objectid, ref: 'Student'}],
  resources: [{type: Schema.Types.Objectid, ref: 'Resource'}]
})
var lessonSchema = Schema({
  _id: Number,
  name: String,       // 'Peter Piper'
  short_name: String, // 'peter_piper'
  description: String,
  content: String, // Markdown? main content
  resources: [{type: Schema.Types.Objectid, ref: 'Resource'}]
})
var lessonStatusSchema = Schema({
  /**
  * This could probably be a sub-doc
  * of either student, or lesson.
  * But for clarity, for now it is here.
  */
  //_id: Number,
  lesson: {type: Schema.Types.Objectid, ref: 'Lesson'},
  student: {type: Schema.Types.Objectid, ref: 'Student'},
  started: Boolean,
  /*
  * Between these two we should be able to determine
  * if they have started, finished, or something in between.
  */
  completed: Boolean
})

var resourceSchema = Schema({
  _id: Number,
  title: String,
  //Not sure if a description attribute is needed here or not?
  type: String, //file, URL, image, video, etc.
  uri: String
})

var staffSchema = Schema({
  _user: { type: Schema.Types.ObjectId, ref: 'User'},
  title: String
})
var studentSchema = Schema({
  _user: { type: Schema.Types.ObjectId, ref: 'User'},
  standing: String
})
var userSchema = Schema({
  _id: Number,
  name: String,
  age: Number
  //TODO: add auth here.
})

var Resource = mongoose.model('Resource', staffSchema)
var Lesson = mongoose.model('Lesson', staffSchema)
var Course = mongoose.model('Course', staffSchema)

var User = mongoose.model('User', staffSchema)
var Staff = mongoose.model('Staff', staffSchema)
var Student = mongoose.model('Student', staffSchema)

var LessonStatus = mongoose.model('LessonStatus', staffSchema)
var Organization = mongoose.model('Organization', organizationSchema)
NathanBland commented 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: