@@ -2,8 +2,10 @@ import { NextFunction, Request, Response } from 'express'
22
33import UserCourseService from './userCourse.service'
44import { serialize } from './userCourse.serializer'
5+ import { serialize as userSerialize } from '../user/user.serializer'
56
67import { GenericResponse , NotFound , Updated } from '../../utils/apiResponse.utils'
8+ import UserService from '../user/user.service'
79
810export async function getAll ( req : Request , res : Response , next : NextFunction ) {
911 try {
@@ -54,6 +56,25 @@ export async function detail(req: Request, res: Response, next: NextFunction) {
5456 }
5557}
5658
59+ export async function getInstructor ( req : Request , res : Response , next : NextFunction ) {
60+ try {
61+ const cid = parseInt ( req . params . courseId )
62+
63+ const instructorInfo = await UserCourseService . instructor ( cid )
64+ if ( ! instructorInfo ) return res . status ( 400 ) . json ( { message : `no instructors found for course ${ cid } ` } )
65+
66+ const userInfo = await UserService . retrieve ( instructorInfo . userId )
67+ if ( ! userInfo ) return res . status ( 400 ) . json ( { message : `no instructors found for course ${ cid } ` } )
68+
69+ // strip out unnecessary info
70+ const { isAdmin, createdAt, ...responsePayload } = userSerialize ( userInfo )
71+
72+ res . status ( 200 ) . json ( responsePayload )
73+ } catch ( err ) {
74+ next ( err )
75+ }
76+ }
77+
5778export async function detailByUser ( req : Request , res : Response , next : NextFunction ) {
5879 try {
5980 const courseId = parseInt ( req . params . courseId )
@@ -122,7 +143,7 @@ export async function checkEnroll(req: Request, res: Response, next: NextFunctio
122143export async function _delete ( req : Request , res : Response , next : NextFunction ) {
123144 try {
124145 const id = parseInt ( req . params . courseId )
125- console . log ( " DELETE PARAMS: " , req . params )
146+ console . log ( ' DELETE PARAMS: ' , req . params )
126147 const currentUser = req . currentUser ?. userId
127148 if ( ! currentUser ) return res . status ( 401 ) . json ( { message : 'Unauthorized' } )
128149
@@ -140,7 +161,7 @@ export async function _delete(req: Request, res: Response, next: NextFunction) {
140161export async function _deleteUser ( req : Request , res : Response , next : NextFunction ) {
141162 try {
142163 const courseID = parseInt ( req . params . courseId )
143- console . log ( " DELETE PARAMS2: " , req . params )
164+ console . log ( ' DELETE PARAMS2: ' , req . params )
144165 // const currentUser = req.currentUser?.userId
145166 const userID = parseInt ( req . params . id )
146167 if ( ! userID ) return res . status ( 401 ) . json ( { message : 'Unauthorized' } )
@@ -194,4 +215,5 @@ export default {
194215 checkEnroll,
195216 addStudents,
196217 dropStudents,
218+ getInstructor,
197219}
0 commit comments