From 3613a4abb087d19662a370635aedee174df394ca Mon Sep 17 00:00:00 2001 From: NeemZ16 <110858265+NeemZ16@users.noreply.github.com> Date: Mon, 28 Oct 2024 17:09:30 -0400 Subject: [PATCH] revert user.controller.ts to previous working state and update userCourse.controller.ts to fix missing catch --- devU-api/src/entities/user/user.controller.ts | 153 +++--------------- .../userCourse/userCourse.controller.ts | 4 + 2 files changed, 27 insertions(+), 130 deletions(-) diff --git a/devU-api/src/entities/user/user.controller.ts b/devU-api/src/entities/user/user.controller.ts index 5b2593a..48e1772 100644 --- a/devU-api/src/entities/user/user.controller.ts +++ b/devU-api/src/entities/user/user.controller.ts @@ -1,37 +1,15 @@ -import { NextFunction, Request, Response } from 'express' +import { Request, Response, NextFunction } from 'express' -import UserCourseService from './userCourse.service' -import { serialize } from './userCourse.serializer' +import UserService from './user.service' import { GenericResponse, NotFound, Updated } from '../../utils/apiResponse.utils' -export async function getAll(req: Request, res: Response, next: NextFunction) { - try { - const userCourses = await UserCourseService.listAll() - - res.status(200).json(userCourses.map(serialize)) - } catch (err) { - next(err) - } -} +import { serialize } from './user.serializer' export async function get(req: Request, res: Response, next: NextFunction) { try { - const id = parseInt(req.params.id) - const userCourses = await UserCourseService.list(id) - - res.status(200).json(userCourses.map(serialize)) - } catch (err) { - next(err) - } -} - -export async function getByCourse(req: Request, res: Response, next: NextFunction) { - try { - const id = parseInt(req.params.courseId) - const userCourses = await UserCourseService.listByCourse(id) - - const response = userCourses.map(serialize) + const users = await UserService.list() + const response = users.map(serialize) res.status(200).json(response) } catch (err) { @@ -42,11 +20,11 @@ export async function getByCourse(req: Request, res: Response, next: NextFunctio export async function detail(req: Request, res: Response, next: NextFunction) { try { const id = parseInt(req.params.id) - const userCourse = await UserCourseService.retrieve(id) + const user = await UserService.retrieve(id) - if (!userCourse) return res.status(404).json(NotFound) + if (!user) return res.status(404).json(NotFound) - const response = serialize(userCourse) + const response = serialize(user) res.status(200).json(response) } catch (err) { @@ -54,15 +32,15 @@ export async function detail(req: Request, res: Response, next: NextFunction) { } } -export async function detailByUser(req: Request, res: Response, next: NextFunction) { +export async function getByCourse(req: Request, res: Response, next: NextFunction) { try { - const courseId = parseInt(req.params.courseId) - const userId = parseInt(req.params.userId) - const userCourse = await UserCourseService.retrieveByCourseAndUser(courseId, userId) + const courseId = parseInt(req.params.id) + const userRole = req.query.role - if (!userCourse) return res.status(404).json(NotFound) - - const response = serialize(userCourse) + const users = await UserService.listByCourse(courseId, userRole as string) + const response = users.map(u => { + if (u) return serialize(u) + }) res.status(200).json(response) } catch (err) { @@ -72,30 +50,22 @@ export async function detailByUser(req: Request, res: Response, next: NextFuncti export async function post(req: Request, res: Response, next: NextFunction) { try { - const userCourse = await UserCourseService.create(req.body) - if (userCourse === null) { - return res.status(409).json('User already enrolled') - } - - const response = serialize(userCourse) + const user = await UserService.create(req.body) + const response = serialize(user) res.status(201).json(response) } catch (err) { if (err instanceof Error) { - res.status(400).json(new GenericResponse(err.message)) + res.status(400).json(new GenericResponse(err.message)) } } } export async function put(req: Request, res: Response, next: NextFunction) { try { - req.body.courseId = parseInt(req.params.id) - const currentUser = req.currentUser?.userId - if (!currentUser) return res.status(401).json({ message: 'Unauthorized' }) - - req.body.userId = currentUser + req.body.id = parseInt(req.params.id) + const results = await UserService.update(req.body) - const results = await UserCourseService.update(req.body) if (!results.affected) return res.status(404).json(NotFound) res.status(200).json(Updated) @@ -104,48 +74,10 @@ export async function put(req: Request, res: Response, next: NextFunction) { } } -export async function checkEnroll(req: Request, res: Response, next: NextFunction) { - try { - const courseId = parseInt(req.params.courseId) - const userId = req.currentUser?.userId - if (!userId) return res.status(401).json({ message: 'Unauthorized' }) - - const userCourse = await UserCourseService.checking(userId, courseId) - if (!userCourse) return res.status(404).json(NotFound) - - res.status(200).json(serialize(userCourse)) - } catch (err) { - next(err) - } -} - export async function _delete(req: Request, res: Response, next: NextFunction) { try { - const id = parseInt(req.params.courseId) - console.log("DELETE PARAMS: ", req.params) - const currentUser = req.currentUser?.userId - if (!currentUser) return res.status(401).json({ message: 'Unauthorized' }) - - const results = await UserCourseService._delete(id, currentUser) - - if (!results.affected) return res.status(404).json(NotFound) - - res.status(204).send() - } catch (err) { - next(err) - } -} - - -export async function _deleteUser(req: Request, res: Response, next: NextFunction) { - try { - const courseID = parseInt(req.params.courseId) - console.log("DELETE PARAMS2: ", req.params) - // const currentUser = req.currentUser?.userId - const userID = parseInt(req.params.id) - if (!userID) return res.status(401).json({ message: 'Unauthorized' }) - - const results = await UserCourseService._delete(courseID, userID) + const id = parseInt(req.params.id) + const results = await UserService._delete(id) if (!results.affected) return res.status(404).json(NotFound) @@ -155,43 +87,4 @@ export async function _deleteUser(req: Request, res: Response, next: NextFunctio } } -export async function addStudents(req: Request, res: Response, next: NextFunction) { - try { - const userEmails = req.body['users'] as string[] - if (!userEmails || userEmails.length == 0) return res.status(422).json({ message: 'users field not found or is empty' }) - const courseId = parseInt(req.params.courseId) - - const result = await UserCourseService.bulkCreate(userEmails, courseId, false) - res.status(201).json(result) - } catch (err) { - next(err) - } -} - -export async function dropStudents(req: Request, res: Response, next: NextFunction) { - try { - const userEmails = req.body['users'] as string[] - if (!userEmails || userEmails.length == 0) return res.status(422).json({ message: 'users field not found or is empty' }) - const courseId = parseInt(req.params.courseId) - - const result = await UserCourseService.bulkCreate(userEmails, courseId, true) - res.status(201).json(result) - } catch (err) { - next(err) - } -} - -export default { - get, - getByCourse, - getAll, - detail, - detailByUser, - post, - put, - _delete, - _deleteUser, - checkEnroll, - addStudents, - dropStudents, -} \ No newline at end of file +export default { get, detail, post, put, _delete, getByCourse } \ No newline at end of file diff --git a/devU-api/src/entities/userCourse/userCourse.controller.ts b/devU-api/src/entities/userCourse/userCourse.controller.ts index 6db429f..c9befeb 100644 --- a/devU-api/src/entities/userCourse/userCourse.controller.ts +++ b/devU-api/src/entities/userCourse/userCourse.controller.ts @@ -150,6 +150,10 @@ export async function _deleteUser(req: Request, res: Response, next: NextFunctio if (!results.affected) return res.status(404).json(NotFound) res.status(204).send() + } catch (err) { + next(err) + } +} export async function addStudents(req: Request, res: Response, next: NextFunction) { try {