Skip to content

Commit

Permalink
Merge pull request #164 from makeopensource/62-edit-course-add/drop-s…
Browse files Browse the repository at this point in the history
…tudents

revert user.controller.ts to previous working state
  • Loading branch information
NeemZ16 authored Oct 28, 2024
2 parents 8aff878 + 3613a4a commit 6fe2a8b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 130 deletions.
153 changes: 23 additions & 130 deletions devU-api/src/entities/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -42,27 +20,27 @@ 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) {
next(err)
}
}

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) {
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -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,
}
export default { get, detail, post, put, _delete, getByCourse }
4 changes: 4 additions & 0 deletions devU-api/src/entities/userCourse/userCourse.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6fe2a8b

Please sign in to comment.