Browse Source

Add course delete and edit

chapel 8 years ago
parent
commit
44996caaf8
4 changed files with 171 additions and 116 deletions
  1. 48 12
      app.js
  2. 45 71
      models.js
  3. 71 33
      views/course/form.jade
  4. 7 0
      views/course/index.jade

+ 48 - 12
app.js

@@ -336,7 +336,7 @@ function loadCourse( req, res, next ) {
   var courseId	= req.params.id;
 
   Course.findById( courseId, function( err, course ) {
-    if( course ) {
+    if( course && !course.deleted ) {
       req.course = course;
 
       // If a course is found, the user is checked to see if they are
@@ -362,7 +362,7 @@ function loadLecture( req, res, next ) {
   var lectureId	= req.params.id;
 
   Lecture.findById( lectureId, function( err, lecture ) {
-    if( lecture ) {
+    if( lecture && !lecture.deleted ) {
       req.lecture = lecture;
 
       // If a lecture is found, the user is checked to see if they are
@@ -392,7 +392,7 @@ function loadNote( req, res, next ) {
   Note.findById( noteId, function( err, note ) {
     // If a note is found, and user is set, check if
     // user is authorized to interact with that note.
-    if( note && user ) {
+    if( note && user && !note.deleted ) {
       note.authorize( user, function( auth ) {
         if( auth ) {
           // If authorzied, then set req.note to be used later
@@ -414,7 +414,7 @@ function loadNote( req, res, next ) {
           res.redirect( '/' );
         }
       })
-    } else if ( note && note.public ) {
+    } else if ( note && note.public && !note.deleted ) {
       // If note is found, but user is not set because they are not
       // logged in, and the note is public, set the note to read only
       // and store the note for later.
@@ -422,7 +422,7 @@ function loadNote( req, res, next ) {
       req.RO = true;
 
       next();
-    } else if ( note && !note.public ) {
+    } else if ( note && !note.public && !note.deleted ) {
       // If the note is found, but user is not logged in and the note is
       // not public, then ask them to login to view the note. Once logged
       // in they will be redirected to the note, at which time authorization
@@ -434,7 +434,7 @@ function loadNote( req, res, next ) {
       // No note was found
       req.flash( 'error', 'Invalid note specified!' );
 
-      res.redirect( '/login' );
+      res.redirect( '/schools' );
     }
   });
 }
@@ -497,7 +497,9 @@ app.get( '/schools', loadUser, function( req, res ) {
               // If any courses are found, set them to the appropriate school, otherwise
               // leave empty.
               if( courses.length > 0 ) {
-                school.courses = courses;
+                school.courses = courses.filter(function(course) {
+                  if (!course.deleted) return course;
+                });
               } else {
                 school.courses = [];
               }
@@ -726,10 +728,44 @@ app.get( '/course/:id', loadUser, loadCourse, function( req, res ) {
   });
 });
 
+// Edit Course
+app.get( '/course/:id/edit', loadUser, loadCourse, function( req, res) {
+  var course = req.course;
+  var user = req.user;
+
+  if ( user.admin ) {
+    res.render( 'course/new', {course: course} )
+  } else {
+    req.flash( 'error', 'You don\'t have permission to do that' )
+    res.redirect( '/schools' );
+  }
+})
+
+// Recieve Course Edit Form
+app.post( '/course/:id/edit', loadUser, loadCourse, function( req, res ) {
+  var course = req.course;
+  var user = req.user;
+
+  if (user.admin) {
+    var courseChanges = req.body;
+    course.number = courseChanges.number;
+    course.name = courseChanges.name;
+    course.description = courseChanges.description;
+    course.department = courseChanges.department;
+
+    course.save(function(err) {
+      if (err) {
+        req.flash( 'error', 'There was an error saving the course' );
+      }
+      res.redirect( '/course/'+ course._id.toString());
+    })
+  } else {
+    req.flash( 'error', 'You don\'t have permission to do that' )
+    res.redirect( '/schools' );
+  }
+});
+
 // Delete Course
-// XXX Non functioning 
-// Will be used as apart of a larger admin interface for managing courses and
-// lectures on the site.
 app.get( '/course/:id/delete', loadUser, loadCourse, function( req, res) {
   var course = req.course;
   var user = req.user;
@@ -737,8 +773,8 @@ app.get( '/course/:id/delete', loadUser, loadCourse, function( req, res) {
   if ( user.admin ) {
     course.delete(function( err ) {
       if ( err ) req.flash( 'info', 'There was a problem removing course: ' + err )
-        else req.flash( 'info', 'Successfully removed course' )
-          res.redirect( '/schools' );
+      else req.flash( 'info', 'Successfully removed course' )
+      res.redirect( '/schools' );
     });
   } else {
     req.flash( 'error', 'You don\'t have permission to do that' )

+ 45 - 71
models.js

@@ -153,6 +153,7 @@ var CourseSchema = new Schema( {
 	// XXX: room for additional resources
   created     : { type : Date, default : Date.now },
   creator     : ObjectId,
+  deleted     : Boolean,
 
 	// many users may subscribe to a course
 	users				: Array
@@ -203,7 +204,14 @@ CourseSchema.method( 'delete', function( callback ) {
   var id = this._id;
 
   Course.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
-    callback( err );
+    if (callback) callback( err );
+    Lecture.find( { course: id }, function( err, lectures) {
+      if (lectures.length > 0) {
+        lectures.forEach(function(lecture) {
+          lecture.delete();
+        })
+      }
+    })
   })
 });
 
@@ -216,6 +224,7 @@ var LectureSchema	= new Schema( {
 	date					: { type : Date, default: Date.now },
 	live					: Boolean,
   creator       : ObjectId,
+  deleted       : Boolean,
 
 	course				: ObjectId
 });
@@ -232,6 +241,24 @@ LectureSchema.method( 'authorize', function( user, cb ) {
 	});
 });
 
+LectureSchema.method( 'delete', function( callback ) {
+  var id = this._id;
+
+  Lecture.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
+    if (callback) callback( err );
+    Note.find( { lecture : id }, function(err, notes) {
+      notes.forEach(function(note) {
+        note.delete();
+      })
+    })
+    Post.find( { lecture : id }, function(err, posts) {
+      posts.forEach(function(post) {
+        post.delete();
+      })
+    })
+  })
+});
+
 var Lecture = mongoose.model( 'Lecture', LectureSchema );
 
 // notes
@@ -244,6 +271,7 @@ var NoteSchema = new Schema( {
 	visits				: Number,
   created         : { type : Date, default : Date.now },
   creator       : ObjectId,
+  deleted       : Boolean,
 
 	lecture				: ObjectId,
 
@@ -268,6 +296,14 @@ NoteSchema.method( 'addVisit', function() {
 	Note.collection.update( { '_id' : id }, { '$inc' : { 'visits' : 1 } } );
 });
 
+NoteSchema.method( 'delete', function( callback ) {
+  var id = this._id;
+
+  Note.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
+    if (callback) callback( err );
+  })
+});
+
 var Note = mongoose.model( 'Note', NoteSchema );
 
 // comments
@@ -285,81 +321,19 @@ var PostSchema = new Schema({
 
   comments   : Array,
 
-  lecture   : String // ObjectId
+  lecture   : String, // ObjectId
+  deleted   : Boolean
 })
 
-mongoose.model( 'Post', PostSchema );
-
-
-// Deleted documents
-
-var DeletedCourse = new Schema( {
-	name				: { type : String, required : true },
-	number			: String,
-	description	: String,
-  instructor  : ObjectId,
-	// courses are tied to one school
-	school			: ObjectId,
-
-	// XXX: room for additional resources
-  created     : { type : Date, default : Date.now },
-  creator     : ObjectId,
-
-	// many users may subscribe to a course
-	users				: Array
-});
-
-
-mongoose.model( 'DeletedCourse', DeletedCourse )
-
-var DeletedLecture = new Schema( {
-	name					: { type : String, required : true },
-	date					: { type : Date, default: Date.now },
-	live					: Boolean,
-  creator       : ObjectId,
-
-	course				: ObjectId
-});
-
-
-mongoose.model( 'DeletedLecture', DeletedLecture )
-
-var DeletedNote = new Schema( {
-	name					: { type : String, required : true },
-	path					: String,
-  public        : Boolean,
-  roID          : String,
-	visits				: Number,
-  created         : { type : Date, default : Date.now },
-  creator       : ObjectId,
-
-	lecture				: ObjectId,
+PostSchema.method( 'delete', function( callback ) {
+  var id = this._id;
 
-	collaborators : [String]
+  Post.collection.update( { '_id' : id }, { '$set' : { 'deleted' : true } }, function( err ) {
+    if (callback) callback( err );
+  })
 });
 
-
-mongoose.model( 'DeletedNote', DeletedNote )
-
-var DeletedPost = new Schema({
-  date      : { type : Date, default : Date.now },
-  body      : String,
-  votes     : [String],
-  reports   : [String],
-  public    : Boolean,
-
-  userid    : String, // ObjectId,
-  userName  : String,
-  userAffil : String,
-
-  comments   : Array,
-
-  lecture   : String // ObjectId
-})
-
-
-mongoose.model( 'DeletedPost', DeletedPost )
-
+mongoose.model( 'Post', PostSchema );
 
 var ArchivedCourse = new Schema({
   id: Number,

+ 71 - 33
views/course/form.jade

@@ -1,38 +1,76 @@
 div.content
 	div.container1
 		form( method = 'POST' )
-			h1 Create New Course
-			p
-				| Use this form to create a new course.
-				| After creating the course, you can create a lecture with in it.
+			- var course = course || false;
+			- if (course)
+				h1 Edit Course
+				p
+					| Use this form to edit a course.
 
-			div.row
-				div.label Course number
-				div.field
-					input( type = 'text', name = 'number' )
+				div.row
+					div.label Course number
+					div.field
+						input( type = 'text', name = 'number', value = course.number )
 
-			div.row
-				div.label Course name
-				div.field
-					input( type = 'text', name = 'name' )
-			div.row
-				div.label Course subject
-				div.field
-					input( type = 'text', name = 'subject' )
-			div.row
-				div.label Course department
-				div.field
-					input( type = 'text', name = 'department' )
-			div.row
-				div.label Instructor's Name
-				div.field
-					input( type = 'text', name = 'instructorName' )
-			div.row
-				div.label Instructor's Email
-				div.field
-					input( type = 'text', name = 'email' )
-			div.row
-				div( class = 'label')
-					 
-				div( class = 'field')
-					button Create Course
+				div.row
+					div.label Course name
+					div.field
+						input( type = 'text', name = 'name', value = course.name )
+				div.row
+					div.label Course subject
+					div.field
+						input( type = 'text', name = 'subject', value = course.subject )
+				div.row
+					div.label Course department
+					div.field
+						input( type = 'text', name = 'department', value = course.department )
+				//
+					div.row
+						div.label Instructor's Name
+						div.field
+							input( type = 'text', name = 'instructorName' )
+					div.row
+						div.label Instructor's Email
+						div.field
+							input( type = 'text', name = 'email' )
+				div.row
+					div( class = 'label')
+						 
+					div( class = 'field')
+						button Save Course
+			- else
+				h1 Create New Course
+				p
+					| Use this form to create a new course.
+					| After creating the course, you can create a lecture with in it.
+
+				div.row
+					div.label Course number
+					div.field
+						input( type = 'text', name = 'number' )
+
+				div.row
+					div.label Course name
+					div.field
+						input( type = 'text', name = 'name' )
+				div.row
+					div.label Course subject
+					div.field
+						input( type = 'text', name = 'subject' )
+				div.row
+					div.label Course department
+					div.field
+						input( type = 'text', name = 'department' )
+				div.row
+					div.label Instructor's Name
+					div.field
+						input( type = 'text', name = 'instructorName' )
+				div.row
+					div.label Instructor's Email
+					div.field
+						input( type = 'text', name = 'email' )
+				div.row
+					div( class = 'label')
+						 
+					div( class = 'field')
+						button Create Course

+ 7 - 0
views/course/index.jade

@@ -26,4 +26,11 @@ div.content
 			div
 				span(class='sub_menu')
 					a( href = '/course/#{ course._id }/lecture/new' ) New Lecture
+		- if ( user.admin )
+			div
+				span(class='sub_menu')
+					a( href = '/course/#{ course._id }/edit' ) Edit Course
+			div
+				span(class='sub_menu')
+					a( href = '/course/#{ course._id }/delete' ) Delete Course