-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cf8b5b
commit bc71322
Showing
12 changed files
with
478 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Request, Response } from 'express'; | ||
import asyncHandler from 'express-async-handler'; | ||
import { getAllCourses, | ||
getCourseById, | ||
createCourse, | ||
deleteCourse, | ||
updateCourse, | ||
} from '../service/course.service'; | ||
|
||
export const courseList = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const courses = await getAllCourses(); | ||
res.status(200).json(courses); | ||
}); | ||
|
||
export const courseDetails = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const courseId = parseInt(req.params.id); | ||
const course = await getCourseById(courseId); | ||
|
||
if (course) { | ||
res.status(200).json(course); | ||
} else { | ||
res.status(404).json({ message: 'Course not found' }); | ||
} | ||
}); | ||
|
||
export const createCourseGet = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
res.status(200).json({ message: 'Provide course data to create a course' }); | ||
}); | ||
|
||
export const createCoursePost = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const courseData = req.body; | ||
const newCourse = await createCourse(courseData); | ||
res.status(201).json(newCourse); | ||
}); | ||
|
||
export const deleteCourseGet = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
res.status(200).json({ message: 'Confirm course deletion' }); | ||
}); | ||
|
||
export const deleteCoursePost = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const courseId = parseInt(req.params.id); | ||
const course = await getCourseById(courseId); | ||
|
||
if (course) { | ||
await deleteCourse(course); | ||
res.status(200).json({ message: 'Course deleted' }); | ||
} else { | ||
res.status(404).json({ message: 'Course not found' }); | ||
} | ||
}); | ||
|
||
export const updateCourseGet = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
res.status(200).json({ message: 'Provide updated course data' }); | ||
}); | ||
|
||
export const updateCoursePost = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const courseId = parseInt(req.params.id); | ||
const course = await getCourseById(courseId); | ||
|
||
if (course) { | ||
const updatedData = req.body; | ||
const updatedCourse = await updateCourse(course, updatedData); | ||
res.status(200).json(updatedCourse); | ||
} else { | ||
res.status(404).json({ message: 'Course not found' }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { getAllUsers, | ||
getUserById, | ||
createUser, | ||
updateUser, | ||
deleteUser | ||
} from '../service/user.service'; | ||
import { Request, Response } from 'express'; | ||
import asyncHandler from 'express-async-handler'; | ||
|
||
export const userList = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const users = await getAllUsers(); | ||
res.status(200).json(users); | ||
}); | ||
|
||
export const userDetails = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const userId = parseInt(req.params.id); | ||
const user = await getUserById(userId); | ||
|
||
if (user) { | ||
res.status(200).json(user); | ||
} else { | ||
res.status(404).json({ message: 'User not found' }); | ||
} | ||
}); | ||
|
||
export const userCreateGet = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
res.status(200).json({ message: 'Display user creation form' }); | ||
}); | ||
|
||
export const userCreatePost = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const newUserData = req.body; | ||
const newUser = await createUser(newUserData); | ||
res.status(201).json(newUser); | ||
}); | ||
|
||
export const userDeleteGet = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const userId = parseInt(req.params.id); | ||
const user = await getUserById(userId); | ||
|
||
if (user) { | ||
res.status(200).json({ message: 'Confirm deletion of user', user }); | ||
} else { | ||
res.status(404).json({ message: 'User not found' }); | ||
} | ||
}); | ||
|
||
export const userDeletePost = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const userId = parseInt(req.params.id); | ||
await deleteUser(userId); | ||
res.status(204).send(); | ||
}); | ||
|
||
export const userUpdateGet = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const userId = parseInt(req.params.id); | ||
const user = await getUserById(userId); | ||
|
||
if (user) { | ||
res.status(200).json(user); | ||
} else { | ||
res.status(404).json({ message: 'User not found' }); | ||
} | ||
}); | ||
|
||
export const userUpdatePost = | ||
asyncHandler(async (req: Request, res: Response) => { | ||
const userToUpdate = await getUserById(parseInt(req.params.id)); | ||
const updatedUserData = req.body; | ||
if (!userToUpdate) { | ||
throw new Error('User not found'); | ||
} | ||
const updatedUser = await updateUser(userToUpdate, updatedUserData); | ||
|
||
if (updatedUser) { | ||
res.status(200).json(updatedUser); | ||
} else { | ||
res.status(404).json({ message: 'User not found' }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
|
||
export const isAdmin = (req: Request, res: Response, next: NextFunction) => { | ||
if (req.session?.user?.role === 'admin') { | ||
return next(); | ||
} else { | ||
res.status(403).json({ message: 'Access forbidden: Admins only' }); | ||
} | ||
}; | ||
|
||
// Middleware to check if the user is a professor or admin | ||
export const isProfessor = (req: Request, res: Response, next: NextFunction) => { | ||
if (req.session?.user?.role === 'professor') { | ||
return next(); | ||
} | ||
res.status(403).json({ message: 'Access forbidden: Professors only' }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Router } from 'express'; | ||
import * as courseController from '../controllers/course.controller'; | ||
|
||
const router: Router = Router(); | ||
|
||
router.get('/', courseController.courseList); | ||
|
||
router.get('/create', courseController.createCourseGet); | ||
|
||
router.get('/:id', courseController.courseDetails); | ||
|
||
router.post('/create', courseController.createCoursePost); | ||
|
||
router.get('/:id/delete', courseController.deleteCourseGet); | ||
|
||
router.post('/:id/delete', courseController.deleteCoursePost); | ||
|
||
router.get('/:id/update', courseController.updateCourseGet); | ||
|
||
router.post('/:id/update', courseController.updateCoursePost); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Router } from 'express' | ||
import userRoutes from './user.routes' | ||
import courseRoutes from './course.routes' | ||
|
||
// **** Variables **** // | ||
const router = Router(); | ||
|
||
/* GET home page. */ | ||
router.use('/users', userRoutes) | ||
router.use('/courses', courseRoutes) | ||
|
||
// **** Export default **** // | ||
export default router; |
Oops, something went wrong.