Skip to content

Commit

Permalink
fixes bugs related to delete course
Browse files Browse the repository at this point in the history
  • Loading branch information
AVtheking committed Nov 21, 2023
1 parent 258620e commit ba10624
Show file tree
Hide file tree
Showing 84 changed files with 55 additions and 22 deletions.
6 changes: 6 additions & 0 deletions controllers/course_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ const courseCtrl = {
path: "videos.video",
select:
"_id videoTitle videoUrl videoDuration videoUrl_144p videoUrl_360p videoUrl_720p",
})
.populate({
path: "preview",
select:
"_id videoTitle videoUrl videoDuration videoUrl_144p videoUrl_360p videoUrl_720p",
});

if (!course) {
return next(new ErrorHandler(400, "No course found"));
}
Expand Down
67 changes: 47 additions & 20 deletions controllers/teacher_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const teacherCtrl = {

const existingTitle = await Course.findOne({ title });
if (existingTitle) {
fs.unlinkSync("public/thumbnail" + "/" + req.file.filename);
return next(
new ErrorHandler(400, "Please select different course title")
);
Expand All @@ -158,6 +159,7 @@ const teacherCtrl = {
},
});
} catch (e) {
fs.unlinksync("public/thumbnail" + "/" + req.file.filename);
next(e);
}
},
Expand All @@ -176,10 +178,24 @@ const teacherCtrl = {
const { videoTitle } = req.body;
const result2 = await videoSchema.validateAsync({ videoTitle });
const videotitle = result2.videoTitle;
const notesfile = req.files.notes;
const videofile = req.files.video;

let course = await Course.findById(courseId);
if (notesfile) {
noteFilePath = "public/course_notes" + "/" + notesfile[0].filename;
}
if (!videofile) {
if (noteFilePath != null) {
fs.unlinkSync(noteFilePath);
}
return next(new ErrorHandler(400, "Please upload a video file"));
}
videoFilePath = "public/course_videos" + "/" + videofile[0].filename;

if (!course) {
fs.unlinkSync(noteFilePath);
fs.unlinkSync(videoFilePath);
return next(
new ErrorHandler(
400,
Expand All @@ -189,22 +205,16 @@ const teacherCtrl = {
}

if (!course.createdBy.equals(req.user._id)) {
fs.unlinkSync(noteFilePath);
fs.unlinkSync(videoFilePath);
return next(new ErrorHandler(400, "You are not the creater of course"));
}
if (course.isPublished) {
fs.unlinkSync(noteFilePath);
fs.unlinkSync(videoFilePath);
return next(new ErrorHandler(400, "Course is already published"));
}

const notesfile = req.files.notes;
const videofile = req.files.video;
if (!videofile) {
return next(new ErrorHandler(400, "Please upload a video file"));
}
if (notesfile) {
noteFilePath = "public/course_notes" + "/" + notesfile[0].filename;
}
videoFilePath = "public/course_videos" + "/" + videofile[0].filename;

// course.notes.push(noteFilePath);

//Video Conversion using worker threads
Expand Down Expand Up @@ -302,10 +312,10 @@ const teacherCtrl = {
name: category,
courses: [courseId],
});
newCategory.save();
await newCategory.save();
} else {
existingCategory.courses.push(course.Id);
existingCategory.save();
existingCategory.courses.push(course._id);
await existingCategory.save();
}
res.json({
success: true,
Expand Down Expand Up @@ -358,7 +368,7 @@ const teacherCtrl = {
if (!req.file) {
return next(new ErrorHandler(400, "Please upload a file"));
}
fs.unlinkSync(course.thumbnail);
fs.unlinkSync("public/thumbnail" + "/" + req.file.filename);
course.thumbnail = "thumbnail" + "/" + req.file.filename;
// await Course.findByIdAndUpdate(courseId, {
// thumbnail: "public/thumbnail" + "/" + req.file.filename,
Expand All @@ -369,6 +379,7 @@ const teacherCtrl = {
message: "Thumbnail changed successfully",
});
} catch (e) {
fs.unlinkSync("public/thumbnail" + "/" + req.file.filename);
next(e);
}
},
Expand All @@ -386,23 +397,34 @@ const teacherCtrl = {
new ErrorHandler(400, "You are not the creater of the course")
);
}

const category = await Category.findOne({ name: course.category });
const catCourseIndex = category.courses.indexOf(courseId);
if (catCourseIndex != -1) {
category.courses.splice(catCourseIndex, 1);
await category.save();
}

const courseIndex = req.user.createdCourse.indexOf(courseId);
req.user.createdCourse.splice(courseIndex, 1);
await req.user.save();
fs.unlinkSync(course.thumbnail);
fs.unlinkSync("public" + "/" + course.thumbnail);
for (const videoId of course.videos) {
const video = await Video.findById(videoId);
const video = await Video.findById(videoId.video);

if (video) {
await Video.findByIdAndDelete(videoId);
await Video.findByIdAndDelete(videoId.video);
fs.unlinkSync(video.videoUrl);
fs.unlinkSync(video.videoUrl_144p);
fs.unlinkSync(video.videoUrl_360p);
fs.unlinkSync(video.videoUrl_720p);
}
}
for (const note of course.notes) {
fs.unlinkSync(note);
for (const notes of course.videos) {
if (notes.note == null) {
continue;
}
fs.unlinkSync(notes.note);
}
await Course.findByIdAndDelete(courseId);
res.json({
Expand All @@ -420,16 +442,18 @@ const teacherCtrl = {

const result = await videoSchema.validateAsync(req.body);
const videoTitle = result.videoTitle;
videoFilePath = "public/course_videos" + "/" + req.file.filename;
let course = await Course.findById(courseId);
if (!course) {
fs.unlinkSync(videoFilePath);
return next(new ErrorHandler(404, "Course not found"));
}
if (!course.createdBy.equals(req.user._id)) {
fs.unlinkSync(videoFilePath);
return next(
new ErrorHandler(400, "You are not the creater of the course")
);
}
videoFilePath = "public/course_videos" + "/" + req.file.filename;
const conversionPromise = resolutions.map((resolution) => {
inputFilePath = videoFilePath;
inputFileName = path.basename(
Expand Down Expand Up @@ -466,6 +490,9 @@ const teacherCtrl = {
message: "Lecture added successfully",
});
} catch (e) {
if (videoFilePath) {
fs.unlinkSync(videoFilePath);
}
next(e);
}
},
Expand Down
4 changes: 2 additions & 2 deletions controllers/user_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const userCtrl = {
},
});
} catch (error) {
fs.unlinksync("public/thumbnail" + "/" + req.file.filename);
next(error);
}
},
Expand Down Expand Up @@ -342,15 +343,14 @@ const userCtrl = {
});
if (!user) {
return next(new ErrorHandler(404, "No user found"));

}
res.json({
success: true,
message: "user found",
data: {
user,
},
})
});
} catch (e) {
next(e);
}
Expand Down
Binary file removed public/course_notes/notes-1700111331571.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700111347410.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700111776357.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700111779975.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700112240474.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700112246899.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700112374400.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700112381985.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700568312575.pdf
Binary file not shown.
Binary file removed public/course_notes/notes-1700573169339.pdf
Binary file not shown.
File renamed without changes.
Binary file removed public/course_videos/video-1700111347381-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111347381-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111347381-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111347381.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111776327-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111776327-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111776327-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111776327.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111779942-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111779942-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111779942-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700111779942.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112240436-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112240436-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112240436-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112240436.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112246857-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112246857-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112246857-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112246857.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112374357-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112374357-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112374357-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112374357.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112381936-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112381936-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112381936-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700112381936.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700568279122.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700568281932.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700568312527-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700568312527-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700568312527-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700568312527.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700572809980-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700572809980-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700572809980-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700573169298-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700573169298-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700573169298-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700573169298.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700573542399-144p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700573542399-360p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700573542399-720p.mp4
Binary file not shown.
Binary file removed public/course_videos/video-1700573542399.mp4
Binary file not shown.
Binary file removed public/thumbnail/1699903999908.png
Binary file not shown.
Binary file removed public/thumbnail/1699904006114.png
Binary file not shown.
Binary file removed public/thumbnail/1699904068553.png
Binary file not shown.
Binary file removed public/thumbnail/1699904098595.png
Binary file not shown.
Binary file removed public/thumbnail/1699904125867.png
Binary file not shown.
Binary file removed public/thumbnail/1699964145332.jpg
Binary file not shown.
Binary file removed public/thumbnail/1699964184412.jpg
Binary file not shown.
Binary file removed public/thumbnail/1699966011371.png
Binary file not shown.
Binary file removed public/thumbnail/1699966015776.png
Binary file not shown.
Binary file removed public/thumbnail/1699966026546.png
Binary file not shown.
Binary file removed public/thumbnail/1700111313385.png
Binary file not shown.
Binary file removed public/thumbnail/1700112188957.png
Binary file not shown.
Binary file removed public/thumbnail/1700112196894.png
Binary file not shown.
Binary file removed public/thumbnail/1700112361069.png
Binary file not shown.
Binary file removed public/thumbnail/1700300919360.jpg
Binary file not shown.
File renamed without changes
File renamed without changes

0 comments on commit ba10624

Please sign in to comment.