Skip to content

Commit

Permalink
✨ new feature student registration
Browse files Browse the repository at this point in the history
  • Loading branch information
reMRKableDev committed Jan 13, 2021
1 parent aa8deb9 commit ff1a5d4
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
jest.mock('../../helpers');
jest.mock('../../../../database/services/modelServices/courseServices');

const { registerToCourseController } = require('../index');
const { render500ErrorHelper } = require('../../helpers');
const {
validateMockValueToHaveBeenCalled,
} = require('../../../../utils/test-utils/validators.utils');
const {
mockRequest,
mockResponse,
} = require('../../../../utils/test-utils/interceptors.utils');
const {
findOneCourseService,
} = require('../../../../database/services/modelServices/courseServices');

let req;
let res;

describe('registerToCourse Controller Test Suite', () => {
beforeEach(() => {
req = mockRequest();
res = mockResponse();

req.user = { _id: expect.anything() };
});

afterEach(() => {
jest.clearAllMocks();
});

test('should validate render500ErrorHelper is called ', async () => {
findOneCourseService.mockReturnValueOnce(new Error());

await registerToCourseController(req, res);

validateMockValueToHaveBeenCalled(findOneCourseService);
validateMockValueToHaveBeenCalled(render500ErrorHelper);
});

test('should validate res.redirect called ', async () => {
findOneCourseService.mockImplementationOnce(() => ({
students: [],
save: jest.fn(),
}));

await registerToCourseController(req, res);

validateMockValueToHaveBeenCalled(findOneCourseService);

const { flash } = req;
const { redirect } = res;

validateMockValueToHaveBeenCalled(flash);
validateMockValueToHaveBeenCalled(redirect);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports.createNewCourseController = async (req, res) => {
}

newCourse.instructors.push(_id);
newCourse.save();
await newCourse.save();

res.redirect('/my-courses');
};
2 changes: 2 additions & 0 deletions api/controllers/coursesControllers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { createNewCourseController } = require('./createNewCourse.controller');
const { renderMyCoursesController } = require('./renderMyCourses.controller');
const { renderAllCoursesController } = require('./renderAllCourses.controller');
const { registerToCourseController } = require('./registerToCourse.controller');
const {
renderCourseDetailsController,
} = require('./renderCourseDetails.controller');
Expand All @@ -27,6 +28,7 @@ module.exports = {
createNewCourseController,
renderMyCoursesController,
renderAllCoursesController,
registerToCourseController,
renderStudentListController,
deleteTaughtCourseController,
updateTaughtCourseController,
Expand Down
22 changes: 22 additions & 0 deletions api/controllers/coursesControllers/registerToCourse.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { render500ErrorHelper } = require('../helpers');
const {
findOneCourseService,
} = require('../../../database/services/modelServices/courseServices');

exports.registerToCourseController = async (req, res) => {
const { _id } = req.user;
const { id } = req.params;

const isFoundCourse = await findOneCourseService(id);

if (isFoundCourse instanceof Error) {
render500ErrorHelper(res);
return;
}

isFoundCourse.students.push(_id);
await isFoundCourse.save();

req.flash('success_msg', 'Successfully registered to course');
res.redirect('/my-courses');
};
2 changes: 2 additions & 0 deletions api/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
const {
createNewCourseController,
renderMyCoursesController,
registerToCourseController,
renderAllCoursesController,
renderStudentListController,
updateTaughtCourseController,
Expand All @@ -41,6 +42,7 @@ module.exports = {
createNewCourseController,
renderMyCoursesController,
renderAllCoursesController,
registerToCourseController,
renderStudentListController,
updateUserProfileController,
renderUserProfileController,
Expand Down
6 changes: 6 additions & 0 deletions api/routes/courses.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
createNewCourseController,
renderMyCoursesController,
renderAllCoursesController,
registerToCourseController,
renderStudentListController,
renderTaughtCourseController,
updateTaughtCourseController,
Expand All @@ -34,6 +35,11 @@ router.get(
);
router.post(newCoursePrefix, isLoggedInUser, createNewCourseController);
router.get(courseDetailsPrefix, isLoggedInUser, renderCourseDetailsController);
router.post(
courseRegistrationPrefix,
isLoggedInUser,
registerToCourseController
);
router.get(allCoursesPrefix, isLoggedInUser, renderAllCoursesController);
router.get(myCoursesPrefix, isLoggedInUser, renderMyCoursesController);
router.get(
Expand Down
2 changes: 1 addition & 1 deletion configs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
googleAuthCallbackPrefix: '/auth/google/callback',
instructorPrefix: '/instructor',
newCoursePrefix: '/course/new',
courseRegistrationPrefix: '/course/register',
courseRegistrationPrefix: '/course/:id/register',
courseDetailsPrefix: '/course/:id/details',
allCoursesPrefix: '/all-courses',
myCoursesPrefix: '/my-courses',
Expand Down
2 changes: 1 addition & 1 deletion views/users/common/course-details.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{{else if isCurrentUserInStudentList}}
<a href="/my-courses/learn/{{isCourse._id}}">Continue Course</a>
{{else}}
<form action="/course/register" method="post">
<form action="/course/{{isCourse._id}}/register" method="post">
<button type="submit">Register To Course</button>
</form>
{{/if}}
Expand Down

0 comments on commit ff1a5d4

Please sign in to comment.