expressServer/controllers/userController.js
LingandRX fbbcf675d1 refactor(user): 重构用户相关代码
- 优化了用户路由、控制器、仓库和服务的代码结构
- 改进了代码的可读性和可维护性
-移除了冗余的导入和导出语句
- 统一了异步函数的定义方式- 简化了错误处理逻辑
2025-01-05 18:44:46 +08:00

128 lines
4.3 KiB
JavaScript

import { body, validationResult } from 'express-validator'
import logger from 'morgan'
import userService from '../services/userService.js'
import FetchResult from '../common/web/fetchResult.js'
import messages from '../config/messages.js'
import { HTTP_STATUS } from '../common/constant/httpStatus.js'
import { SearchQuery } from '../models/search.js'
const UserController = {
async getAllUsers(res) {
try {
const users = await userService.getUserList()
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, 'success', users)
} catch (err) {
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
}
},
async findUserList(req, res) {
try {
const { page, size, sort } = req.query
const search = new SearchQuery({ page: page, size: size, sort: sort })
const result = await userService.getUserList(search)
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, 'success', result)
} catch (err) {
console.log(err)
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
}
},
createUser: [
body('account').isLength({ min: 3 }).withMessage('Account must be at least 3 characters long'),
body('account').isEmpty().withMessage('Account is required'),
body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters long'),
body('password').isEmpty().withMessage('Password is required'),
async (req, res, next) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return FetchResult.formatResult(
res,
HTTP_STATUS.BAD_REQUEST,
errors
.array()
.map((err) => err.msg)
.join(', ')
)
}
next()
},
async (req, res) => {
try {
const user = req.body
await userService.createUser(user)
return FetchResult.formatResult(res, HTTP_STATUS.CREATED, messages.user.created)
} catch (err) {
logger('Error creating user: ', err)
if (err.message === messages.user.alreadyExists) {
return FetchResult.formatResult(res, HTTP_STATUS.CONFLICT, messages.user.alreadyExists)
}
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
}
}
],
login: [
body('account').notEmpty().withMessage('Account is required'),
body('password').notEmpty().withMessage('Password is required'),
async (req, res, next) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
console.log(errors)
return FetchResult.formatResult(
res,
400,
errors
.array()
.map((err) => err.msg)
.join(', ')
)
}
next()
},
async (req, res) => {
try {
const { account, password } = req.body
req.session.user = await userService.login(account, password)
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, messages.user.login)
} catch (err) {
if (err.message === messages.user.not_found) {
return FetchResult.formatResult(res, HTTP_STATUS.NOT_FOUND, messages.user.accountPasswordNotMatch)
}
logger('Error logging in: ', err)
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
}
}
],
logout: [
async (req, res) => {
try {
req.session.destroy()
return FetchResult.formatResult(res, HTTP_STATUS.OK, messages.user.logout)
} catch (err) {
logger('Error logging out: ', err)
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
}
}
],
async getUserExists(req, res) {
try {
const { account } = req.query
const exist = await userService.getUserExists(account)
if (!exist) {
return FetchResult.formatResult(res, HTTP_STATUS.NOT_FOUND, messages.user.not_found)
}
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, messages.user.exists)
} catch (err) {
logger('Error checking user existence: ', err)
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
}
},
}
export default UserController