Add course list for school and sanitize course
authorchapel <jacob.chapel@gmail.com>
Sun, 13 Nov 2011 00:17:19 +0000 (16:17 -0800)
committerchapel <jacob.chapel@gmail.com>
Sun, 13 Nov 2011 00:17:19 +0000 (16:17 -0800)
app.js
models.js

diff --git a/app.js b/app.js
index 601dda1d926ad0e7474f4995a4a1bfb55fb70580..34b4c757e231f46fbf55bb658ba7accf667841b8 100644 (file)
--- a/app.js
+++ b/app.js
@@ -493,40 +493,9 @@ app.get( '/schools', checkAjax, loadUser, function( req, res ) {
     if( schools ) {
       // If schools are found, loop through them gathering any courses that are
       // associated with them and then render the page with that information.
-      async.forEach(
-        schools,
-        function( school, callback ) {
-          // Check if user is authorized with each school
-          school.authorize( user, function( authorized ) {
-            // This is used to display interface elements for those users
-            // that are are allowed to see them, for instance a 'New Course' button.
-            var sanitizedSchool = school.sanitized;
-            sanitizedSchool.authorized = authorized;
-            // Find all courses for school by it's id and sort by name
-            Course.find( { 'school' : school._id } ).sort( 'name', '1' ).run( function( err, courses ) {
-              // If any courses are found, set them to the appropriate school, otherwise
-              // leave empty.
-              if( courses.length > 0 ) {
-                sanitizedSchool.courses = courses.filter(function(course) {
-                  if (!course.deleted) return course;
-                });
-              } else {
-                sanitizedSchool.courses = [];
-              }
-              schoolList.push(sanitizedSchool);
-              // This tells async (the module) that each iteration of forEach is
-              // done and will continue to call the rest until they have all been
-              // completed, at which time the last function below will be called.
-              callback();
-            });
-          });
-        },
-        // After all schools and courses have been found, render them
-        function( err ) {
-          //res.render( 'schools', { 'schools' : schools } );
-          res.json({ 'schools' : schoolList });
-        }
-      );
+      res.json({ 'schools' : schools.map(function(school) {
+        return school.sanitized;
+      })})
     } else {
       // If no schools have been found, display none
       //res.render( 'schools', { 'schools' : [] } );
@@ -535,6 +504,36 @@ app.get( '/schools', checkAjax, loadUser, function( req, res ) {
   });
 });
 
+app.get( '/school/:id', checkAjax, loadUser, loadSchool, function( req, res ) {
+  var school = req.school;
+  var user = req.user;
+
+  school.authorize( user, function( authorized ) {
+    // This is used to display interface elements for those users
+    // that are are allowed to see th)m, for instance a 'New Course' button.
+    var sanitizedSchool = school.sanitized;
+    sanitizedSchool.authorized = authorized;
+    // Find all courses for school by it's id and sort by name
+    Course.find( { 'school' : school._id } ).sort( 'name', '1' ).run( function( err, courses ) {
+      // If any courses are found, set them to the appropriate school, otherwise
+      // leave empty.
+      if( courses.length > 0 ) {
+        sanitizedSchool.courses = courses.filter(function(course) {
+          if (!course.deleted) return course;
+        }).map(function(course) {
+          return course.sanitized;
+        });
+      } else {
+        sanitizedSchool.courses = [];
+      }
+      // This tells async (the module) that each iteration of forEach is
+      // done and will continue to call the rest until they have all been
+      // completed, at which time the last function below will be called.
+      res.json({ 'school': sanitizedSchool })
+    });
+  });
+});
+
 // New course page
 // Displays form to create new course
 // Private, requires user to be authorized
index 87d5c72349d80e0d59491e5f1bd5ba636e51d0ba..f9bea517884dc5cd846eaf2457a6e85a1dadbea9 100644 (file)
--- a/models.js
+++ b/models.js
@@ -134,6 +134,7 @@ var SchoolSchema = new Schema( {
 
 SchoolSchema.virtual( 'sanitized' ).get(function() {
   var school = {
+    _id: this._id,
     name: this.name,
     description: this.description,
     url: this.url
@@ -169,6 +170,19 @@ var CourseSchema = new Schema( {
        users                           : Array
 });
 
+CourseSchema.virtual( 'sanitized' ).get(function() {
+  var course = {
+    _id: this._id,
+    name: this.name,
+    number: this.number,
+    description: this.description,
+    subject: this.subject,
+    department: this.department
+  }
+
+  return course;
+});
+
 CourseSchema.virtual( 'displayName' )
        .get( function() {
                if( this.number ) {